在LINUX上建立GIT服務器

若是使用Git的人數較少,可使用下面的步驟快速部署一個git服務器環境。html

備註: .ssh 文件夾權限 700; .ssh/authorized_keys 文件權限 600linux

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

每一個須要使用git服務器的工程師,本身須要生成一個ssh公鑰android

進入本身的~/.ssh目錄,看有沒有用 文件名 和 文件名.pub 來命名的一對文件,這個 文件名 一般是 id_dsa 或者 id_rsa。 .pub 文件是公鑰,另外一個文件是密鑰。假如沒有這些文件(或者乾脆連 .ssh 目錄都沒有),你能夠用 ssh-keygen 的程序來創建它們,該程序在 Linux/Mac 系統由 SSH 包提供, 在 Windows 上則包含在 MSysGit 包裏:git

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),而後它會讓你重複一個密碼兩次,若是不想在使用公鑰的時候輸入密碼,能夠留空。shell

如今,全部作過這一步的用戶都得把它們的公鑰給你或者 Git 服務器的管理者(假設 SSH 服務被設定爲使用公鑰機制)。他們只須要複製 .pub 文件的內容而後 e-email 之。公鑰的樣子大體以下:ubuntu

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 目錄,在用戶主目錄下:vim

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 收到了幾個公鑰並存到了臨時文件裏(bash

或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/7979221sudo apt-get install openssh-serversudo net start sshd  sudo ufw disable ssh localhost  (6)Q:ubuntu系統下「關於'xx'用戶不在 sudoers文件中,此事將被報告。」的解決方法A:http://blog.sina.com.cn/s/blog_bede36550101b0av.htmlgit 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 repositoryfatal: The remote end hung up unexpectedlyA: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 masterAgent 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 masterAgent 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 ./objectsfatal: failed to write objecterror: unpack failed: unpack-objects abnormal exitTo 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.htmQ:xiongmc@xiongmc-desktop:~/myproject2$ git push origin masterAgent 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/masterremote: error: By default, updating the current branch in a non-bare repositoryremote: error: is denied, because it will make the index and work tree inconsistentremote: error: with what you pushed, and will require 'git reset --hard' to matchremote: error: the work tree to HEAD.remote: error: remote: error: You can set 'receive.denyCurrentBranch' configuration variable toremote: error: 'ignore' or 'warn' in the remote repository to allow pushing intoremote: error: its current branch; however, this is not recommended unless youremote: error: arranged to update its work tree to match what you pushed in someremote: error: other way.remote: error: remote: error: To squelch this message and still keep the default behaviour, setremote: 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

相關文章
相關標籤/搜索