在LINUX上創建GIT服務器【轉】


轉自:http://blog.csdn.net/xiongmc/article/details/9176785

如果使用git的人數較少,可以使用下面的步驟快速部署一個git服務器環境。

1. Client生成 SSH 公鑰,以便Server端識別。

每個需要使用git服務器的工程師,自己需要生成一個ssh公鑰

進入自己的~/.ssh目錄,看有沒有用 文件名 和 文件名.pub 來命名的一對文件,這個 文件名 通常是 id_dsa 或者 id_rsa。 .pub 文件是公鑰,另一個文件是密鑰。假如沒有這些文件(或者干脆連 .ssh 目錄都沒有),你可以用 ssh-keygen 的程序來建立它們,該程序在 Linux/Mac 系統由 SSH 包提供, 在 Windows 上則包含在 MSysGit 包里:

1
2
3
4
5
6
7
8
9
$ ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/schacon/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /Users/schacon/.ssh/id_rsa.
Your public key has been saved in /Users/schacon/.ssh/id_rsa.pub.
The key fingerprint is:
43:c5:5b:5f:b1:f1:50:43:ad:20:a6:92:6a:1f:9a:3a schacon@agadorlaptop.local

它先要求你確認保存公鑰的位置(.ssh/id_rsa),然后它會讓你重復一個密碼兩次,如果不想在使用公鑰的時候輸入密碼,可以留空。

現在,所有做過這一步的用戶都得把它們的公鑰給你或者 Git 服務器的管理者(假設 SSH 服務被設定為使用公鑰機制)。他們只需要復制 .pub 文件的內容然后 e-email 之。公鑰的樣子大致如下:

1
2
3
4
5
6
7
$ cat ~/.ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAklOUpkDHrfHY17SbrmTIpNLTGK9Tjom/BWDSU
GPl+nafzlHDTYW7hdI4yZ5ew18JH4JW9jbhUFrviQzM7xlELEVf4h9lFX5QVkbPppSwg0cda3
Pbv7kOdJ/MTyBlWXFCR+HAo3FXRitBqxiX1nKhXpHAZsMciLq8V6RjsNAQwdsdMFvSlVK/7XA
t3FaoJoAsncM1Q9x5+3V0Ww68/eIFmb1zuUFljQJKprrX88XypNDvjYNby6vw/Pb0rwert/En
mZ+AW4OZPnTPI89ZPmVMLuayrD2cE86Z/il8b+gw3r3+1nKatmIkjn2so1d01QraTlMqVSsbx
NrRFi9wrf+M7Q== schacon@agadorlaptop.local

2. 架設Server

 

首先,創建一個 ‘git’ 用戶並為其創建一個 .ssh 目錄,在用戶主目錄下:

1
2
3
4
$ sudo adduser git
$ su git
$ cd
$ mkdir .ssh
注意:將git用戶添加到sudo組,以便解決
ubuntu系統下“關於'xx'用戶不在 sudoers文件中,此事將被報告。”的解決方法。
怎么做?在具有sudo用戶下執行如下命令:
xiongmc@xiongmc-desktop:~$ sudo vim /etc/sudoers
然后,添加 git     ALL=(ALL:ALL) ALL  
#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults        env_reset
Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
# Host alias specification
# User alias specification
# Cmnd alias specification
# User privilege specification
root    ALL=(ALL:ALL) ALL
# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL
# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL
git     ALL=(ALL:ALL) ALL
# See sudoers(5) for more information on "#include" directives:
#includedir /etc/sudoers.d                        

接下來,把開發者的 SSH 公鑰添加到這個用戶的 authorized_keys 文件中。假設你通過 e-mail 收到了幾個公鑰並存到了臨時文件里(

或git@xiongmc-desktop:~$ sudo cat /home/client2/.ssh/id_rsa.pub >> /home/git/.ssh/authorized_keys)。只要把它們加入 authorized_keys 文件

 

1
2
3
$ cat /tmp/id_rsa.john.pub >> ~/.ssh/authorized_keys
$ cat /tmp/id_rsa.josie.pub >> ~/.ssh/authorized_keys
$ cat /tmp/id_rsa.jessica.pub >> ~/.ssh/authorized_keys

現在可以使用 –bare 選項運行 git init 來設定一個空倉庫,這會初始化一個不包含工作目錄的倉庫。

1
2
3
4
$ cd /opt/git
$ mkdir project.git
$ cd project.git
$ git --bare init

這時,開發人員就可以把它加為遠程倉庫,推送一個分支,從而把第一個版本的工程上傳到倉庫里了。值得注意的是,每次添加一個新項目都需要通過 shell 登入主機並創建一個純倉庫。我們不妨以 gitserver 作為 git 用戶和倉庫所在的主機名。如果你在網絡內部運行該主機,並且在 DNS 中設定 gitserver 指向該主機,那么以下這些命令都是可用的:

1
2
3
4
5
6
7
# 在一個工程師的電腦上
$ cd myproject
$ git init
$ git add .
$ git commit -m 'initial commit'
$ git remote add origin git@gitserver:/opt/git/project.git
$ git push origin master

這樣,其他人的克隆和推送也一樣變得很簡單:

1
2
3
4
$ git clone git@gitserver:/opt/git/project.git
$ vim README
$ git commit -am 'fix for the README file'
$ git push origin master

用這個方法可以很快捷的為少數幾個開發者架設一個可讀寫的 Git 服務。

作為一個額外的防范措施,你可以用 Git 自帶的 git-shell 簡單工具來把 git 用戶的活動限制在僅與 Git 相關。把它設為 git 用戶登入的 shell,那么該用戶就不能擁有主機正常的 shell 訪問權。為了實現這一點,需要指明用戶的登入shell 是 git-shell ,而不是 bash 或者 csh。你可能得編輯 /etc/passwd 文件

1
$ sudo vim /etc/passwd

在文件末尾,你應該能找到類似這樣的行:

1
git:x:1000:1000::/home/git:/bin/sh

把 bin/sh 改為 /usr/bin/git-shell (或者用 which git-shell 查看它的位置)。該行修改后的樣子如下:

1
git:x:1000:1000::/home/git:/usr/bin/git-shell

現在 git 用戶只能用 SSH 連接來推送和獲取 Git 倉庫,而不能直接使用主機 shell。嘗試登錄的話,你會看到下面這樣的拒絕信息:

 

1
2
3
$ ssh git@gitserver
fatal: What do you think I am? A shell? (你以為我是個啥?shell嗎?)
Connection to gitserver closed. (gitserver 連接已斷開。)

 

Q&A參考

(4)為了集成到SCM,我們在Linxu上安裝GIT
http://www.examw.com/linux/all/182529/index-2.html
在LINUX上創建GIT服務器
http://lionest.iteye.com/blog/1447310
http://blog.csdn.net/andy_android/article/details/6996134
Receiving objects:  26% (5668/21560), 8.06 MiB | 183 KiB/s      21560)   
(5)
Q:
xiongmc@xiongmc-desktop:~/myproject.git$ git push origin master ssh: connect to host xiongmc-desktop port 22: Connection refused
fatal: The remote end hung up unexpectedly
xiongmc@xiongmc-desktop:~/myproject.git$ git push origin master 
ssh: connect to host xiongmc-desktop port 22: Connection refused
fatal: The remote end hung up unexpectedly

A:
http://blog.csdn.net/zlm_250/article/details/7979221
sudo apt-get install openssh-server
sudo net start sshd  
sudo ufw disable 
ssh localhost  

(6)
Q:
ubuntu系統下“關於'xx'用戶不在 sudoers文件中,此事將被報告。”的解決方法


A:
http://blog.sina.com.cn/s/blog_bede36550101b0av.html
git ALL=(ALL:ALL) ALL


(7)
Q:
xiongmc@xiongmc-desktop:~/myproject.git$ git push origin master 
git@xiongmc-desktop's password: 
fatal: '/opt/git/project.git' does not appear to be a git repository
fatal: The remote end hung up unexpectedly


A:
http://www.dotkam.com/2010/08/22/gitolite-does-not-appear-to-be-a-git-repository/








2013-5-26
(1)
Q:
xiongmc@xiongmc-desktop:~/myproject2$ git push origin master
Agent admitted failure to sign using the key.
git@localhost's password: 
error: src refspec master does not match any.
error: failed to push some refs to 'git@localhost:/opt/git/project.git/'


A:
http://www.linuxidc.com/Linux/2013-03/81022.htm


如果初始的代碼倉庫為空,git push origin master提交代碼的時候會出現以下異常:


(2)
Q:
xiongmc@xiongmc-desktop:~/myproject2$ git push origin master
Agent admitted failure to sign using the key.
git@localhost's password: 
Permission denied, please try again.
git@localhost's password: 
Counting objects: 3, done.
Writing objects: 100% (3/3), 213 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
error: insufficient permission for adding an object to repository database ./objects


fatal: failed to write object
error: unpack failed: unpack-objects abnormal exit
To git@localhost:/opt/git/project.git/
 ! [remote rejected] master -> master (n/a (unpacker error))
error: failed to push some refs to 'git@localhost:/opt/git/project.git/'
A:
服務器無權限。
http://linsheng1990526.blog.163.com/blog/static/203824150201231423917228/


(3)
http://www.linuxidc.com/Linux/2013-03/81022.htm
Q:


xiongmc@xiongmc-desktop:~/myproject2$ git push origin master
Agent admitted failure to sign using the key.
git@localhost's password: 
Counting objects: 3, done.
Writing objects: 100% (3/3), 213 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: error: refusing to update checked out branch: refs/heads/master
remote: error: By default, updating the current branch in a non-bare repository
remote: error: is denied, because it will make the index and work tree inconsistent
remote: error: with what you pushed, and will require 'git reset --hard' to match
remote: error: the work tree to HEAD.
remote: error: 
remote: error: You can set 'receive.denyCurrentBranch' configuration variable to
remote: error: 'ignore' or 'warn' in the remote repository to allow pushing into
remote: error: its current branch; however, this is not recommended unless you
remote: error: arranged to update its work tree to match what you pushed in some
remote: error: other way.
remote: error: 
remote: error: To squelch this message and still keep the default behaviour, set
remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To git@localhost:/opt/git/project.git/
 ! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to 'git@localhost:/opt/git/project.git/'


A:


$cd .git
$vim config


該配置文件的原始內容為:


[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true


在該配置文件中加入以下內容:


[receive]
denyCurrentBranch = ignore


(4)
Linux服務器:Ubuntu配置git服務 - 逸雲沙鷗


http://www.xue5.com/Server/Linux/667461.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM