溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何在CentOS 6.3下使用Gitosis安裝搭建Git Server

發(fā)布時(shí)間:2021-07-29 18:35:47 來(lái)源:億速云 閱讀:116 作者:chen 欄目:系統(tǒng)運(yùn)維

本篇內(nèi)容介紹了“如何在CentOS 6.3下使用Gitosis安裝搭建Git Server”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

Git作為一個(gè)分布式的版本控制系統(tǒng),使用git的時(shí)候,一般和服務(wù)器通訊使用的是ssh協(xié)議,用ssh的主要優(yōu)點(diǎn)是速度快(傳輸前數(shù)據(jù)會(huì)先壓縮,比HTTP快),安全,方便讀寫(xiě)。
 
客戶端通過(guò)ssh訪問(wèn)服務(wù)器端的驗(yàn)證方式一般有兩種,一種是用戶名密碼的方式,一種是使用公私鑰認(rèn)證的方式. 使用公私鑰的方式比較方便,無(wú)需每次登錄輸入密碼。

某個(gè)受信任的客戶端的公鑰會(huì)被設(shè)置在服務(wù)器端的 ~/.ssh/authorized_keys文件中,有關(guān)此文件的格式可以參見(jiàn) sshd的用戶手冊(cè) man sshd . authorized_keys有個(gè)比較厲害的功能是 支持 command參數(shù),使得每次用戶使用此公鑰進(jìn)行驗(yàn)證的時(shí)候執(zhí)行此后面的命令.這樣就可以做一些邏輯處理了.

一般git庫(kù)的管理需要權(quán)限控制,如何方便簡(jiǎn)單的進(jìn)行庫(kù)的權(quán)限管理呢? authorized_keys是一個(gè)思路,指定特定command參數(shù),每次驗(yàn)證好用戶后首先執(zhí)行相關(guān)邏輯,檢測(cè)當(dāng)前用戶是否具有某個(gè)權(quán)限。 所以便有了gitosis,與其說(shuō)gitosis是一個(gè)git權(quán)限管理系統(tǒng),還不如說(shuō)它是一個(gè)authorized_keys文件管理器.

解決方案:

環(huán)境部署

操作系統(tǒng):              centos6.3 x64
Git:                         git-1.7.1
Gitosis:                   Gitosis
Gitweb:                   1.7.1-3        
OpenSSH Server:     openssh-server-5.3p1
apache:                  httpd-2.4.4
python-setuptools:   python-setuptools-0.6.10-3
        
Git server(centos6.3 x64): node2.example.com
Git client(centos6.3 x64): node1.example.com

server端配置:

一.關(guān)閉iptables和SELINUX

代碼如下:


# service iptables stop
# setenforce 0
# vi /etc/sysconfig/selinux
---------------
SELINUX=disabled
---------------

二.同步時(shí)間

代碼如下:


# ntpdate cn.pool.ntp.org

三.安裝apache

傳送門(mén):http://www.kemok4.com/article/54969.htm

四.安裝OpenSSH

1.yum安裝OpenSSH:

代碼如下:


# yum install openssh-server -y


2.修改ssh服務(wù)端配置:

代碼如下:


# vi /etc/ssh/sshd_config
——————————————————————————————
  Port 22 # 修改成你想要的登陸端口
  PermitRootLogin no # 禁止root用戶登陸
  StrictModes yes # 檢查密鑰的用戶和權(quán)限是否正確,默認(rèn)打開(kāi)的
  RSAAuthentication yes # 啟用 RSA 認(rèn)證
  PubkeyAuthentication yes # 啟用公鑰認(rèn)證
  PasswordAuthentication yes # 啟用密碼認(rèn)證,默認(rèn)是打開(kāi)的
  ServerKeyBits 1024 # 修改后變?yōu)榇藸顟B(tài),將ServerKey強(qiáng)度改為1024比特
 PermitEmptyPasswords no # 修改后變?yōu)榇藸顟B(tài),禁止空密碼進(jìn)行登錄
——————————————————————————————


3.重啟服務(wù):

代碼如下:


# /etc/init.d/sshd restart

五.安裝Git

代碼如下:


# yum install git-core -y

六.安裝Gitosis

1.安裝Gitosis依賴(lài)python-setuptools包

代碼如下:


# yum install python-setuptools -y

2.安裝Gitosis

代碼如下:


# cd ~
# mkdir src
# cd src
# git clone https://github.com/tv42/gitosis.git
# cd gitosis
# python setup.py install

3.為gitosis創(chuàng)建系統(tǒng)用戶

代碼如下:


# useradd -m git
# passwd git

4. 運(yùn)行g(shù)itosis

(1).將管理員生成的公鑰上傳或拷貝到服務(wù)器上。這里的公鑰需要在git服務(wù)器管理員下使用ssh-keygen命令來(lái)創(chuàng)建

代碼如下:


# su - git


保證web頁(yè)面有權(quán)限顯示該倉(cāng)庫(kù)內(nèi)容

代碼如下:


# chmod -R 755 /home/git
# ssh-keygen -t rsa
# cp ~/.ssh/id_rsa.pub /tmp

(2).初始化gitosis

進(jìn)入到拷貝過(guò)來(lái)的id_rsa.pub所在目錄

代碼如下:


# cd /tmp
# gitosis-init < id_rsa.pub


此時(shí),會(huì)在/home/git目錄下生成gitosis倉(cāng)庫(kù)和配置目錄

代碼如下:


# cd /home/git
# ll
----------------------------------------------------------------
drwxr-xr-x 2 git git 4096 Aug 12 13:39 gitosis
drwxr-xr-x 4 git git 4096 Aug 12 13:39 repositories
---------------------------------------------------------------

(3).切換回當(dāng)前(root)用戶

代碼如下:


# exit

(4).配置權(quán)限

如果想要?jiǎng)e人能夠clone gitosis-admin.git,需要執(zhí)行以下操作:

代碼如下:


# chmod 755 /home/git/repositories/gitosis-admin.git/hooks/post-update


至此,gitosis的安裝工作已完成,其相關(guān)配置可以有管理員來(lái)操作,然后再提交到服務(wù)器上.

(5)現(xiàn)在可以試一下用初始化 Gitosis 的公鑰的擁有者身份 SSH 登錄服務(wù)器,應(yīng)該會(huì)看到類(lèi)似下面這樣:

代碼如下:


# su - git
$ ssh git@127.0.0.1
------------------------------------------------
PTY allocation request failed on channel 0
ERROR:gitosis.serve.main:Need SSH_ORIGINAL_COMMAND in environment.
 Connection to gitserver closed.
------------------------------------------------


說(shuō)明 Gitosis 認(rèn)出了該用戶的身份,但由于沒(méi)有運(yùn)行任何 Git 命令,所以它切斷了連接。那么,現(xiàn)在運(yùn)行一個(gè)實(shí)際的 Git 命令 &mdash; 克隆 Gitosis 的控制倉(cāng)庫(kù):
在你本地計(jì)算機(jī)上克隆git倉(cāng)庫(kù)

代碼如下:


# cd /tmp
# git clone git@gitserver:gitosis-admin.git


這會(huì)得到一個(gè)名為 gitosis-admin 的工作目錄,主要由兩部分組成:
紅色為git倉(cāng)庫(kù)配置,藍(lán)色為實(shí)際倉(cāng)庫(kù)保存的文件

代碼如下:


# cd gitosis-admin
# ll -a
----------------------------------------------------------
total 20
drwxr-xr-x 4 git git 4096 Aug 12 13:21 .
drwxr-xr-x 4 git git 4096 Aug 12 13:23 ..
drwxr-xr-x 8 git git 4096 Aug 12 13:22 .git
-rwxr-xr-x 1 git git  157 Aug 12 13:21 gitosis.conf
drwxr-xr-x 2 git git 4096 Aug 12 13:20 keydir
-----------------------------------------------------------


以上操作相當(dāng)于,系統(tǒng)git用戶初始化并成為gitosis管理員,且利用其管理員權(quán)限將gitosis-admin倉(cāng)庫(kù)clone到本地.

5.添加本地用戶john和倉(cāng)庫(kù)test到gitosis,并和管理員git合作管理gitosis

1. 用戶john添加并發(fā)送id_rsa.pub給git

代碼如下:


# su -
# useradd john & passwd john
# su - john
# ssh-keygen -t rsa
-----------------------------------------------------------
Generating public/private rsa key pair.
Enter file in which to save the key (/home/john/.ssh/id_rsa):
Created directory '/home/john/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/john/.ssh/id_rsa.
Your public key has been saved in /home/john/.ssh/id_rsa.pub.
-----------------------------------------------------------
# cp /home/john/.ssh/id_rsa.pub /tmp


2. gitosis管理員git分配john權(quán)限

代碼如下:


# su - git
# mkdir projects
# cd ~/projects
# git clone git@node2.example.com:gitosis-admin
# cd gitosis-admin
# cat gitosis.conf
------------------------------------------------
[gitosis]
[group gitosis-admin]
writable = gitosis-admin
members = git@node2.example.com
------------------------------------------------
# ls keydir/
-------------------------
git@node2.example.com.pub
-------------------------
# cp /tmp/id_rsa.pub keydir/john.pub
# vi gitosis.conf
&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;
[gitosis]
[group gitosis-admin]
writable = gitosis-admin
members = git@node2.example.com
[group test]
writable = test
members = git@node2.example.com john
&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;
# git add .
# git commit -am "add member john and project foo"
# git push

3. 用戶git添加項(xiàng)目test

代碼如下:


# su - git
# cd ~/projects
# mkdir test
# cd test
# git init
# echo "Hello World." > hello.txt
# git add hello.txt
# git commit -am 'first commit'
# git remote add origin git@node2.example.com:test.git
# git push origin master

4. 用戶 john clone test并修改hello.txt

代碼如下:


# su - john
# git clone git@node2.example.com:test.git
# cd test
# date >> hello.txt
# git commit -am 'add time to hello.txt' && git push

整個(gè)過(guò)程分為:

1.通過(guò)修改gitosis-admin管理gitosis用戶權(quán)限,需要clone到本地,然后修改配置文件,最后add push將結(jié)果推送到遠(yuǎn)程實(shí)現(xiàn)權(quán)限修改.

2.添加系統(tǒng)用戶,生成該用戶公鑰,并將其復(fù)制到keydir下,實(shí)現(xiàn)該用戶有權(quán)限進(jìn)行g(shù)it等相關(guān)操作.

3.登陸該用戶賬戶進(jìn)行g(shù)it相關(guān)操作,修改完后commit,push到中服務(wù)器即可完成倉(cāng)庫(kù)權(quán)限配置.

七.安裝gitweb

1.首先我們需要Git的源碼,其中帶有GitWeb,并能生成定制的CGI腳本:

代碼如下:


# git clone git://git.kernel.org/pub/scm/git/git.git
# cd git/
# make GITWEB_PROJECTROOT="/home/git/repositories" prefix=/usr gitweb      
# cp -rf gitweb /usr/local/apache2/htdocs/


注: 通過(guò)指定 GITWEB_PROJECTROOT 變量告訴編譯命令 Git 倉(cāng)庫(kù)的位置

2.設(shè)置Apache以CGI方式運(yùn)行該腳本,并添加一個(gè)VirtualHost配置:

(1).加載apache的vhost配置文件

代碼如下:


# vi /usr/local/apache2/conf/httpd.conf


搜索包含httpd-vhosts的行,并去掉該行注釋.
(2).加載cgid模塊,使其支持perl語(yǔ)言.

代碼如下:


# vi /usr/local/apache2/conf/httpd.conf


搜索包含mod_cgid.so的行,并去掉該行注釋.
(3).配置VirtualHost

代碼如下:


# vi /usr/local/apache2/conf/extra/httpd-vhosts.conf


添加如下配置:

代碼如下:


&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;
<VirtualHost *:80>
   ServerName git.example.com
   DocumentRoot /usr/local/apache2/htdocs/gitweb
   <Directory /usr/local/apache2/htdocs/gitweb>
       Options +ExecCGI
       AllowOverride All
       order allow,deny
       Allow from all
       AddHandler cgi-script cgi pl
       DirectoryIndex gitweb.cgi
   </Directory>
</VirtualHost>
&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;


(4).安裝Time/HiRes.pm perl模塊
首次打開(kāi)web頁(yè)面報(bào)Can't locate Time/HiRes.pm in @INC &hellip;.錯(cuò)誤
解決方法:

代碼如下:


# yum install perl-devel perl-CPAN -y
# perl -MCPAN -e shell
cpan[2]> install Time::HiRes
cpan[3]> exit


(5).重啟apache服務(wù)

代碼如下:


# /usr/local/apache2/bin/apachectl restart


(6).修改本機(jī)HOST,并打開(kāi)gitweb頁(yè)面
http://git.example.com

如何在CentOS 6.3下使用Gitosis安裝搭建Git Server如何在CentOS 6.3下使用Gitosis安裝搭建Git Server

如何在CentOS 6.3下使用Gitosis安裝搭建Git Server

如何在CentOS 6.3下使用Gitosis安裝搭建Git Server

“如何在CentOS 6.3下使用Gitosis安裝搭建Git Server”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI