Set up a Git server on your own server

Posted Jun 6, 20202 min read

Environment and tools:

  • Server:CentOS 7.2
  • Client:Window 10
  • Tools:Git, Ssh
  1. Install Git on the server

    yum install git -y

Configure git username and email

git config --global user.name "user"
git config --global user.email "user@example.com"

liunx add git user

adduser git

Switch to the git user, configure ssh, check whether the .ssh directory exists in the directory /home/git, if not, create a new .ssh directory, and create a new file authorized_keys(later used to store the client's ssh public key id_rsa. pub, one per line)

mkdir .ssh
cd .ssh
touch authorized_keys
  1. The client installs git and generates the ssh public key
    Client download and install git
    After the default installation, there will be git-bash-here in the right mouse button panel, click to enter git-bash, you must first set the git user name and mailbox, and then enter the current user directory to see whether ssh has been configured, and then generate the ssh public key and Private key

    cd ~/.ssh
    ssh-keygen -t rsa
    cat id_rsa.pub

(Here, press ENTER key 3 times after ssh-keygen -t rsa, because it is prompted whether there is a password to set, if you set it, you need to enter the password after using git, so here is the Enter key directly, set the password to be empty , Then the public key id_rsa.pub and private key id_rsa will be generated in the current folder .ssh, and then open the public key file to copy the content, and paste it on the server authorized_keys)

  1. The server sets authorized_keys
    Log in to the server, switch to the git user, and enter the ~/home/git/.ssh folder, modify the authorized_keys file, paste the contents of the previously copied id_rsa.pub, and then modify the authorized_keys file permissions, Then open the git, .ssh folder permissions(open permissions are very important here, otherwise you will be prompted to enter the password after the clone and other operations, if you are prompted to enter the password, the ssh public key configuration is definitely wrong here)

    su git
    cd /home/git/.ssh
    vim authorized_keys
    chmod 700 authorized_keys
    cd /home
    chmod 700 git
    cd git
    chmod 700 .ssh

================================================= ==========

After setting, you can use git on the client
Let's first create an empty warehouse on the server

cd /myweb
mkdir gitExample
cd gitExample
git init

![VA~RZY$Z_VC0]1ZKFML_LB.png](https://i0.wp.com/segmentfault.com/img/bVbH3A0 "VA~RZY$Z_VC0]1ZKFML_LB.png")

Then enter git clone git@serverAddress:/repoPath on the client
1591372454(1).jpg

Reference:[git official document]( https://git-scm.com/book/zh/v2/%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B8%8A%E7%9A%84-Git-%E5%9C%A8%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B8%8A%E6%90%AD%E5%BB%BA- Git#_getting_git_on_a_server)