Build your own git server under Ubuntu and centos

Use the remote repository Github, Github public project is free, but if you do not want others to see your project will need to be charged. I do not want to pay, then we need to build their own Git server as a private warehouse.


  • Git server set up under Ubuntu

  1. View git version, Ubuntu and general centos installation of the system will be automatically installed
 $ git --version  
git version 1.7.1 
  1. If not, use the command sudo apt-get install git installation
 $ sudo apt-get install git
  1. Our best to configure a dedicated git user and set a password, a special code management and service management git
  sudo useradd -m git  
 sudo passwd git(change to yours)  

Note: The above command generates a user name and password are both git account, you can also set your password, there must first set up, otherwise there will be problems later, (if there is no password, no ssh-key when it can not be verified, you can not clone) can also create your own user name as long as other related operations (such as clone, push, pull) to specify when user name to (this article all to git, for example), - m option is allowed in the home generate the user's home folder directory, our code repository will be arranged in the main folder.

  1. Then, create a folder of git repositories as new git repository in the user's home directory, and equipped with the most basic security for the warehouse - access control
  sudo mkdir /home/git/repositories # 最好使用repositories作为文件夹名称,这样可以简化后面的操作  
sudo chown git:git /home/git/repositories  
sudo chmod 755 /home/git/repositories  
  1. Thus, a simple git server already be in place, you can create yourself a simple test project. In order to facilitate following operations, we switch to the first user git
  su git
  1. In respositories a new directory helloworld (warehouse), and switch to the directory, where initialize the empty warehouse
 mkdir helloworld  
cd helloworld 
git --bare init  

It is noted here next , we initialize the warehouse, the best use git -bare init
this command can save a lot of trouble, but do not use: git init on the difference between these two commands I look at another blog : http://blog.csdn.net/qq_29232943/article/details/60971061

If you use git init initialization, the directory also includes a remote repository work tree.
When the local repository push to a remote repository, if the remote repository is push a branch (if not then push the branch, there is no problem), then the result will not push the reaction in the work tree, ie directory on a remote repository under the corresponding file or previous content.
Solution: the server must run the command: git reset -hard to see the contents of the push.

Problem 2
Git: the Push wrong solution master -> master (branch is currently checked out)

This is because the default rejection git push operation needs to be set, modify the server .git / config add the following code:

[receive]
denyCurrentBranch = ignore

ok, an empty warehouse establishment of good, git machine with another one installed (such as your development machine windows) test, to see you git IP address of the server through the ifconfig -a command, to distinguish your IP address, your server scored a wired network, the IP address for the eth0 of inter, if the wireless network is the wlan0 inet address, IP server is assumed here that you used above is 192.168.8.34

 $ git clone git@192.168.8.34:/home/git/repositories/helloworld
Cloning into 'helloworld'...
The authenticity of host '192.168.8.34 (192.168.8.34)' can't be established.
RSA key fingerprint is 2b:55:45:e7:4c:29:cc:05:33:78:03:bd:a8:cd:08:9d.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.8.34' (RSA) to the list of known hosts.
[email protected]'s password:  

Description: Here are two things to note:
First , when you first use the clone Git commands or push connection GitHub, will get a warning:

The authenticity of host 'github.com (xx.xx.xx.xx)' can't be established.
RSA key fingerprint is xx.xx.xx.xx.xx.
Are you sure you want to continue connecting (yes/no)?

This is because Git uses SSH connection, and when the first SSH connection verification Key GitHub server, you need to confirm whether the fingerprint information Key's GitHub GitHub really from the server (that is, your ssh-key, the public key to upload to the server, the private key locally, when the first clone to verify this information, after verification successful, will generate a known_hosts file, which is your connection information, the future will not verified), enter yes press enter . Git will output a warning, telling you have to add GitHub is Key to a trusted list of this machine in the:

Warning: Permanently added 'github.com' (RSA) to the list of known hosts.

This warning will only appear once, the latter operation will not have any warning.
If you are really worried about people posing as GitHub server, before you can enter yes consistent control of GitHub RSA Key fingerprint information is given in connection with the SSH.

Second , this prompts you to enter a password to clone, of course, if you know the password, you can type a password to clone, but a more common way is to use SSH public key to complete the verification.


Some basic operational test on a development machine: for this project add some files, then commit, then push, if in addition to several input git user's password, all other normal, then explain the above installation and configuration have been a success .

In the above testing, is not found in the clone of a long path, it is easy to mistake? Several steps are required to enter the user's password git (clone, push), it is not annoying , but according to the rhythm of development for each additional member, you have to tell him git user's password is not cumbersome and unsafe?
So, if you can clone a specified time as long as the project name, clone / push / pull automate authentication, and it is best to give different users different permissions to different warehouses, so much the better! The following configuration can handle all of this, let's git repository management easier, more convenient!

First, the authentication problem, we have already mentioned git data exchange and operation are based on ssh, so naturally we authentication can be solved by configuring ssh . ssh authentication is managed by key, which includes a public key (to the server) and a private key (own retention), each corresponding to the public key of a private key, only the private key corresponding to each of a public key. . . . Some words will be omitted here. . . . So a simple way to solve the authentication is automatic: each person needs access to the code repository, generate their own public and private keys via ssh-keygen on your machine, the public key to the server, the server administrator Add the user's public key to the server to change the user's git .ssh / authorized_keys file

Install openssh Service


Command to install SSH Server

sudo apt-get install openssh-server

By command, see ssh service is started.

ps –e|grep ssh

Starting the SSH service.
Enter the command: restart the SSH service
service sshd restart sshd unrecognized service

ssh service 在ubantu叫 ssh  
所以重启ssh  
sudo service ssh restart  

It is on centos sshd

service sshd restart 

Command: service sshd start to start the service | command: service sshd stop to stop the service
after reboot enter: netstat -antp | grep sshd start to see if 22-port (can be omitted).
How to set up SSH service to boot?
◆ enter the command: chkconfig sshd on to.
Note: If the chkconfig sshd off is prohibited SSH boot.

- create a framework / .ssh, and to set the appropriate permissions. This is important if permission is set too open to make themselves protected by SSH does not make sense.

$ su - git
$ mkdir .ssh && chmod 700 .ssh 
$ touch .ssh/authorized_keys 
$ chmod 600 .ssh/authorized_keys 

authorized_keys file contains all of your developers of SSH public key, you can open permissions allow them to work in your Git project. They have to create their own SSH key pair and their public key to you. Copy the public key to the authorized_keys file in the user gituser

Create your own clients in the local SSH Key

First, in the user's home directory to see if there .ssh directory, if there is, and then see if there id_rsa and id_rsa.pub these two files in this directory, if you already have, you can jump directly to the next step. If not, open the Shell (open Git Bash under Windows)
set up a local user to configure git

$ git config --global user.name "username"
$ git config --global user.email "[email protected]"
$ ssh-keygen -t rsa -C "[email protected]"  

You need to e-mail addresses into your own e-mail address, and then all the way round, use the default value, because of this Key is not used for military purposes, so there is no need to set a password.
If all goes well, can be found in the user's home directory .ssh directory, there are two id_rsa and id_rsa.pub file, both SSH Key is secret key pair, id_rsa private key, can not leak out, id_rsa.pub is the public key, can safely tell anyone.
Description : View git all configuration items

$ git config -l  

Git server opens RSA authentication
and then you can go to add your public key to verify the information on your Git server. First need to / etc / ssh / sshd_config will open on the RSA authentication server Git, namely:

sudo vim /etc/ssh/sshd_config
RSAAuthentication yes     
PubkeyAuthentication yes     
AuthorizedKeysFile  .ssh/authorized_keys

Just copy the generated public key in, there'll be a member to join in, to be in authorized_keys in additional, added then clone again when or after the push when you do not need to enter the password:

Zhu@XXX/E/testgit/8.34
$ git clone git@192.168.8.34:/home/git/repositories/helloworld
Cloning into 'helloworld'...
warning: You appear to have cloned an empty repository.
Checking connectivity... done.

Disable git user's login shell
for security reasons, the second step is to create a git user is not allowed to log shell, which can be done by editing the / etc / passwd file. Find a line like the following:

sudo vim /etc/passwd
git:x:1001:1001:,,,:/home/git:/bin/bash  

Finally, after a colon read:

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

In this way, git users can normally use through ssh git, but you can not login shell, because we specified for the git git-shell every time a user login automatically exit.

Management and public authority
if the team is small, put everyone into the server's public key collected /home/git/.ssh/authorized_keys file is feasible. If the team has several hundred people, so would not be able to play, then you can use to manage public and Gitlab rights, Gitlab similar Github


Install git server under centos


Note that the command under Ubuntu and similar differences
yum install git
update git version under CentOS http://www.cnblogs.com/boxuan/articles/6434109.html


Thanks to the following links Help
Reference: http://freeloda.blog.51cto.com/2033581/1410562
Reference: http://blog.csdn.net/xsl1990/article/details/25486211
Reference: HTTP: //blog.csdn .net / wave_1102 / article / details / 47779401

Published 175 original articles · won praise 76 · Views 230,000 +

Guess you like

Origin blog.csdn.net/qq_29232943/article/details/56835554