搭建Git服务器-Git详解(10)

在服务器上安装Git和SSH服务

安装git

1
$ sudo apt-get install git

安装ssh服务

1
$ sudo apt-get install openssh-server

在服务器上部署Git仓库

登录到服务器

1
2
3
4
5
6
$ ssh rb@192.168.1.3
$ mkdir repos
$ sudo addgroup git-rw
$ sudo chgrp -R git-rw repos
$ sudo useradd -M -g git-rw git-student #-M无home目录
$ sudo passwd git-student

服务器上的Git仓库必须是裸仓库。
假设已经有了仓库repo_a在本地,我们把repo_a转换成裸仓库

1
2
$ git clone --bare repo_a repo_a.git
$ scp -r repo_a.git git_teacher@192.168.1.3:/home/rb/repos

给组加上写权限

1
$ sudo chmod -R g+w repos/git_teacher.git

使用ssh公钥管理Git仓库

目标:使用不同的密钥,登录相同的git账户

1
2
3
4
5
$ sudo adduser git
$ su git
$ mkdir .ssh && chmod 700 .ssh
$ touch .ssh/authorized_keys
$ chmod 600 .ssh/authorized_keys

客户端生成密钥

1
2
$ ssh-keygen -t rsa
$ cat id_rsa.pub | ssh git@192.168.1.3 'cat - >> ~/.ssh/authorized_keys'

限定git用户权限,只能使用git shell

1
2
3
4
5
$ which git-shell
/usr/bin/git-shell
$ sudo vim /etc/shells #添加/usr/bin/git-shell
$ sudo chsh git #更改git用户使用的shell
#设置 Login Shell 为 /usr/bin/git-shell