Git服务器连接可用的协议通常有SSH、GIT、HTTP(S)和FTP等,其中最方便实现的是SSH方式。
SSH方式的GIT服务器实现也有两种方式:如果你在一个小型团队并且不需要做权限控制,那么使用SSH就可以搭建了,否则你还需要gitolite
本文将介绍如何使用SSH实现最简单Git服务器搭建。注意,前面所述的两种实现方式不可同时搭建。如何解决,在写本文时还未探索。想来应该需要在服务器上使用不同用户做隔离吧。
因为公司版本控制使用的是SVN,并没有提供搭建git的服务器,因此我的git服务器搭建在了本机Ubuntu上(为了配合Redmine一起使用,否则没必要搭这个本地服务器)。
准备工作
首先,要保证git服务功能可用。在安装git的时候,你可能安装了git包,而不是git-core包。要提供git服务器功能需要完整安装git-core包(不知道是不是真的,网上大家都这么说,反正我搭建之前已经安装了,没去验证真伪)。
sudo apt-get install git-core
当然,你还需要保证你的SSH服务是可用的
sudo apt-get install openssh-server
服务器用户
之后需要配置git服务用户
$ sudo useradd -c "git server account" -m -r -U git
$ id git
uid=999(git) gid=998(git) 组=998(git)
这样我们就创建了一个隐藏账户git,其id为999(ID小于1000的用户不会出现在登录列表中)。同时为其创建了同名用户组
之后需要为git账户临时设置密码(正常情况下git账户是不允许登录的,但我们现在需要登录或切换用户来进行配置)
$ sudo passwd git
输入新的 UNIX 密码:
重新输入新的 UNIX 密码:
passwd:已成功更新密码
SSH连接
之后我们需要配置服务端git用户的密钥认证登录。可参考我的另一篇博客《使用RSA Key代替密码进行ssh远程登录》
这里简单记录下操作代码提供参考,不再解释
$ ssh-keygen -t rsa -f ~/.ssh/git_rsa -C "xxx@org" -b 4096
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/xxx/.ssh/git_rsa.
Your public key has been saved in /home/xxx/.ssh/git_rsa.pub.
The key fingerprint is:
15:45:de:77:8e:19:34:97:64:3b:27:65:89:cf:47:ec xxx@org
The key's randomart image is:
+--[ RSA 4096]----+
| .oo =+*|
| o +.B+|
| . . *=+|
| . XE|
| S o o|
| |
| |
| |
| |
+-----------------+
$ ssh-copy-id -i ~/.ssh/git_rsa.pub git@localhost
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
git@localhost's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'git@localhost'"
and check to make sure that only the key(s) you wanted were added.
$ ssh git@localhost -t
Welcome to Ubuntu 14.04.1 LTS (GNU/Linux 3.13.0-32-generic x86_64)
* Documentation: https://help.ubuntu.com/
System information as of Sun Aug 17 10:54:39 CST 2014
System load: 0.08 Processes: 253
Usage of /home: 46.3% of 98.31GB Users logged in: 1
Memory usage: 21% IP address for wlan0: 192.168.0.103
Swap usage: 0%
Graph this data and manage this system at:
https://landscape.canonical.com/
0 packages can be updated.
0 updates are security updates.
Last login: Sun Aug 17 10:47:31 2014 from localhost
至此我们可以删除git用户的密码了。之后再需要增加用户密钥时,远程登录修改$HOME/.ssh/authorized_keys文件,增加对应公钥内容即可。
$ sudo passwd git -d
passwd:密码过期信息已更改。
GIT远程仓库
现在,用户和SSH都准备好了,如何提供服务呢?
这个简单,只要在git用户主目录下建立一个bare版本库就好了。
我们新建一个空仓库,你也可以从现有git仓库克隆
$ ssh git@localhost
$ git init --bare project.git
初始化空的 Git 版本库于 /home/git/project.git/
或者使用git clone --bare命令进行裸版本库克隆,注意不要使用--shared参数。
使用远程仓库
与使用github相似,不过只能使用git@server:repository.git的形式连接
$ git remote add origin git@localhost:project.git
$ git push origin master
Counting objects: 57, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (28/28), done.
Writing objects: 100% (57/57), 10.40 KiB | 0 bytes/s, done.
Total 57 (delta 9), reused 0 (delta 0)
To git@localhost:project.git
* [new branch] master -> master