1. 程式人生 > >Centos 7.3搭建git服務器

Centos 7.3搭建git服務器

生成 mct cat script private scrip 不需要輸入密碼 cati air

服務器端:Centos 7.3環境搭建git服務器

客戶端:git客戶端可以是windows、linux和mac


1、git服務器和客戶端都安裝Git

[root@localhost ~]# yum install git


2、git服務器上創建一個git用戶組和用戶,用來運行git服務

[root@localhost ~]# groupadd git
[root@localhost ~]# useradd git -g git


3、創建證書登錄(如果用ssh key操作,要操作這步。如果用密碼登錄不需要操作這步)
收集所有需要登錄的客戶端的公鑰,公鑰位於id_rsa.pub文件中。ssh key可以讓客戶端與git服務器安全加密連接,而且不需要輸入密碼。


(1)客戶端生成公鑰和私鑰。

[root@localhost ~]# ssh-keygen -t rsa -C "[email protected]"
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
64:78:e9:5d:72:d0:d5:0c:51:f9:dc:25:ff:b5:5b:d9 [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
|          .. .+*o|
|       . . .. ..+|
|      . = . o  ++|
|       = . +    *|
|        S .     *|
|               oE|
|                o|
|               . |
|                 |
+-----------------+


(2)查看客戶端生成的公鑰。

[root@localhost ~]# cat ~/.ssh/id_rsa.pub


(3)git服務器上創建/home/git/.ssh/authorized_keys文件,並設置權限。

[root@localhost ~]# cd /home/git/
[root@localhost git]# mkdir .ssh
[root@localhost git]# chmod 700 .ssh
[root@localhost git]# chown -R git.git .ssh
[root@localhost git]# touch .ssh/authorized_keys
[root@localhost git]# chmod 600 .ssh/authorized_keys   (網上還有說法最好644)


(4)把客戶端公鑰內容復制到/home/git/.ssh/authorized_keys文件

(5)git服務器上修改ssh配置文件,將密碼驗證關掉,開啟ssh key驗證。

vi /etc/ssh/sshd_config
找到PasswordAuthentication節點並設置為no;
開啟RSA認證,將前面的#去掉,並確保如下配置:
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys


(6)git服務器上重啟SSH服務使配置生效:

[root@localhost git]# systemctl restart sshd
[root@localhost git]# service sshd restart


4、git服務器上初始化Git倉庫
首先我們選定一個目錄作為Git倉庫,比如是/home/gitrepo/runoob.git(叫這個名字,是因為參考完善別的文章):

[root@localhost git]# cd /home
[root@localhost home]# mkdir gitrepo
[root@localhost home]# chown git:git gitrepo/
[root@localhost home]# cd gitrepo
[root@localhost gitrepo]# git init --bare runoob.git
初始化空的 Git 版本庫於 /home/gitrepo/runoob.git/
[root@localhost gitrepo]# chown -R git:git runoob.git
備註:服務器上的Git倉庫名一般都以.git結尾。然後,把倉庫所屬用戶改為git:


5、客戶端操作,克隆倉庫

[root@localhost ~]# mkdir testdata
[root@localhost testdata]# git clone [email protected]:/home/gitrepo/runoob.git
Initialized empty Git repository in /root/testdata/runoob/.git/
warning: You appear to have cloned an empty repository.


6、客戶端操作,提交文件

[root@localhost testdata]# cd runoob/
[root@localhost runoob]# vi test.sh
[root@localhost runoob]# git add test.sh 
[root@localhost runoob]# git commit -m "測試"
[master (root-commit) ee961b2] 測試
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 test.sh
[root@localhost runoob]# git status
# On branch master
nothing to commit (working directory clean)
[root@localhost runoob]# git log
commit ee961b270d4541ff7440765a4c32d9ea722e3611
Author: gxm <[email protected]>
Date:   Sun May 22 09:02:40 2016 +0800
    測試
[root@localhost runoob]# git remote -v
origin    [email protected]:/home/gitrepo/runoob.git (fetch)
origin    [email protected]:/home/gitrepo/runoob.git (push)
[root@localhost runoob]# git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 216 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To [email protected]:/home/gitrepo/runoob.git
 * [new branch]      master -> master


7、git服務器上,可以查看objects這個時間知道是否提交了

[root@localhost runoob.git]# ll
總用量 12
drwxr-xr-x.  2 git git   6 9月  14 00:12 branches
-rw-r--r--.  1 git git  66 9月  14 00:12 config
-rw-r--r--.  1 git git  73 9月  14 00:12 description
-rw-r--r--.  1 git git  23 9月  14 00:12 HEAD
drwxr-xr-x.  2 git git 242 9月  14 00:12 hooks
drwxr-xr-x.  2 git git  21 9月  14 00:12 info
drwxr-xr-x. 10 git git  90 9月  14 00:55 objects
drwxr-xr-x.  4 git git  31 9月  14 00:12 refs

Centos 7.3搭建git服務器