(十九)git版本管理軟件——搭建git服務器

建立管理員git

以root用戶的形式建立一個專門管理git服務器的管理員git

建立管理員gitgithub

root@ghost-machine:/home/ghost# adduser git
正在添加用戶"git"...
正在添加新組"git" (1001)...
正在添加新用戶"git" (1001) 到組"git"...
建立主目錄"/home/git"...
正在從"/etc/skel"複製文件...
輸入新的 UNIX 密碼: 
從新輸入新的 UNIX 密碼: 
passwd:已成功更新密碼
正在改變 git 的用戶信息
請輸入新值,或直接敲回車鍵以使用默認值
    全名 []: 
    房間號碼 []: 
    工做電話 []: 
    家庭電話 []: 
    其它 []: 
這些信息是否正確? [Y/n] y
root@ghost-machine:/home/ghost#

Ubuntu會在建立的時候設置密碼,若其餘系統沒有提示設置密碼,須要手動設置git管理員密碼
命令:sudo passwd gitvim

有些網站建議關閉git管理員的ssh訪問,但目前不作這一步。bash

爲管理員用戶添加sudo權限
編輯文件/etc/sudoers,該文件要更變成可寫模式服務器

root@ghost-machine:~# ll /etc/sudoers
-r--r----- 1 root root 755 5月  29  2017 /etc/sudoers
root@ghost-machine:~# chmod +w /etc/sudoers
root@ghost-machine:~# vim /etc/sudoers
root@ghost-machine:~# chmod -w /etc/sudoers
root@ghost-machine:~# ll /etc/sudoers
-r--r----- 1 root root 778 12月  9 10:20 /etc/sudoers
root@ghost-machine:~#

 編輯內容,在root後面加上gitapp

# User privilege specification
root    ALL=(ALL:ALL) ALL
git     ALL=(ALL:ALL) ALL

 

生成管理員祕鑰

切換到git用戶並跳轉到本身的工做目錄下dom

root@ghost-machine:/home/ghost# su git
git@ghost-machine:/home/ghost$ cd
git@ghost-machine:~$

生成git管理員祕鑰,輸入的是用戶名和郵箱的組合  ssh

git@ghost-machine:~$ ssh-keygen -t rsa -C "git@email.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/git/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/git/.ssh/id_rsa.
Your public key has been saved in /home/git/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:FxzVolVlTN5iK9XATCVHADyt4mHkN+zR036f+oB91XI git@email.com
The key's randomart image is:
+---[RSA 2048]----+
|          .ooO*BB|
|         ...= =Bo|
|         oo+ =+.+|
|          *.*oooo|
|        So.=.oooE|
|         .. +. o+|
|           . o .+|
|              o..|
|             .o. |
+----[SHA256]-----+
git@ghost-machine:~$

設置管理員git提交帳號和郵箱

git@ghost-machine:~$ git config --global user.name "git"
git@ghost-machine:~$ git config --global user.email  "git@email.com"

下載安裝gitolite

下載地址:下載路徑最好放在/home/git/下ide

命令:git clone git://github.com/sitaramc/gitolite網站

git@ghost-machine:~$ git clone git://github.com/sitaramc/gitolite
正克隆到 'gitolite'...
remote: Counting objects: 9509, done.
remote: Compressing objects: 100% (12/12), done.
remote: Total 9509 (delta 4), reused 5 (delta 2), pack-reused 9495
接收對象中: 100% (9509/9509), 3.00 MiB | 587.00 KiB/s, 完成.
處理 delta 中: 100% (5881/5881), 完成.
檢查鏈接... 完成。
git@ghost-machine:~$

手動下源碼安裝保證gitolite的控制權在git管理員手上。

將命令加入系統環境變量中。 
sudo vi /etc/profile

export PATH=$PATH:$HOME/bin

啓動gitolite

將管理員生成的sskkey加載在軟件中

git@ghost-machine:~$ gitolite setup -pk ~/.ssh/id_rsa.pub
初始化空的 Git 倉庫於 /home/git/repositories/gitolite-admin.git/
初始化空的 Git 倉庫於 /home/git/repositories/testing.git/
WARNING: /home/git/.ssh/authorized_keys missing; creating a new one
    (this is normal on a brand new install)
git@ghost-machine:~$

gitolite的管理都圍繞gitolite-admin.git這個版本庫進行。管理服務器的項目git版本庫,用戶的添加刪除,項目git版本庫的受權。

爲統一管理服務器版本庫,後續添加git版本庫都統一交給gitolite-admin.git進行統一管理。

gitoltie會用相對路徑去下載版本庫。

管理員git首先要下載gitolite-admin.git,下載須要輸入管理員sshkey的密碼。

git@ghost-machine:~$ git clone git@localhost:gitolite-admin.git
正克隆到 'gitolite-admin'...
The authenticity of host 'localhost (127.0.0.1)' can't be established.
ECDSA key fingerprint is
SHA256:ifKZZ/A2lLcOGPCOaQIOTVvvD+fgAgGiB2WJN4R00Xw.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'localhost' (ECDSA) to the list of known hosts.
Enter passphrase for key '/home/git/.ssh/id_rsa': 
remote: 對象計數中: 6, 完成.
remote: 壓縮對象中: 100% (4/4), 完成.
remote: Total 6 (delta 0), reused 0 (delta 0)
接收對象中: 100% (6/6), 完成.
檢查鏈接... 完成。
git@ghost-machine:~$

添加項目版本庫

進入下載好的gitolite-admin版本庫中,編輯文件gitolite.conf

命令:vim conf/gitolite.conf

添加的項目名爲:zx_git_repo

git@ghost-machine:~/gitolite-admin$ git diff
diff --git a/conf/gitolite.conf b/conf/gitolite.conf
index 8eb9fbb..64e882d 100644
--- a/conf/gitolite.conf
+++ b/conf/gitolite.conf
@@ -3,3 +3,6 @@ repo gitolite-admin

 repo testing
     RW+     =   @all
+
+repo zx_git_repo
+    RW+     =   id_rsa
git@ghost-machine:~/gitolite-admin$

 項目設置爲git管理員能夠管理,git管理員對應名稱id_rsa 
在文件keydir會自動建立加載的管理員公鑰 

git@ghost-machine:~/gitolite-admin$ ls keydir/
id_rsa.pub

將修改push到庫中  

git@ghost-machine:~/gitolite-admin$ git add .
git@ghost-machine:~/gitolite-admin$ git commit -m "add repo zx_git_repo."
[master 5d8b89b] add repo zx_git_repo.
 1 file changed, 3 insertions(+)
git@ghost-machine:~/gitolite-admin$ git push origin master 
Enter passphrase for key '/home/git/.ssh/id_rsa': 
對象計數中: 4, 完成.
Delta compression using up to 4 threads.
壓縮對象中: 100% (3/3), 完成.
寫入對象中: 100% (4/4), 382 bytes | 0 bytes/s, 完成.
Total 4 (delta 0), reused 0 (delta 0)
remote: 初始化空的 Git 倉庫於 /home/git/repositories/zx_git_repo.git/
To git@localhost:gitolite-admin.git
   6c9b6a3..5d8b89b  master -> master
git@ghost-machine:~/gitolite-admin$

隨着gitolite-admin的更改,gitolite會在版本庫中初始化添加的庫,添加的版本庫就在/home/git/repositories/zx_git_repo.git/下

添加項目成員
項目須要添加成員,成員經過郵件或其餘通訊方式發送本身sshkey公鑰給git管理員,將id_rsa.pub修改爲本身的名字以便識別。

管理員拿到新成員的公鑰後將其複製到keydir目錄下

git@ghost-machine:~/gitolite-admin$ cp /mnt/hgfs/share/user_zx.pub keydir/
git@ghost-machine:~/gitolite-admin$ ls keydir/
id_rsa.pub  user_zx.pub

編輯文件gitolite.conf

git@ghost-machine:~/gitolite-admin$ git diff
diff --git a/conf/gitolite.conf b/conf/gitolite.conf
index 64e882d..b173b92 100644
--- a/conf/gitolite.conf
+++ b/conf/gitolite.conf
@@ -1,3 +1,6 @@
+@admin = id_rsa
+@user = user_zx
+
 repo gitolite-admin
     RW+     =   id_rsa

@@ -5,4 +8,5 @@ repo testing
     RW+     =   @all

 repo zx_git_repo
-    RW+     =   id_rsa
+    RW+     =   @admin
+    RW+     =   @user
git@ghost-machine:~/gitolite-admin$

這裏將用戶分組,分紅admin和user,如有多個用戶,用空格隔開

將修改push到庫中

git@ghost-machine:~/gitolite-admin$ git add .
git@ghost-machine:~/gitolite-admin$ git status
位於分支 master
您的分支與上游分支 'origin/master' 一致。
要提交的變動:
  (使用 "git reset HEAD <文件>..." 以取消暫存)

    修改:     conf/gitolite.conf
    新文件:   keydir/user_zx.pub

git@ghost-machine:~/gitolite-admin$ git commit -m "add user user_zx to zx_git_pro."
[master 07faacc] add user user_zx to zx_git_pro.
 2 files changed, 6 insertions(+), 1 deletion(-)
 create mode 100755 keydir/user_zx.pub
git@ghost-machine:~/gitolite-admin$ git push origin master 
Enter passphrase for key '/home/git/.ssh/id_rsa': 
對象計數中: 6, 完成.
Delta compression using up to 4 threads.
壓縮對象中: 100% (5/5), 完成.
寫入對象中: 100% (6/6), 816 bytes | 0 bytes/s, 完成.
Total 6 (delta 1), reused 0 (delta 0)
To git@localhost:gitolite-admin.git
   5d8b89b..07faacc  master -> master
git@ghost-machine:~/gitolite-admin$

 

項目成員下載項目

添加成功成員後,發送git項目的下載地址給對應成員,便可下載項目

項目地址:git@192.168.215.132:zx_git_repo.git

$ git clone git@192.168.215.132:zx_git_repo.git
Cloning into 'zx_git_repo'...
Enter passphrase for key '/c/Users/zx_work/.ssh/id_rsa':
warning: You appear to have cloned an empty repository.
Checking connectivity... done.

gitolite將下載路徑變成相對地址訪問,若不用gitolite管理,須要輸入絕對地址而且輸入git管理員的密碼。 
使用了gitolite管理以後,只須要輸入受權成員的sskey便可。

推送代碼

$ git push  origin master
Enter passphrase for key '/c/Users/zx_work/.ssh/id_rsa':
Counting objects: 4, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 328 bytes | 0 bytes/s, done.
Total 4 (delta 0), reused 0 (delta 0)
To git@192.168.215.132:zx_git_repo.git
 * [new branch]      master -> master

經過git管理員能夠查看到該用戶可以正常提交代碼  

git@ghost-machine:~/repositories/zx_git_repo.git$ git log
commit 541c1a53aa884a69d41b9e70b06bdeb7756ed747
Author: zxng <zxngyulin@163.com>
Date:   Sat Dec 9 12:21:21 2017 +0800

    add README.md
git@ghost-machine:~/repositories/zx_git_repo.git$
相關文章
相關標籤/搜索