天天看點

Ubuntu16 搭建Git 伺服器

剛入職一個月左右,昨晚公司給教育訓練了SVN和Git的使用。之前在研究所學生期間,和導師做項目就一直使用git做版本管理。想起研究所學生有一段時間,git總出錯(秘鑰都不好使了,隻有某個機器生成的rsa key 可以使用),我和亮哥、楊老師調了一下午,想起這段經曆,加上昨晚教育訓練的内容,讓我有種想搭建Git伺服器的沖動。于是。趁着今天周五,晚上有空,說幹就幹。

環境準備:虛拟機:VMware WorkStation 12 Player.(免費,自行下載下傳). 鏡像檔案:Ubuntu16

環境準備好之後,那就開始吧。

reference:http://www.linuxidc.com/Linux/2015-07/120616.htm

Ubuntu16 搭建Git 伺服器

一.在Ubuntu上安裝Git伺服器所需軟體

1.安裝git-core, openssh-server, openssh-client三個軟體。git-core是git的核心軟體; openssh-server、openssh-client是伺服器和用戶端傳輸檔案通過ssh協定。

安裝指令:”sudo apt-get install git-core openssh-server openssh-client”。

Ubuntu16 搭建Git 伺服器

注意:如果此步驟安裝失敗,可能是軟體庫過舊,使用“sudo apt-get update”指令,更新軟體庫。

2. 安裝python-setuptools軟體。該軟體是用來安裝gitosis的。

安裝指令:”sudo apt-get install python-setuptools”。

Ubuntu16 搭建Git 伺服器

3.安裝gitosis。Gitosis主要用于管理使用者對倉庫的操作權限。

Pre:安裝gitosis之前,需要初始化git伺服器使用者資訊。

輸入指令:“git config --global user.name ”charlie””;

“git config --global user.email ”[email protected]””。

Ubuntu16 搭建Git 伺服器

Next: 擷取gitosis版本檔案

輸入指令:“git clone https://github.com/res0nat0r/gitosis.git”。

Final: 安裝gitosis.

進入gitosis目錄,使用python指令安裝目錄下的setup.py的python腳本進行安裝。輸入指令:“sudo python setup.py install”。

Ubuntu16 搭建Git 伺服器

到這裡,伺服器上git所需軟體都已安裝完畢。

二.配置Git伺服器

1.建立git管理者賬戶

增加使用者指令:sudo useradd  -m  git

修改git使用者密碼:sudo passwd git 輸入密碼(123)

Ubuntu16 搭建Git 伺服器

2. 建立項目倉庫存儲點

在/home/目錄下建立一個目檔案夾作為倉庫sudo mkdir /home/gitrepository,

修改使用者組和權限

Ubuntu16 搭建Git 伺服器

由于gitosis預設狀态下會将倉庫放在使用者的repositories目錄下,例如git使用者的倉庫位址預設在/home/git/repositories/目錄下,這裡我們需要建立一個連結映射。讓他指向我們前面建立的專門用于存放項目的倉庫目錄/home/gitrepository。

Ubuntu16 搭建Git 伺服器

 3. 初始化gitosis

First: 在服務端生成SSH公鑰,輸入指令:“ssh-keygen -t rsa”,這裡會提示輸入密碼,我們不輸入直接回車即可。

Next: 用剛生成公鑰id_rsa.pub來對gitosis進行初始化。輸入指令:“sudo -H -u git gitosis-init <  /root/.ssh/id_rsa.pub”。

Ubuntu16 搭建Git 伺服器

出現如上資訊說明gitosis已經初始化成功。

gitosis主要是通過gitosis-admin.git倉庫來管理一些配置檔案的,如使用者權限的管理。這裡我們需要對其中的一個post-update檔案添加可執行的權限。

ubuntu:/home/git$ sudo chmod 755 /home/gitrepository/gitosis-admin.git/hooks/post-update

三.伺服器中建立項目倉庫

使用git賬戶在伺服器上建立一個目錄(mytestproject.git)并初始化成git項目倉庫。

ubuntu:/home/git$ su git

$ cd /home/gitrepository

$ mkdir mytestproject.git

$ cd mytestproject.git/

$ git init --bare

$ exit

Ubuntu16 搭建Git 伺服器

四.使用gitosis管理使用者操作項目的權限

1. 首先需要在前面生成ssh公鑰(用來初始化gitosis)的機器上将gitosis-admin.git的倉庫clone下來。

在用戶端機器上建立一個目錄用于存放gitosis-admin.git倉庫

ubuntu:~$ mkdir gitadmin

ubuntu:~$ cd gitadmin/

ubuntu:~/gitadmin$ git clone git@localhost:gitosis-admin.git

clone下來會有一個gitosis.conf的配置檔案和一個keydir的目錄。gitosis.conf用于配置使用者的權限資訊,keydir主要使用者存放ssh公鑰檔案(一般以“使用者名.pub”命名,gitosis.conf配置檔案中需使用相同使用者名),用于認證請求的用戶端機器。

現在讓需要授權的使用者使用前面的方式各自在其自己的機器上生成相應的ssh公鑰檔案,管理者把他們分别按使用者名命名好,複制到keydir目錄下。

繼續編輯gitosis.conf檔案

[gitosis]

[group gitosis-admin] 

####管理者組

members = charn@ubuntu 

####管理者使用者名,需要在keydir目錄下找到相應的.pub檔案,多個可用空格隔開(下同)

writable = gitosis-admin####可寫的項目倉庫名,多個可用空格隔開(下同)

[group testwrite] 

####可寫權限組

members = zhangsan####組使用者

writable = mytestproject####可寫的項目倉庫名

[group

 testread] ####隻讀權限組

members =lisi####組使用者

readonly= mytestproject####隻讀項目倉庫名

因為這些配置的修改隻是在本地修改的,還需要推送到伺服器中才能生效。

ubuntu:~/gitadmin/gitosis-admin$ git add .

ubuntu:~/gitadmin/gitosis-admin$ git commit -am "add a user permission"

ubuntu:~/gitadmin/gitosis-admin$ git push origin master

重新啟動一下sshd服務

ubuntu:~/gitadmin/gitosis-admin$ sudo /etc/init.d/ssh restart

現在,服務端的git就已經安裝和配置完成了,接下來就需要有權限的組成員在各自的機器上clone伺服器上的相應

項目倉庫進行相應的工作了。

五.用戶端使用Git

下載下傳安裝windows版本的git用戶端軟體,下載下傳位址:http://msysgit.github.io/

安裝完成後右鍵菜單會出現幾個git相關的菜單選項,我們主要使用其中的git

 bash通過指令行來進行操作。

在本地建立一個目錄,使用git初始化這個目錄,然後再裡面建立一個文本檔案用于測試,最後關聯到git伺服器倉庫

中的相關項目,最後上傳本地版本到伺服器。

$ mkdir testgit

$ cd testgit

$ git init

$ echo "this is a test text file,will push to server" > hello.txt

$ git add .

$ git commit -am "init a base version,add a first file for push to server"

$ git remote add origin git@serverip:mytestproject.git

$ git push origin master

這樣服務端就建立好了一個mytestproject.git的倉庫的基礎版本了,現在其他組員隻要從服務端進行clone就可以了。

window下面進入到需要克隆的本地目錄下面右鍵選擇git bash選項,輸入

$ git clone git@serverip:mytestproject.git

就可以把項目clone到本地倉庫了。

下面進行簡單的修改和送出操作

$ cd mytestproject

$ echo "this is another text file created by other" >another.txt

$ git commit -am "add a another file by other"

Ubuntu16 搭建Git 伺服器

六、錯誤清單

1. “remote: error: insufficient permission for adding an object to repository database ./objects ”

Ubuntu16 搭建Git 伺服器

解決方案:檢視原mytestproject.git目錄權限,發現mytestproject.git/object/沒有group寫權限導緻;執行 $ sudo chmod g+w -R mytestproject.git/object 後OK

繼續閱讀