Install the Git server environment and set up your own Git server code repository.

Server environment: centos

Client: MacOS

1. Install Git on the server

1.1 Install Git using the yum command

[root@localhost home] yum install -y git
Copy the code

Wait until the installation is complete and then check the Git version number.

1.2 Checking the Git version number

[root@localhost home] git --version git version 1.8.3.1Copy the code

Git 1.8.3.1 install git with yum.

1.3 Creating git Users Combine Git users

Git [root@localhost home] useradd -g git git Git [root@localhost home] passwd git // Set the password of the user, need to be complex, otherwise Linux system will fail to check the passwordCopy the code

2. Configure the Git repository on the server

2.1 Creating a Git Repository

Set/home/data/git/gittest git for git repository, then the owner of the git repository is modified to git

[root@localhost home]# mkdir -p /home/git/data/git/gittest.git [root@localhost home]# git init --bare data/git/gittest.git Initialized empty Git repository in /home/data/git/gittest.git/ [root@localhost home]# cd data/git/  [root@localhost git]# chown -R git:git gittest.git/Copy the code

2.2 Client Clone Git Repository

Then clone the project from the Linux Git server, the default SSH port is 22

Git clone [email protected]: / home/data/gittest gitCopy the code

If the SSH port is not the default 22, run the following command (assuming the SSH port number is 8000) :

Git clone SSH: / / [email protected]:8000 / home/data/gittest gitCopy the code

Prompt when connecting to target Git for the first time

RSA key fingerprint is The authenticity of host '192.168.0.10 (192.168.0.10)' can't be established SHA256:Ve6WV/FGH059EqoUOERFoZdfmMh3B259nhkfmvdamkd. Are you sure you want to continue connecting (yes/no)?Copy the code

Enter yes. In this case, an additional file “known_hosts” will be created under SSH. In the future, the above statement will not be prompted when you connect to the target Git server again on this computer.

You need to enter a password to download the code repository. The following describes how to use SSH public keys for verification instead of entering a password each time.

3. Enable RSA authentication for Git on the server

3.1 Enabling RSA Authentication on the Server

Go to /etc/ssh and edit sshd_config to open the comments for the following three configurations:

RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
Copy the code

Save and restart the SSHD service:

systemctrl sshd restart
Copy the code

$Home/.ssh/authorized_keys = $Home/.ssh/authorized_keys = $Home/.ssh/authorized_keys So the actual store the path of the public key is/home/git /. SSH/authorized_keys

Create the.ssh directory under /home/git/ and change the owner of the.ssh folder to git

chown -R git:git .ssh
Copy the code

3.2 Creating SSH Public and Private Keys on the Client

Run the following command on the client:

ssh-keygen -t rsa -C "[email protected]"
Copy the code

In this case, two files id_rsa and id_rsa.pub are added under ~/. SSH

Id_rsa is the private key

Id_rsa.pub is public key

3.3 Importing the Client Public Key to the Git Server

Run the following command to copy the public key to the authorized_keys file on the server (you need to enter your git password for this step)

SSH [email protected] 'cat >>. SSH /authorized_keys' < ~/. SSH /id_rsa.pubCopy the code

On the server, check whether the authorized_keys file exists in. SSH.

3.4 important

Important:

Example Change the permission of the.ssh directory to 700

Example Change the permission of the.ssh/authorized_keys file to 600

chmod 700 .ssh
cd .ssh
chmod 600 authorized_keys 
Copy the code

3.5 The Client will clone the remote repository again

Git clone SSH: / / [email protected]:8000 / home/data/gittest gitCopy the code

You do not need to enter the password again.

3.6 Disabling Git Users from Logging In to the Server over SSH

The git user previously created on the server is not allowed to log in to the server over SSH

Edit/etc/passwd

Find:

git:x:502:504::/home/git:/bin/bash
Copy the code

Modified to

git:x:502:504::/home/git:/bin/git-shell
Copy the code

In this case, git users can use Git through SSH, but cannot log in to the system through SSH.