很多時候考慮到企業(yè)項目可維護性,大家還是希望擁有自己的代碼版本庫甫恩,github逆济、gitee都是不錯的選擇,但是完全依賴于這些平臺填物,對代碼安全性總有疑慮纹腌,也不利于自己團隊在版本管理上的成長霎终。gitee企業(yè)版今天也出了點小問題滞磺,登錄后總提示500錯誤。于是考慮了落實自建git服務(wù)器的計劃莱褒。
Git服務(wù)器搭建過程
參考文章:
Aaron的個人技術(shù)博客 CentOS7搭建git 服務(wù)器击困。
正如Aaron所說,這個方案適合于自己開發(fā)广凸,自己使用阅茶,遇到團隊作戰(zhàn)時,管理起來就不那么方便靈活了谅海。但是熟悉Git服務(wù)器搭建過程脸哀,是繼續(xù)團戰(zhàn)的基礎(chǔ)。本文會根據(jù)Aaron和Git中文網(wǎng)的資料扭吁,進行團戰(zhàn)搭建的介紹撞蜂。
本文是實踐總結(jié)盲镶,主要記錄按照上述文章實際搭建中遇到的問題。
- yum 安裝 git
# yum install git
- 備注:網(wǎng)上有很多從源碼安裝的例子蝌诡,我實操下來感覺不是很有必要溉贿,yum庫安裝速度快,還可以避免一些目錄和權(quán)限的問題浦旱,所以推薦直接yum 安裝 git宇色。
- 確認git版本
# git --version
git version 1.8.3.1
- 備注:CentOS7 自帶了1.8.3.1
- 添加git用戶組和git用戶
# groupadd git
# adduser git -g git
# passwd
- 備注1:原文只使用 adduser git,但實際上他也是創(chuàng)建了git組的颁湖,我還是老老實實用了上面這串命令
- 備注2:passwd執(zhí)行后宣蠕,要求輸入密碼,就是給git賬號設(shè)置密碼
- 創(chuàng)建git工作目錄
# mkdir -p /home/git/.ssh
# chmod 700 /home/git/.ssh
# chown -R git:git /home/git
- 備注:加上-p支持 mkdir創(chuàng)建多級目錄
- 創(chuàng)建用戶公鑰保存的文件
# touch /home/git/.ssh/authorized_keys
# vi /home/git/.ssh/authorized_keys
- 備注1:添加用戶公鑰時爷狈,要一行一個植影。
- 備注2:windows下獲取ssh公鑰的方法,可在git客戶端的git bash下執(zhí)行如下的命令
# ssh-keygen
- 備注3:上面的命令可以一路yes涎永,一路默認思币,不輸入密碼。最終在windows的用戶目錄下的.ssh目錄下羡微,可以找到id_rsa.pub谷饿,把內(nèi)容copy出來,一行一個用戶的粘貼到上面的文件中即可妈倔。
- 創(chuàng)建測試代碼庫
# cd /home
# git init --bare test.git
Initialized empty Git repository in /home/test.git/
# chown -R git:git test.git
# ll
total 0
drwxr-xr-x 7 git git 119 Jan 27 22:44 test.git
- 設(shè)置git用戶只能登錄執(zhí)行g(shù)it-shell博投,且每次都要登錄
# vi /etc/passwd
找到git:x開頭的那行改為
git:x:1001:1001:,,,:/home/git:/usr/bin/git-shell
- 備注:上面這里的/usr/bin/git-shell是你的git-shell所在的目錄,如果用yum安裝git盯蝴,應(yīng)該就是在這個目錄毅哗,如需要確認,可執(zhí)行下面的命令查找
# find / | grep git-shell
find: ‘/proc/1831’: No such file or directory
/usr/bin/git-shell
/usr/share/doc/git-1.8.3.1/contrib/git-shell-commands
- 本地電腦遠程克隆test.git測試
這里給出兩個方案:
方案1: 如果你的ssh端口號沒有變動捧挺,可能下面的命令已經(jīng)可以執(zhí)行了虑绵,我自己的服務(wù)器是改了ssh端口號的,所以我不知道下面的命令是否可以正常執(zhí)行
# git clone ssh://git@服務(wù)器IP地址:/home/git/test.git
Cloning into 'test'...
warning: You appear to have cloned an empty repository.
方案2: 如果你的ssh端口號變了闽烙,要按照下面的命令來執(zhí)行翅睛,才能成功。這也是我一開始沒搞定的原因黑竞,特此記錄捕发。
# git clone ssh://git@服務(wù)器IP地址:ssh端口/home/git/test.git
Cloning into 'test'...
The authenticity of host '[服務(wù)器IP]:ssh端口 ([服務(wù)器IP]:ssh端口)' can't be established.
ECDSA key fingerprint is SHA256:************************************************.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[服務(wù)器IP]:ssh端口' (ECDSA) to the list of known hosts.
warning: You appear to have cloned an empty repository.
- 備注:至此,git服務(wù)器搭建完成了很魂,我們還要把自己的git庫傳到服務(wù)器上去扎酷,這個問題等一會兒會講。
增加Gitosis管理遏匆,方便團隊化作戰(zhàn)
安裝Gitosis
恭喜你法挨,經(jīng)過剛才的實踐骤铃,你已經(jīng)掌握了Git服務(wù)器的搭建方法。上面的方案有三個問題:
- git clone 的遠程git路徑坷剧,是全路徑惰爬,相對來說記憶比較困難。
- 用戶的添加是通過修改/home/git/.ssh/authorized_keys惫企,人員增加后維護起來就比較麻煩撕瞧。
- 這個問題應(yīng)該比較關(guān)鍵。 git倉庫無法進行多項目的權(quán)限控制狞尔,一旦增加人員丛版,就自動獲取了全部項目的代碼訪問權(quán)限。
為了解決上述問題偏序,Gitosis應(yīng)運而生了页畦。
Gitosis 主要解決兩個問題:
- 相對路徑問題
- 項目的分組管理和人員授權(quán)訪問問題。
還是先進行安裝實踐研儒,我會結(jié)合實際情況豫缨,介紹解決遇到的一些問題。
- 安裝python依賴
Gitosis 的工作依賴于某些 Python 工具端朵,所以首先要安裝 Python 的 setuptools 包
# yum install python-setuptools
1.1 (補充) 執(zhí)行上面的命令時好芭,在一個新的阿里云服務(wù)器上出了問題
Installed /usr/lib/python2.7/site-packages/gitosis-0.2-py2.7.egg
Traceback (most recent call last):
File "setup.py", line 64, in <module>
'setuptools>=0.6c5',
File "/usr/lib64/python2.7/distutils/core.py", line 152, in setup
dist.run_commands()
File "/usr/lib64/python2.7/distutils/dist.py", line 953, in run_commands
self.run_command(cmd)
File "/usr/lib64/python2.7/distutils/dist.py", line 972, in run_command
cmd_obj.run()
File "/usr/lib/python2.7/site-packages/setuptools/command/install.py", line 73, in run
self.do_egg_install()
File "/usr/lib/python2.7/site-packages/setuptools/command/install.py", line 101, in do_egg_install
cmd.run()
File "/usr/lib/python2.7/site-packages/setuptools/command/easy_install.py", line 380, in run
self.easy_install(spec, not self.no_deps)
File "/usr/lib/python2.7/site-packages/setuptools/command/easy_install.py", line 604, in easy_install
return self.install_item(None, spec, tmpdir, deps, True)
File "/usr/lib/python2.7/site-packages/setuptools/command/easy_install.py", line 655, in install_item
self.process_distribution(spec, dist, deps)
File "/usr/lib/python2.7/site-packages/setuptools/command/easy_install.py", line 701, in process_distribution
distreq.project_name, distreq.specs, requirement.extras
TypeError: __init__() takes exactly 2 arguments (4 given)
搜索后確認,是yum 安裝的setuptools 版本不對
# ll /usr/lib/python2.7/site-packages/
drwxr-xr-x 5 root root 4096 Mar 4 23:29 setuptools
drwxr-xr-x 2 root root 4096 Mar 4 23:29 setuptools-0.9.8-py2.7.egg-info
-rw-r--r-- 1 root root 793874 Mar 5 00:10 setuptools-33.1.1-py2.7.egg
drwxr-xr-x 2 root root 4096 Nov 29 11:40 setuptools-36.4.0.dist-info
-rw-r--r-- 1 root root 30 Mar 5 00:10 setuptools.pth
網(wǎng)上的解決方案是 刪除 該目錄下 所有的 setuptools文件夾冲呢,然后再重新安裝
# yum remove python-setuptools
# rm /usr/lib/python2.7/site-packages/setuptool* -rf
用pip重新安裝 setuptools
# wget https://bootstrap.pypa.io/ez_setup.py
--2019-03-05 00:09:44-- https://bootstrap.pypa.io/ez_setup.py
Resolving bootstrap.pypa.io (bootstrap.pypa.io)... 151.101.228.175, 2a04:4e42:36::175
Connecting to bootstrap.pypa.io (bootstrap.pypa.io)|151.101.228.175|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 12537 (12K) [text/x-python]
Saving to: ‘ez_setup.py’
100%[=============================================================================================================>] 12,537 --.-K/s in 0.07s
2019-03-05 00:09:45 (177 KB/s) - ‘ez_setup.py’ saved [12537/12537]
# python ez_setup.py
ez_setup.py is deprecated and when using it setuptools will be pinned to 33.1.1 since it's the last version that supports setuptools self upgrade/installation, check https://github.com/pypa/setuptools/issues/581 for more info; use pip to install setuptools
Downloading https://pypi.io/packages/source/s/setuptools/setuptools-33.1.1.zip
Extracting in /tmp/tmpi_jCtH
Now working in /tmp/tmpi_jCtH/setuptools-33.1.1
...
Installed /usr/lib/python2.7/site-packages/setuptools-33.1.1-py2.7.egg
Processing dependencies for setuptools==33.1.1
Finished processing dependencies for setuptools==33.1.1
# easy_install-2.7 pip
Searching for pip
Best match: pip 18.1
Adding pip 18.1 to easy-install.pth file
Installing pip script to /usr/bin
Installing pip3.7 script to /usr/bin
Installing pip3 script to /usr/bin
Using /usr/lib/python2.7/site-packages
Processing dependencies for pip
Finished processing dependencies for pip
# pip2.7 install --upgrade pip
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
Collecting pip
Downloading http://mirrors.aliyun.com/pypi/packages/d8/f3/413bab4ff08e1fc4828dfc59996d721917df8e8583ea85385d51125dceff/pip-19.0.3-py2.py3-none-any.whl (1.4MB)
100% |████████████████████████████████| 1.4MB 73.7MB/s
Installing collected packages: pip
Found existing installation: pip 18.1
Uninstalling pip-18.1:
Successfully uninstalled pip-18.1
Successfully installed pip-19.0.3
# rm /usr/lib/python2.7/site-packages/setuptool* -rf
# python -m pip install --upgrade pip setuptools wheel
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7.
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
Requirement already up-to-date: pip in /usr/lib/python2.7/site-packages (19.0.3)
Collecting setuptools
Downloading http://mirrors.aliyun.com/pypi/packages/d1/6a/4b2fcefd2ea0868810e92d519dacac1ddc64a2e53ba9e3422c3b62b378a6/setuptools-40.8.0-py2.py3-none-any.whl (575kB)
100% |████████████████████████████████| 583kB 52.2MB/s
Collecting wheel
Downloading http://mirrors.aliyun.com/pypi/packages/96/ba/a4702cbb6a3a485239fbe9525443446203f00771af9ac000fa3ef2788201/wheel-0.33.1-py2.py3-none-any.whl
Installing collected packages: setuptools, wheel
Found existing installation: wheel 0.29.0
Uninstalling wheel-0.29.0:
Successfully uninstalled wheel-0.29.0
Successfully installed setuptools-40.8.0 wheel-0.33.1
最后得到 setuptools-40.8.0 這個版本
再次執(zhí)行
# python setup.py install
安裝成功
- 獲取 Gitosis 源碼舍败,并安裝
# git clone https://github.com/tv42/gitosis.git
# cd gitosis
# python setup.py install
- 備注: 這會安裝幾個供 Gitosis 使用的工具。默認 Gitosis 會把 /home/git 作為存儲所有 Git 倉庫的根目錄敬拓。
- 修改git賬號的權(quán)限
# vi /etc/passwd
找到 git:x: 開頭那行邻薯,改回
git:x:1001:1002::/home/git:/bin/bash
- 備注:上一段,我們直接用Git服務(wù)器時乘凸,為了避免git賬戶的非法登錄厕诡,我們改成每次必須登錄,命令行為git命令行的方式翰意。但是由于gitosis需要的系統(tǒng)命令更多木人,所以還是必須要改回bin/bash的模式
- 清掉/home/git/.ssh/authorized_keys
# rm -rf /home/git/.ssh/authorized_keys
# touch /home/git/.ssh/authorized_keys
# chown -R git:git /home/git/
- 備注:因為Gitosis需要接管Git服務(wù)器的授權(quán)訪問控制信柿,所以authorized_keys將由Gitosis來維護冀偶,為了避免控制面的沖突,我們需要清掉authorized_keys渔嚷,重新通過Gitosis來創(chuàng)建用戶进鸠。不用擔心之前的訪問權(quán)限丟失,下一步將之前的賬號加回來之后形病,權(quán)限自然恢復(fù)客年。
- 初始化Gitosis
準備工作:用ftp或其他將你的id_rsa.pub霞幅,ssh公鑰文件上傳的git服務(wù)器,假設(shè)位置在/tmp/id_rsa.pub量瓜。
然后還是在剛才的Gitosis源碼目錄下
# su - git
# gitosis-init < /tmp/id_rsa.pub
# cd ~
# ll
total 0
drwxr-xr-x 2 git git 27 Jan 28 01:28 gitosis
drwxr-xr-x 8 git git 124 Jan 28 01:37 repositories
- 備注:這里我執(zhí)行時司恳,只遇到過權(quán)限導致的問題,解決方案就是 回到 root賬號下绍傲,執(zhí)行 chown -R git:git /home/git/
- 總結(jié)一下扔傅,上面干了什么
主要工作是:
- 利用Python安裝好了Gitosis的環(huán)境
- 利用 /tmp/id_rsa.pub 初始化了一個管理Gitosis的賬戶
一切的真相在你打開 /home/git/.ssh/authorized_keys 后就知曉了。
# vi /home/git/.ssh/authorized_keys
你會看到剛才被我們清掉的authorized_keys文件里烫饼,被導入了id_rsa.pub的內(nèi)容猎塞,但是和我們自己的寫入的時候有所差異
### autogenerated by gitosis, DO NOT EDIT
command="gitosis-serve 用戶名",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
管理Gitosis
剛才我們安裝Gitosis后,發(fā)現(xiàn)/home/git下多了兩個目錄
# ll
total 0
drwxr-xr-x 2 git git 27 Jan 28 01:28 gitosis
drwxr-xr-x 8 git git 124 Jan 28 01:37 repositories
重要的目錄只有一個repositories杠纵,這里面存儲以后我們所有的項目的git倉庫文件
# cd /home/git/repositories
# ll
total 0
drwxr-x--- 7 git git 139 Jan 28 01:28 gitosis-admin.git
目前這個目錄下只有一個文件夾gitosis-admin.git荠耽,這就是Gitosis自帶的管理工具了,接下來我們需要把這個工具clone到本地比藻,來管理一下Gitosis
- clone Gitosis-admin
在你剛才獲得id_rsa.pub的那臺電腦铝量,一般就是你的工作機吧。
# git clone git@服務(wù)器IP:/gitosis-admin.git
如果是ssh端口號改變的情況
# git clone ssh://git@服務(wù)器IP:ssh端口號/gitosis-admin.git
- 備注1:注意到嗎银亲?gitosis-admin.git的并沒有使用全路徑來獲取款违。如果你執(zhí)行上面的命令成功了,說明你已經(jīng)可以使用相對路徑來訪問git倉庫了
- 備注2:如果你遇到錯誤:fatal: '/gitosis-admin.git' does not appear to be a git repository 群凶,也就是相對路徑不生效插爹。原因只是因為你可能沒有清空/home/git/.ssh/authorized_keys,該文件里有不是Gitosis標準的公鑰配置存在请梢,導致了Gitosis出錯了赠尾。方法就是刪掉authorized_keys,走剛才的安裝Gitosis時的第5步毅弧,重新初始化一下他气嫁,一般問題就解決了。
- clone成功够坐,怎么通過gitosis-admin就能管理Gitosis和Git呢寸宵?
原理:gitosis-admin 通過gitosis.conf來配置項目組、用戶組和權(quán)限關(guān)系元咙,通過keydir目錄保存用戶的公鑰梯影,通過git提交實現(xiàn)對Gitosis服務(wù)配置的更新維護。
看一下gitosis-admin的目錄
# dir
2019/01/28 01:15 <DIR> .
2019/01/28 01:15 <DIR> ..
2019/01/28 01:22 365 gitosis.conf
2019/01/28 01:21 <DIR> keydir
- 編輯gitosis.conf庶香,實現(xiàn)項目和用戶管理
[gitosis]
// gitosis-admin的可訪問用戶設(shè)置
[group gitosis-admin]
writable = gitosis-admin // 可讀寫的工程項目
members = Administrator // 這里名字和keydir目錄下的pub文件名字要保持一致甲棍,也就是說keydir目錄下要有一個名為 Administrator.pub 的文件
// 組可以只有用戶而沒有工程項目,這種可以方便的對項目的訪問權(quán)限進行用戶群組管理
[group developer]
members = tom Jams KK // 開發(fā)組赶掖,完整的讀寫權(quán)限
[group tester]
members = miya lucy lily //測試組感猛,只需要只讀權(quán)限
// 工程項目實例
[group test1]
writable = test1 // 可讀寫的工程項目
members = @developer // 三個開發(fā)組成員將同時具有該項目的讀寫權(quán)限七扰。
[group test1_readonly]
readonly = test1 // 只讀權(quán)限
members = @tester Jack // 三個測試組成員和Jack可以克隆和獲取更新,但 Gitosis 不會允許他向項目推送任何內(nèi)容陪白。
- 小結(jié)
通過上面的工作颈走,我們搞定了Gitosis管理Git服務(wù)器。Gitosis利用Git本身非常巧妙的實現(xiàn)了控制Git服務(wù)器的目的咱士。
如何將已有g(shù)it代碼遷移到自己的git服務(wù)器
這部分估計很多人都有經(jīng)驗了疫鹊,但是我還是記錄一下,讓這個經(jīng)驗總結(jié)有頭有尾司致,方便大家查看拆吆。
- 將本地代碼的變更及時緩存起來
# git add .
# git commit -m "init own git server"
- 在git服務(wù)器上創(chuàng)建空白倉庫
# cd /home/git/repositories
# mkdir 倉庫名.git
# cd 倉庫名.git
# git init --bare
- 利用Gitosis賦予倉庫訪問權(quán)限
# vi gitosis-admin/gitosis.conf
將內(nèi)容編輯為
[gitosis]
[group gitosis-admin]
members = administrator
writable = gitosis-admin
[group common]
members = Jack
[group 倉庫名]
members = @common
writable = 倉庫名
提交變更
# cd gitosis-admin
# git add .
# git commit -m "添加倉庫"
# git push
- 備注:到這里,倉庫名.git就能夠用Jack的賬戶進行遠程更新和上傳了脂矫。
- 修改本地代碼的git remote枣耀,將代碼上傳到git服務(wù)器
# git remote remove origin
# git remote add origin ssh://git@服務(wù)器IP:ssh端口/倉庫名.git
# git push -u origin master
# git pull
# git branch --set-upstream-to=origin/master master
# git pull
這里,Jack的本地代碼和git服務(wù)器的代碼就關(guān)聯(lián)起來了庭再。