天天看點

Centos 7.3搭建git伺服器

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

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

1、git伺服器和用戶端都安裝Git

1

<code>[root@localhost ~]</code><code># yum install git</code>

2、git伺服器上建立一個git使用者組和使用者,用來運作git服務

2

<code>[root@localhost ~]</code><code># groupadd git</code>

<code>[root@localhost ~]</code><code># useradd git -g git</code>

3、建立證書登入(如果用ssh key操作,要操作這步。如果用密碼登入不需要操作這步)

收集所有需要登入的用戶端的公鑰,公鑰位于id_rsa.pub檔案中。ssh key可以讓用戶端與git伺服器安全加密連接配接,而且不需要輸入密碼。

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

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

<code>[root@localhost ~]</code><code># ssh-keygen -t rsa -C "[email protected]"</code>

<code>Generating public</code><code>/private</code> <code>rsa key pair.</code>

<code>Enter </code><code>file</code> <code>in</code> <code>which</code> <code>to save the key (</code><code>/root/</code><code>.</code><code>ssh</code><code>/id_rsa</code><code>): </code>

<code>Created directory </code><code>'/root/.ssh'</code><code>.</code>

<code>Enter passphrase (empty </code><code>for</code> <code>no passphrase): </code>

<code>Enter same passphrase again: </code>

<code>Your identification has been saved </code><code>in</code> <code>/root/</code><code>.</code><code>ssh</code><code>/id_rsa</code><code>.</code>

<code>Your public key has been saved </code><code>in</code> <code>/root/</code><code>.</code><code>ssh</code><code>/id_rsa</code><code>.pub.</code>

<code>The key fingerprint is:</code>

<code>64:78:e9:5d:72:d0:d5:0c:51:f9:</code><code>dc</code><code>:25:ff:b5:5b:d9 [email protected]</code>

<code>The key's randomart image is:</code>

<code>+--[ RSA 2048]----+</code>

<code>|          .. .+*o|</code>

<code>|       . . .. ..+|</code>

<code>|      . = . o  ++|</code>

<code>|       = . +    *|</code>

<code>|        S .     *|</code>

<code>|               oE|</code>

<code>|                o|</code>

<code>|               . |</code>

<code>|                 |</code>

<code>+-----------------+</code>

(2)檢視用戶端生成的公鑰。

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

(3)git伺服器上建立/home/git/.ssh/authorized_keys檔案,并設定權限。

<code>[root@localhost ~]</code><code># cd /home/git/</code>

<code>[root@localhost git]</code><code># mkdir .ssh</code>

<code>[root@localhost git]</code><code># chmod 700 .ssh</code>

<code>[root@localhost git]</code><code># chown -R git.git .ssh</code>

<code>[root@localhost git]</code><code># touch .ssh/authorized_keys</code>

<code>[root@localhost git]</code><code># chmod 600 .ssh/authorized_keys   (網上還有說法最好644)</code>

(4)把用戶端公鑰内容複制到/home/git/.ssh/authorized_keys檔案

(5)git伺服器上修改ssh配置檔案,将密碼驗證關掉,開啟ssh key驗證。

<code>vi</code> <code>/etc/ssh/sshd_config</code>

<code>找到PasswordAuthentication節點并設定為no;</code>

<code>開啟RSA認證,将前面的</code><code>#去掉,并確定如下配置:</code>

<code>RSAAuthentication </code><code>yes</code>

<code>PubkeyAuthentication </code><code>yes</code>

<code>AuthorizedKeysFile .</code><code>ssh</code><code>/authorized_keys</code>

(6)git伺服器上重新開機SSH服務使配置生效:

<code>[root@localhost git]</code><code># systemctl restart sshd</code>

<code>[root@localhost git]</code><code># service sshd restart</code>

4、git伺服器上初始化Git倉庫

首先我們標明一個目錄作為Git倉庫,比如是/home/gitrepo/runoob.git(叫這個名字,是因為參考完善别的文章):

<code>[root@localhost git]</code><code># cd /home</code>

<code>[root@localhost home]</code><code># mkdir gitrepo</code>

<code>[root@localhost home]</code><code># chown git:git gitrepo/</code>

<code>[root@localhost home]</code><code># cd gitrepo</code>

<code>[root@localhost gitrepo]</code><code># git init --bare runoob.git</code>

<code>初始化空的 Git 版本庫于 </code><code>/home/gitrepo/runoob</code><code>.git/</code>

<code>[root@localhost gitrepo]</code><code># chown -R git:git runoob.git</code>

<code>備注:伺服器上的Git倉庫名一般都以.git結尾。然後,把倉庫所屬使用者改為git:</code>

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

<code>[root@localhost ~]</code><code># mkdir testdata</code>

<code>[root@localhost testdata]</code><code># git clone [email protected]:/home/gitrepo/runoob.git</code>

<code>Initialized empty Git repository </code><code>in</code> <code>/root/testdata/runoob/</code><code>.git/</code>

<code>warning: You appear to have cloned an empty repository.</code>

6、用戶端操作,送出檔案

23

24

<code>[root@localhost testdata]</code><code># cd runoob/</code>

<code>[root@localhost runoob]</code><code># vi test.sh</code>

<code>[root@localhost runoob]</code><code># git add test.sh </code>

<code>[root@localhost runoob]</code><code># git commit -m "測試"</code>

<code>[master (root-commit) ee961b2] 測試</code>

<code> </code><code>1 files changed, 1 insertions(+), 0 deletions(-)</code>

<code> </code><code>create mode 100644 </code><code>test</code><code>.sh</code>

<code>[root@localhost runoob]</code><code># git status</code>

<code># On branch master</code>

<code>nothing to commit (working directory clean)</code>

<code>[root@localhost runoob]</code><code># git log</code>

<code>commit ee961b270d4541ff7440765a4c32d9ea722e3611</code>

<code>Author: gxm &lt;gxm@</code><code>test</code><code>.com&gt;</code>

<code>Date:   Sun May 22 09:02:40 2016 +0800</code>

<code>    </code><code>測試</code>

<code>[root@localhost runoob]</code><code># git remote -v</code>

<code>origin    [email protected]:</code><code>/home/gitrepo/runoob</code><code>.git (fetch)</code>

<code>origin    [email protected]:</code><code>/home/gitrepo/runoob</code><code>.git (push)</code>

<code>[root@localhost runoob]</code><code># git push origin master</code>

<code>Counting objects: 3, </code><code>done</code><code>.</code>

<code>Writing objects: 100% (3</code><code>/3</code><code>), 216 bytes, </code><code>done</code><code>.</code>

<code>Total 3 (delta 0), reused 0 (delta 0)</code>

<code>To [email protected]:</code><code>/home/gitrepo/runoob</code><code>.git</code>

<code> </code><code>* [new branch]      master -&gt; master</code>

7、git伺服器上,可以檢視objects這個時間知道是否送出了

<code>[root@localhost runoob.git]</code><code># ll</code>

<code>總用量 12</code>

<code>drwxr-xr-x.  2 git git   6 9月  14 00:12 branches</code>

<code>-rw-r--r--.  1 git git  66 9月  14 00:12 config</code>

<code>-rw-r--r--.  1 git git  73 9月  14 00:12 description</code>

<code>-rw-r--r--.  1 git git  23 9月  14 00:12 HEAD</code>

<code>drwxr-xr-x.  2 git git 242 9月  14 00:12 hooks</code>

<code>drwxr-xr-x.  2 git git  21 9月  14 00:12 info</code>

<code>drwxr-xr-x. 10 git git  90 9月  14 00:55 objects</code>

<code>drwxr-xr-x.  4 git git  31 9月  14 00:12 refs</code>

本文轉自 sailikung 51CTO部落格,原文連結:http://blog.51cto.com/net881004/2068517,如需轉載請自行聯系原作者

繼續閱讀