在前面的文章中,我给大家介绍了如何自己架设一个git
服务器并建立一个名为git
的用户来进行所有的git
操作:
但是我们会发现,git
用户是可以通过ssh
进行远程连接我们的服务器的,这将有一定的安全隐患。
那么在这篇文章中,我们会介绍如何限制git
用户,禁止其使用ssh
登陆进行操作。
具体办法就是将git
用户的shell
换成git-shell
这个工具:
# see if `git-shell` is already in there (检查`git-shell`是否已经在shells文件中)
$ cat /etc/shells
# make sure git-shell is installed on your system (如果没有,确定git-shell已经在你的系统里安装)
$ which git-shell
# and add the path to git-shell from last command (将上一条命令返回的路径添加进shells)
$ sudo vim /etc/shells
我们看到,首先我们需要找到git-shell
的安装位置,如果没有被添加在/etc/shells
中,我们需要手动将其添加进去(通常git-shell
路径为/usr/bin/git-shell
)。
接下来,我们就可以把默认的shell
替换成git-shell
了:
$ sudo chsh git -s $(which git-shell)
现在,git
用户就只能进行push
和pull
操作,而不能用ssh
来连接服务器了:
$ ssh git@gitserver
fatal: Interactive git shell is not enabled.
hint: ~/git-shell-commands should exist and have read and execute access.
Connection to gitserver closed.