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"
git config --global ""

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

(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 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, 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]( "VA~RZY$Z_VC0]1ZKFML_LB.png")

Then enter git clone git@serverAddress:/repoPath on the client

Reference:[git official document]( Git#_getting_git_on_a_server)