在 Ubuntu 系统中部署 Git Server
虽然有很多开源的Git仓库,不过并非所有都尽人意,譬如Github,Gitlab等,不是服务器不稳定,就是强制开源,又或者有文件大小,项目数量等方面的限制,我们认为Git服务提供商设置的这些限制是合理的,
是无可指责的,但从实际出发,这些免费仓库并不一定都符合所有开发者的需求。或许对于个人开发者或者小团队开发组来说,这些开源仓库还是非常有用的,至少在减少成本方面有不可取代的地位,但也正如
我之前所说,并非每一个开发者都认同这些,幸好作为一个优秀的CVS(control version system),除了具备分布式开发的特点外,还兼具有传统CVS的功能。
也就是说,我们可以在本地创建Git Server,用于保存和维护我们自己的代码。在您继续阅读以下内容前,需要说明的是,这些内容是基于Linux发行版中最流行之一的Ubuntu,当然也可以用于Debian操作系统,不过除此之外的其他Linux系统,甚至是Unix系统或者是Windows系统,作为使用者的您可能需要参考相关文献及文档,有些命令可能不一样,不过总体思路应该是一样的。
开始前的准备工作
首先,你需要更新你的包文件,包括更新你的系统,Git或者某些组件可能需要一些新的特性以支持,不过在Ubuntu系统中,这一步相当简单,你只需要在Terminal中输入
- sudo apt-get update
完成这一步后,我们就可以安装 git core 组件,通常情况下,我们安装在系统中的 git 只是一个简单的客户端,包括一些最基本的命令以及特性,而作为保管代码的仓库,则还需要其他特性支持,所以我们需要安装 git core,如果你之前已经安装了 git,那么仍然需要安装 git core
- sudo apt-get install git-core
完成这两步之后,我们就可以开始配置 Git Server 了。
增加 developers 组以及创建保存项目的仓库
我们需要创建一个新的用户组,并且将创建的仓库都放置在名为 git 的用户下,当然 git 要隶属于 developers 组,这样也是为了方便将来的管理需要。
- sudo groupadd developers
- cd /home/
- sudo mkdir git
- sudo useradd git -d /home/git
以上四个命令分别为我们创建 developers 用户组, 并在 /home/ 文件夹下为名为 git 的用户创建工作目录,创建名为 git 的用户账户并将它的工作目录指向 /home/git。
之后,我们需要将增加的用户添加到 developers 用户组中,这一步一般通过修改 /etc/group 文件达成,当然在正式修改 /etc/group 文件前或许需要先做一个备份。
找到你创建的用户组,譬如这里的例子中创建的 developers。你或许能在文件中看到类似的一行:
- developers:x:1003:
将刚才创建的用户 git,添加到developers中,当然你也可以添加其他的“开发者”,就像这样
- developers:x:1003:git,tom,francklin,james
创建代码仓库,并修改权限
现在,让我们回到创建好的工作目录 /home/git/,你可能需要通过命令 cd 来完成将工作路径由 /etc/ 切换为 /home/git/
然后让我们在 git 下创建一个新的仓库
- sudo mkdir yourproject.git
- sudo chgrp developers yourproject.git
- sudo chmod g+rws yourproject.git
我们需要将创建的文件夹的所有人由 root 修改为 developers,这样当我们在本地上传代码至仓库时就不会遇到权限不够的问题。其次我们需要将仓库的所有权修改,你也可以用“777”修改仓库的权限,不过不建议你这样做。
- sudo git init —bare —shared yourproject.git
在正式上传代码至仓库前,还需要对仓库进行初始化,请注意这里和平常使用方式的不同,—bare 指定创建的仓库为公共仓库,—shared 将使其他人都有权限将他们的代码提交至这个仓库中。
SSH
代码的上传方式主要为SSH,假如我们不希望每次上传代码都需要输入密码时,我们就需要在 git 下创建一个 .ssh 的目录,并且将客户机的 ssh pub Key 保存至 authorized_keys 文件中。
- sudo mkdir .ssh
- touch ./.ssh/authorized_keys
- cat id_rsa.pub >> ./.ssh/authorized_keys
上传代码
完成这些后,就可以上传你的代码至本地 Git server上了,不过在此之前,你仍然需要
- git remote add origin git@x.x.x.x:/home/git/yourproject.git
- git push origin master
可能出现的 Error
- 如果出现了类似的错误
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: POSSIBLE DNS SPOOFING DETECTED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
The RSA host key for **.net has changed,
and the key for the according IP address xx.xx.xxx.xxx
is unknown. This could either mean that
DNS SPOOFING is happening or the IP address for the host
and its host key have changed at the same time.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that the RSA host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx.
Please contact your system administrator.
Add correct host key in /Users/user/.ssh/known_hosts to get rid of this message.
Offending key in /Users/user/.ssh/known_hosts:5
RSA host key for **.net has changed and you have requested strict
checking.
Host key verification failed.
处理方法是,删除客户端的 .ssh/known_hosts 文件。
___________________________________________________________________________
http://git-scm.com/book/en/Git-on-the-Server-Setting-Up-the-Server
4.4 Git on the Server - Setting Up the Server
Setting Up the Server
Let’s walk through setting up SSH access on the server side. In this example, you’ll use theauthorized_keys
method for authenticating your users. We also assume you’re running a standard Linux distribution like Ubuntu. First, you create a 'git' user and a .ssh
directory for that user.
$ sudo adduser git
$ su git
$ cd
$ mkdir .ssh
Next, you need to add some developer SSH public keys to the authorized_keys
file for that user. Let’s assume you’ve received a few keys by e-mail and saved them to temporary files. Again, the public keys look something like this:
$ cat /tmp/id_rsa.john.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCB007n/ww+ouN4gSLKssMxXnBOvf9LGt4L
ojG6rs6hPB09j9R/T17/x4lhJA0F3FR1rP6kYBRsWj2aThGw6HXLm9/5zytK6Ztg3RPKK+4k
Yjh6541NYsnEAZuXz0jTTyAUfrtU3Z5E003C4oxOj6H0rfIF1kKI9MAQLMdpGW1GYEIgS9Ez
Sdfd8AcCIicTDWbqLAcU4UpkaX8KyGlLwsNuuGztobF8m72ALC/nLF6JLtPofwFBlgc+myiv
O7TCUSBdLQlgMVOFq1I2uPWQOkOWQAHukEOmfjy2jctxSDBQ220ymjaNsHT4kgtZg2AYYgPq
dAv8JggJICUvax2T9va5 gsg-keypair
You just append them to your authorized_keys
file:
$ 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
Now, you can set up an empty repository for them by running git init
with the --bare
option, which initializes the repository without a working directory:
$ cd /opt/git
$ mkdir project.git
$ cd project.git
$ git --bare init
Then, John, Josie, or Jessica can push the first version of their project into that repository by adding it as a remote and pushing up a branch. Note that someone must shell onto the machine and create a bare repository every time you want to add a project. Let’s use gitserver
as the hostname of the server on which you’ve set up your 'git' user and repository. If you’re running it internally, and you set up DNS forgitserver
to point to that server, then you can use the commands pretty much as is:
# on Johns computer
$ 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
At this point, the others can clone it down and push changes back up just as easily:
$ git clone git@gitserver:/opt/git/project.git
$ cd project
$ vim README
$ git commit -am 'fix for the README file'
$ git push origin master
With this method, you can quickly get a read/write Git server up and running for a handful of developers.
As an extra precaution, you can easily restrict the 'git' user to only doing Git activities with a limited shell tool called git-shell
that comes with Git. If you set this as your 'git' user’s login shell, then the 'git' user can’t have normal shell access to your server. To use this, specify git-shell
instead of bash or csh for your user’s login shell. To do so, you’ll likely have to edit your /etc/passwd
file:
$ sudo vim /etc/passwd
At the bottom, you should find a line that looks something like this:
git:x:1000:1000::/home/git:/bin/sh
Change /bin/sh
to /usr/bin/git-shell
(or run which git-shell
to see where it’s installed). The line should look something like this:
git:x:1000:1000::/home/git:/usr/bin/git-shell
Now, the 'git' user can only use the SSH connection to push and pull Git repositories and can’t shell onto the machine. If you try, you’ll see a login rejection like this:
$ ssh git@gitserver
fatal: What do you think I am? A shell?
Connection to gitserver closed.