在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

相关文章
相关标签/搜索