Introduction of git

Git is currently the most popular distributed version control system, while SVN is a centralized version control system. The difference between the two can be seen from Liao Xuefeng’s centralized vs. distributed version. Currently, the popular open source website Github and code cloud Gitee both use Git to do version control, but both are open source and private ones need to pay. So try to set up your own Git service in centos

Git installation configuration

$ yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel
$ yum install gitCopy the code

Specifies the installation address of other Linux oss

Create a Git user and password

The following create user and password are git

useradd git
passwd gitCopy the code

The useradd command is used to create a new system user in Linux. Useradd can be used to create user accounts. After the account is created, run the passwd command to set the password. Userdel can be used to delete accounts. The account created using the useradd directive is actually stored in the /etc/passwd text file.

Creating a Git repository

Create a Git repository in the appropriate directory

sudo git init --bare /home/gitfolder.gitCopy the code

Adding the –bare parameter to the git init command means that you do not create a local working directory when initializing your git repository, so everything in the git folder is directly created in the current directory instead of being placed in the.git directory.

Clone code

Clone the server code on PC

Git clone [email protected]: / home/gitfolder. GitCopy the code

Here the git before [email protected] represents the useradd user name

Enter the git password (the password is git). The following information is displayed

warning: You appear to have cloned an empty repository.Copy the code

To set up the Git service, add a commit file to your working directory. Error insufficient Permission for adding an object to repository database is found because git users do not have permission to operate the folder. To grant the current gitfoler.git repository permission to the git user, run the following command on the server

sudo chown -R git:git /home/gitfolder.gitCopy the code

The chown command changes the owner and owning group of a file or directory. This command can authorize a user to become the owner of a specified file or change the owning group of a file. The user can be a user or user D. The user group can be a group name or id. The file name can be a list of files separated by Spaces, and the file name can contain wildcards. Only file owners and superusers can use this command.

So far, git server has been successfully set up, but there is a disadvantage, each clone or push requires the input of password, you can use the client’s public key to the server to achieve encryption-free communication

Generating an SSH Public Key

Because the Git server can use SSH public keys for authentication, you can generate the private key and public key on the client, and then add the public key to the authorized_keys file on the Git server. Run the command in Windows first

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

Enter all the way, in the C: \ Users \ kelen \. SSH directory to generate the corresponding private key and public key, will. SSH id_rsa in folder. The pub is appended to the contents of the server inside ~ /. SSH/authorized_keys file, To copy the local id_rsa.pub file to the authorized_keys directory, run the following command

cat id_rsa.pub >> authorized_keysCopy the code

And then restart the service

$ systemctl restart sshd.service // restart ssh serviceCopy the code

At this stage, it is found that the password still needs to be entered for each push code. The reason is that the authorized_keys permission is still not granted to the Git user, so you need to switch the Git user on the server to operate

$ su gitCopy the code

Create the.ssh folder and corresponding permissions

$ mkdir ~/.ssh && chmod 700 ~/.sshCopy the code

Add authorized_keys and corresponding permissions

$ touch ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keysCopy the code

Enabling RSA Authentication

Enable RSA authentication in the /etc/ssh/sshd_config file on the server, locate the following options, and uncomment them

RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile  .ssh/authorized_keysCopy the code

Disabling Shell Login

For security reasons, the Git user created in step 2 is not allowed to log into the shell, which can be done by editing the /etc/passwd file. Find a line similar to the following

git:x:1001:1001:,,,:/home/git:/bin/bash

git:x:1001:1001:,,,:/home/git:/usr/bin/git-shellCopy the code

In this way, git users can normally use Git over SSH, but they can’t log in to the shell because the Git-shell we specified for git users automatically logs out every time they log in


Version control