佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > CentOS 6.3下使用Gitosis安裝搭建Git Server教程

CentOS 6.3下使用Gitosis安裝搭建Git Server教程

熱門標(biāo)簽:銀行業(yè)務(wù) 客服熱線 外呼系統(tǒng) 電銷行業(yè) 客戶服務(wù) 記事本 分布式呼叫中心 回?fù)芟到y(tǒng)

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

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

一般git庫的管理需要權(quán)限控制,如何方便簡單的進行庫的權(quán)限管理呢? authorized_keys是一個思路,指定特定command參數(shù),每次驗證好用戶后首先執(zhí)行相關(guān)邏輯,檢測當(dāng)前用戶是否具有某個權(quán)限。 所以便有了gitosis,與其說gitosis是一個git權(quán)限管理系統(tǒng),還不如說它是一個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


復(fù)制代碼
代碼如下:

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

二.同步時間


復(fù)制代碼
代碼如下:

# ntpdate cn.pool.ntp.org

三.安裝apache

傳送門:https://www.jb51.net/article/54969.htm

四.安裝OpenSSH

1.yum安裝OpenSSH:

復(fù)制代碼
代碼如下:

# yum install openssh-server -y

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

復(fù)制代碼
代碼如下:

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

3.重啟服務(wù):

復(fù)制代碼
代碼如下:

# /etc/init.d/sshd restart

五.安裝Git


復(fù)制代碼
代碼如下:

# yum install git-core -y

六.安裝Gitosis

1.安裝Gitosis依賴python-setuptools包

復(fù)制代碼
代碼如下:

# yum install python-setuptools -y

2.安裝Gitosis

復(fù)制代碼
代碼如下:

# 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)用戶

復(fù)制代碼
代碼如下:

# useradd -m git
# passwd git

4. 運行g(shù)itosis

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

復(fù)制代碼
代碼如下:

# su - git

保證web頁面有權(quán)限顯示該倉庫內(nèi)容

復(fù)制代碼
代碼如下:

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

(2).初始化gitosis

進入到拷貝過來的id_rsa.pub所在目錄

復(fù)制代碼
代碼如下:

# cd /tmp
# gitosis-init id_rsa.pub

此時,會在/home/git目錄下生成gitosis倉庫和配置目錄

復(fù)制代碼
代碼如下:

# 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)用戶


復(fù)制代碼
代碼如下:

# exit

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

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

復(fù)制代碼
代碼如下:

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

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

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

復(fù)制代碼
代碼如下:

# 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.
------------------------------------------------

說明 Gitosis 認(rèn)出了該用戶的身份,但由于沒有運行任何 Git 命令,所以它切斷了連接。那么,現(xiàn)在運行一個實際的 Git 命令 — 克隆 Gitosis 的控制倉庫:
在你本地計算機上克隆git倉庫

復(fù)制代碼
代碼如下:

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

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

復(fù)制代碼
代碼如下:

# 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倉庫clone到本地.

5.添加本地用戶john和倉庫test到gitosis,并和管理員git合作管理gitosis

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

復(fù)制代碼
代碼如下:

# 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)限

復(fù)制代碼
代碼如下:

# 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
————————————————————————————————————
[gitosis]
[group gitosis-admin]
writable = gitosis-admin
members = git@node2.example.com
[group test]
writable = test
members = git@node2.example.com john
————————————————————————————————————
# git add .
# git commit -am "add member john and project foo"
# git push

3. 用戶git添加項目test


復(fù)制代碼
代碼如下:

# 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

復(fù)制代碼
代碼如下:

# 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

整個過程分為:

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

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

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

七.安裝gitweb

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

復(fù)制代碼
代碼如下:

# 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/

注: 通過指定 GITWEB_PROJECTROOT 變量告訴編譯命令 Git 倉庫的位置

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

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

復(fù)制代碼
代碼如下:

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

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

復(fù)制代碼
代碼如下:

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

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

復(fù)制代碼
代碼如下:

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

添加如下配置:

復(fù)制代碼
代碼如下:

——————————————————————————————————————————
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>
——————————————————————————————————————————

(4).安裝Time/HiRes.pm perl模塊
首次打開web頁面報Can't locate Time/HiRes.pm in @INC ….錯誤
解決方法:

復(fù)制代碼
代碼如下:

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

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

復(fù)制代碼
代碼如下:

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

(6).修改本機HOST,并打開gitweb頁面
http://git.example.com

大功告成....

標(biāo)簽:拉薩 德州 阜陽 公主嶺 濱州 阿里 廣東 綿陽

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《CentOS 6.3下使用Gitosis安裝搭建Git Server教程》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    莲花县| 霞浦县| 介休市| 内乡县| 钟山县| 庆元县| 深水埗区| 白银市| 玛沁县| 安宁市| 龙海市| 黑龙江省| 梁河县| 寿宁县| 广宁县| 塘沽区| 隆昌县| 遵义县| 米泉市| 临安市| 清水县| 页游| 松滋市| 渭源县| 赫章县| 织金县| 清镇市| 连州市| 新乡县| 丹寨县| 邯郸市| 商洛市| 仙游县| 西城区| 枣阳市| 玉门市| 晋宁县| 仲巴县| 曲阳县| 淮南市| 莒南县|