Teach you how to build a Git server on Ubuntu

It takes about 5 minutes to read this article.

Recently I was writing some scripts. In order to maintain continuity, I took time to catch up at home, so I encountered the problem of code synchronization. At that time, I thought of three methods:

Use a cloud disk, which is ready to use, but because the cloud disk also stores some other things, it is not appropriate to always synchronize in real time;
Using SVN, a traditional code hosting tool, has been in use;
Using Git, the latest distributed code hosting tool, is said to be very powerful.

Finally decided to use Git, mainly to learn the latest technology. The following are the steps to take notes, but also to guide other students:

Let me talk about the environment first:

Server: Ubuntu 16.04 x64
Client: Windows7 x64

Server configuration

First use the following command to switch to root privileges for operation:

sudo bash

When prompted for a password, just enter the root password.

After the login is successful, start to install git, command:

apt-get install git

When prompted whether to continue, enter y and press Enter to continue the installation process until the installation is complete.

Then start to install ssh, command:

apt-get install openssh-server openssh-client

Also when asked whether to continue, type y and press Enter, and the installation will be completed automatically.

Then we create a dedicated git user, the user name is also called git, the command:

adduser git

After the creation is successful, you will be prompted to set a user password. Please set a password that you can remember and continue. The subsequent detailed information can be filled in as appropriate.

Let's start to create a new git warehouse. We choose the warehouse storage directory to be under /srv and the warehouse name is myfiles.git, so the command:

git init --bare /srv/myfiles.git

Because the current user is root, in order to allow the dedicated git account to operate the warehouse directory, we need to authorize the warehouse directory to git, the command:

chown -R git:git /srv/myfiles.git/

Client operation

First, you need to download the Windows version of git, download address: https://git-scm.com/download/win

After the download is complete, click Install, and click "Next" according to the prompts until the installation is complete.

Create a working directory on the client. For example, mine is gitdir. Right-click in the working directory and click "Git Bash Here".

After the pop-up command, clone the warehouse to the local:

$ git clone [email protected]:/srv/myfiles.git
Cloning into 'myfiles'...
The authenticity of host '192.168.252.128 (192.168.252.128)' can't be established.
ECDSA key fingerprint is SHA256:zqtjAg+FGfWrT3SCp1Qa2KqhE2UOy3PmudhhrTFlm7A.
Are you sure you want to continueconnecting(yes/no)? yes
Warning: Permanently added '192.168.252.128'(ECDSA) to the list of known hosts.
[email protected]'s password:
warning: You appear to have cloned an empty repository.

Note that please replace "192.168.252.128" with your own server ip, and enter "yes" when confirming, and finally enter the password when you created the git user.

In order for the client to perform subsequent submission operations, we also need to indicate the user information of the current machine. The command is as follows:

git config --global user.email "[email protected]"git config --global user.name "Your Name"

After registration, this registration information will be used to record the operator's information when committing, and then the corresponding information can be seen when using git log, the effect is as follows:

$ git log
commit ae72bcc89ea8f5d9d3a44f0e00cf35e91a1afce8(HEAD -> master, origin/master)
Author: sylan215 <[email protected]>
Date:   Wed Oct 1818:37:412017+0800 test submission

At this point, we have completed the entire configuration process.

File modification and synchronization to Git server

After the configuration is complete, we enter the actual use link.

First, we modify a few files and copy them to the myfiles directory, then submit them to the server, and run the submit command under myfiles:

git add .
git commit -am "Test submission"git push

Command line content with output:

$ git add .
$ git commit -am "Test submission"[master(root-commit) ae72bcc] Test submission 1 file changed,1insertion(+)
create mode 100644 test.txt
$ git push
[email protected]'s password:
Counting objects:3, done.
Writing objects:100%(3/3),223 bytes |223.00 KiB/s, done.
Total 3(delta 0), reused 0(delta 0)
To 192.168.252.128:/srv/myfiles.git
*[ newbranch]      master -> master

The place where you are prompted to enter the password is the password of the git account you entered.

Note: For detailed commands for git operations, please refer to: http://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html

After the submission is successful, we use the command git pull on another machine to synchronize the latest content:

$ git pull
[email protected]'s password:
remote: Counting objects:3, done.
remote: Total 3(delta 0), reused 0(delta 0)
Unpacking objects:100%(3/3), done.
From 192.168.252.128:/srv/myfiles
 ae72bcc..afad897  master     -> origin/master
Updating ae72bcc..afad897
Fast-forward
test.txt |3++-1 file changed,2insertions(+),1deletion(-)

For complex operations of multiple users, please refer to: http://www.ruanyifeng.com/blog/2014/06/git_remote.html

Other configuration

Disable shell of git account

For security reasons, if you need to disable the shell of the git account, you can modify the /etc/passwd file:

Put in it

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

change into

git:x:1001:1001:git-user,,,:/home/git:/usr/bin/git-shell

The path of /usr/bin/git-shell can be obtained by the command which git-shell.

Use public key and private key to achieve secret-free effect

In the above process, we need to enter the password of the git user to interact with the server every time, which will be very troublesome. At this time, we can configure public and private keys to achieve encryption.

First, you need to generate public and private keys on the client:

ssh-keygen -t rsa

After you press Enter, you will be prompted to enter the private key password. If you want to avoid the secret, press Enter directly, otherwise you can customize a password (if you customize the password, you will fill in the set password every time you push and pull).

After the command is executed successfully, the files "id_rsa" and "id_rsa." will be generated in the .ssh folder of the current user directory (Windows directory is X:\Users{username}.ssh, Linux is /home/{username}/.ssh). pub", where the .pub file is the public key and the other is the private key.

Copy the file "id_rsa.pub" to the server, and use the following command to set:

mkdir /home/git/.ssh
cp /home/currentuser/Desktop/id_rsa.pub /home/git/.ssh/authorized_keys
chown -R git:git /home/git/.ssh

Note, if the authorized_keys file does not exist, you can use the cp command, otherwise, use the cat command to append, for example:

cat /home/currentuser/Desktop/id_rsa.pub >>/home/git/.ssh/authorized_keys

In order to ensure that the configuration takes effect, you also need to check whether the following settings in the /etc/ssh/sshd_config file are enabled:

AuthorizedKeysFile %h/.ssh/authorized_keys

Is it commented out? If so, you need to remove the # in front and restart the ssh service (command service ssh restart).

After all configuration is complete, we try the effect:

$ git pull
Already up-to-date.

Look, this time there is no prompt to enter the password, right? The password-free setting takes effect.

Non-22 port connection Git

The config file in the .ssh configuration directory now contains:

host ip address
port port name

The location of the config configuration file on Windows and Mac systems is: X:/users/username/.ssh directory, where X is the system disk and username is the current login user name;

If it is a liunx series system, the location is the /home/username/.ssh directory, where username is the current login user name.

Recommended Posts

Teach you how to build a Git server on Ubuntu
3 minutes to teach you to build gitea on Centos server
How to set up a DNS server on Ubuntu 18.04
How to install Git on Ubuntu 20.04
How to install Git on Ubuntu 20.04
Build a file server on ubuntu
How to build an NFS file sharing server on Ubuntu 16.04
How to build nfs service on ubuntu16.04
How to install Bacula Server on Ubuntu 14.04
How to use Samba server on Ubuntu 16.04
How to install Zabbix on Ubuntu 16.04 Server
How to set static IP on Ubuntu 18.04 Server
How to set static IP on Ubuntu 18.04 Server
How to quickly deploy docker on ubuntu server
How to use Jenkins to build automatically on Ubuntu
How to build a LAMP environment on centos7.2
How to run the parsing server on Ubuntu 14.04
How to install Git on Ubuntu 18.04 [Quick Start]
How to start a blog with Hexo on Ubuntu 14.04
How to configure a fixed IP based on Ubuntu 18.04
How to set a fixed IP based on Ubuntu 16.04
Teach you how to install Ubuntu system enhancement tools
How to open https on nginx server under Ubuntu
How to install Memcached on Ubuntu 20.04
Build Nginx-RTMP live server on ubuntu
How to install Java on Ubuntu 20.04
How to install MySQL on Ubuntu 20.04
How to install VirtualBox on Ubuntu 20.04
How to install Elasticsearch on Ubuntu 20.04
How to install Protobuf 3 on Ubuntu
How to install Nginx on Ubuntu 20.04
How to install Apache on Ubuntu 20.04
How to install MySQL on Ubuntu 20.04
How to install Git on CentOS 8
How to install Vagrant on Ubuntu 20.04
How to install Bacula-Web on Ubuntu 14.04
How to install PostgreSQL on Ubuntu 16.04
How to set up a Masterless Puppet environment on Ubuntu 14.04
How to install Anaconda3 on Ubuntu 18.04
How to install Memcached on Ubuntu 18.04
How to install Jenkins on Ubuntu 16.04
How to install MemSQL on Ubuntu 14.04
How to set up a firewall with UFW on Ubuntu 14.04
How to install Go on Ubuntu 20.04
How to install MongoDB on Ubuntu 16.04
How to install Mailpile on Ubuntu 14.04
How to install PrestaShop on Ubuntu 16.04
How to upgrade to PHP 7 on Ubuntu 14.04
How to install Skype on Ubuntu 20.04
How to install Jenkins on Ubuntu 20.04
How to install Python 3.8 on Ubuntu 18.04
How to install KVM on Ubuntu 18.04
How to install KVM on Ubuntu 20.04
How to install opencv3.0.0 on ubuntu14.04
How to install Anaconda on Ubuntu 20.04
How to install Prometheus on Ubuntu 16.04
How to install Jenkins on Ubuntu 18.04
How to deploy Django on Ubuntu 14.04
How to install Apache on Ubuntu 20.04
How to install R on Ubuntu 20.04
How to set up a production Elasticsearch cluster on Ubuntu 14.04