CentOS 架設git完整版

來源:互聯網
上載者:User

標籤:

 Git 伺服器搭建:

http://www.centoscn.com/CentosServer/ftp/2014/0414/2789.html

http://871421448.iteye.com/blog/1912205

http://weizhifeng.tumblr.com/post/25209375100/host-git-repositories-on-gitosis

 

1. 環境部署

系統內容:伺服器端:CentOS 6.5 ,ip:172.20.48.15

        用戶端:CentOS 6.5 ,ip:172.20.48.18

軟體版本:伺服器端:源碼編譯安裝,git-1.9.0.tar.gz

        用戶端:yum線上安裝機制

 

2. 安裝  (伺服器端)

安裝git

#yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel

#Wget : http://distfiles.macports.org/git/

#tar zxvf git-1.9.0.tar.gz

#cd git-1.9.0

#make prefix=/usr/local all

#make prefix=/usr/local install  #root使用者運行

查看版本號碼:git --version

git version 1.9.0

 

安裝gitosis

gitosis為Git使用者權限管理系統,通過管理服務端的/home/git/.ssh/authorized_key檔案來執行對使用者權限的管理,是一個python模組包

#yum install python python-setuptools

#git clone git://github.com/res0nat0r/gitosis.git

#cd gitosis/

#python setup.py install

顯示Finished processing dependencies for gitosis==0.2即表示成功 

 

 

安裝 git ( 用戶端 ):

#yum install git

#git --version

git version 1.7.1

 

 

3. ssh設定  (client秘鑰 , server 公開金鑰)

Client:

#scp ~/.ssh/id_rsa.pub   [email protected]:~/

Server

#ssh-keygen -t rsa

# cp  id_rsa.pub  /tmp/

 

4. 伺服器上產生git使用者

添加使用者git:

#useradd  -d /home/git git

#chown git:git /home/git

 

修改PATH,使git使用者可以調用git:

vi /home/git/.bashrc
PATH=/usr/local/bin:/usr/local/git/bin:$PATH
 

 

在伺服器端產生管理庫 : (用來初始化 gitosis )

方式1: (client 公開金鑰產生管理庫)

#sudo -H -u git gitosis-init < ~/id_rsa.pub

Initialized empty Git repository in /home/git//repositories/gitosis-admin.git/ Reinitialized existing Git repository in /home/git/repositories/gitosis-admin.git/ 

註解:

1. 產生的gitosis-admin為Git的使用者存取權限管理庫,gitosis通過這個git庫來管理所有git庫的存取權限。

2. 通過執行初始化,該公開金鑰的擁有者就能修改用於配置gitosis的那個特殊Git倉庫了

修改上傳許可權:

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

 

 

方式2: (server 公開金鑰產生管理庫)

$ su git # 切換倒git使用者下

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

 

方式3: 簡單命令產生管理庫:

git init –bare test.git

 

5.擷取並配置gitosis-admin 管理端(server端-----client端)

 Server 端 只可以 clone server 公開金鑰產生的管理庫

 Client 端 只可以 clone  client 公開金鑰產生的管理庫

 

#mkdir -p /git-repo/

#cd /git-repo/

#git clone [email protected]端IP:gitosis-admin.git

#cd gitosis-admin

#find .

./gitosis.conf

./keydir

./keydir/[email protected]

註解:

gitosis.conf檔案用來設定使用者、倉庫和許可權的控制檔案

keydir目錄則是儲存所有具有存取權限使用者公開金鑰的地方

./keydir/[email protected]:如前所述,該使用者具有存取權限

 

6.建立及設定管理項目 

#cd /git-repo/gitosis-admin

#ls keydir/

[email protected]     ###### 檔案名稱必須為: xxx.pub

 

添加項目:

#vim gitosis.conf

[gitosis]
[group gitosis-admin]
writable = gitosis-admin
members = [email protected]        #顯示使用者[email protected]是初始化gitosis公開金鑰的擁有者,是唯一能管理gitosis-admin項目的人


[group test ]           #組名稱
members = [email protected]        #密鑰使用者名稱
writable = test               #倉庫名稱

#Git commit  -a  -m “user add”

#Git  push

 

Writable  可讀寫  readonly 唯讀

對於同一個倉庫設定不同的許可權, 如 :

[group test]

Writable =test    倉庫名

Members =[email protected]

[grouptest2]

Readonly=  test   倉庫名

Members =[email protected]

如果在gitpush的時候,遇到錯誤“ddress 192.168.0.77 maps to bogon, butthis does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!”,解決為修改/etc/hosts檔案,將ip地址與主機名稱對應關係寫進去就可以了。

 

7. 初始、增加及使用項目git-test (用戶端 )

#cd /git-repo

#mkdir test

#cd test

 

#touch README

#git add .

#git commit -a -m "init git-test"

#git remote add origin [email protected]:git-test.git

#git push origin master

 

註解:在新項目git-test裡首次推送資料到伺服器前,需先設定該伺服器位址為遠程倉庫,但你不用事先到伺服器上手工建立該項目個體的裸倉庫— Gitosis 會在第一次遇到推送時自動建立。

 

說明:如果在執行git push origin master 的時候,提示以下錯誤:

error: srcrefspec master does not match any.

error: failedto push some refs to ‘[email protected]:pro2.git‘

這是由於項目為空白的原因,我們在項目目錄裡新建立一個檔案。經過->add -> commit -> push 就可以解決了

 

Ps:

$ touch a.txt

$ git     add     a.txt

在添加檔案的目錄中, 輸入 gitadd 。

$ git commit -a -m ‘adda.txt‘

$ git push

如果在gitclone的時候遇到“

error: cannotrun ssh: No such file or directory - cygwin git”錯誤,則表示本機沒有安裝ssh命令。安裝方法請參考:http://blog.haohtml.com/archives/13313

 

8. 用戶端增加其他成員公開金鑰到系統中:通過添加使用者的公開金鑰到keydir目錄即可

 

1.產生密鑰與公開金鑰

[[email protected] ~]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/dev1/.ssh/id_rsa):
Created directory ‘/home/dev1/.ssh‘.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/dev1/.ssh/id_rsa.
Your public key has been saved in /home/dev1/.ssh/id_rsa.pub.
The key fingerprint is:
99:4c:e4:00:56:a9:a2:e2:68:20:8c:60:90:2e:b9:b8 [email protected]
The key‘s randomart image is:
+--[ RSA 2048]----+
| . ooo.. |
|o . .+ |
|o. . o |
|+o. . o o |
|Bo . S |
|O. |
|*. |
|Eo |
|. |
+-----------------+

[[email protected]~]$ ls .ssh
id_rsa id_rsa.pub

2.將公開金鑰上傳到gitservergitosis-admin/keydir/目錄裡,檔案名稱就是是使用者名稱(這裡檔案名稱必須與用戶端的使用者名稱一致,否則無法使用.不知道同名的情況下如何處理?)
注意:我這裡是在同一台機器上測試的,不同的機器可以用scp命令來實現上傳檔案,主要pub檔案上傳到keydir目錄下才可以(scp ~/.ssh/id_rsa.pub [email protected]:/tmp/sxf.pub) 

cd /root/gitosis-admin/keydir/
cp /home/dev1/.ssh/id_rsa.pub ./dev1.pub

3.建立新項目,將dev1使用者添加進去

cd /home/gitosis-admin/

vigitosis.conf
[group project2]
writable = project2
members = dev1

4.儲存修改,並將修改提交到git server

git add .
git commit -a -m ‘add new user dev1‘
git push

5.在用戶端測試(使用者為dev1)
建立項目project2目錄(一定要和項目名稱一樣)

mkdir/data/project2
cd /data/project2
git init
touch test.txt
git add .

gitconfig --global user.email "[email protected]"
git config --global user.name "Your Name"

gitcommit -a -m ‘init project2‘
git remote add origin [email protected]:project2.git
git push origin master

gitpull

可以看到已經提交成功了!

如果用戶端為Linux的話,在執行git pull的時候,出現

 Permissions0644 for ‘/root/.ssh/id_rsa’ are too open.

等錯誤顯示了,只要把許可權降到0600就ok了.這種情況下一般是屬於將另其它地方的公開金鑰直接放在其它用戶端用的原因。

輸入命令

chmod 0600 ~/.ssh/id_rsa

然後再執行相應的命令即可.這樣就可以密鑰登陸了.

 

9. 客戶免密碼登陸:

在 /home/dongsheng/.ssh/authorized_keys檔案中將在用戶端產生的dongsheng 公開金鑰寫入。  (如果沒有authorized_keys 此檔案,自己建立)


注意:git上的倉庫對git使用者要有寫入權限,同時需要將/etc/ssh/sshd_config中將RSA認證開啟,即: 

  1. 1.RSAAuthentication yes     
  2. 2.PubkeyAuthentication yes     
  3. 3.AuthorizedKeysFile  .ssh/authorized_keys

 

10. 常見問題:

/home/git/repositories/gitosis-admin.git/hooks/post-update為可執行即屬性為 0755

1. git操作需要輸入密碼
原因
公密未找到
解決
上傳id_rsa.pub到keydir並改為’gitosis帳號.pub’形式,如miao.pub。副檔名.pub不可省略

2.ERROR:gitosis.serve.main:Repository read access denied

 

11. 自己隨手補充:

1》  git init –bare  test.git 命令建立的倉庫 ,如果clone code , 只能使用git 組內的成員

2》  gitosis- admin  命令建立的倉庫, 如果clone  code , 只有git 一個user 可以使用

CentOS 架設git完整版

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.