准备工作
服务器上安装git,(git version 1.7.12.4)
新增git用户
$ useradd -d /home/git git
$ su - git
根据官网提示修改git用户登录shell:https://git-scm.com/docs/git-shell。
目的在于禁止git用户通过ssh登录。
$ chsh -s /usr/bin/git-shell
$ mkdir $HOME/git-shell-commands
$ cat >$HOME/git-shell-commands/no-interactive-login <<\EOF
#!/bin/sh
printf '%s\n' "Hi $USER! You've successfully authenticated, but I do not"
printf '%s\n' "provide interactive shell access."
exit 128
EOF
$ chmod +x $HOME/git-shell-commands/no-interactive-login
如果chsh -s报错:
$ chsh -s /usr/bin/git-shell
Changing shell for git.
chsh: "/usr/bin/git-shell" is not listed in /etc/shells.
chsh: Use -l option to see list.
使用root账户进入/etc/passwd修改git账户后面的登录shell
/home/git:/bin/bash
修改为
/home/git:/usr/bin/git-shell
创建SSH KEY
$ ssh-keygen -t rsa -C "your@email.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/savepath/.ssh/id_rsa): /savepath/.ssh/test
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /savepath/.ssh/test.
Your public key has been saved in /savepath/.ssh/test.pub.
The key fingerprint is:
SHA256:s+pP6bI+hOAxGISlhDoobOQAXcyhqSzFCdBFMNCN4+s your@email.com
The key's randomart image is:
+---[RSA 2048]----+
|@O+X=. |
|*=*=+ |
|O+*. |
|B*= |
|=+ = . S |
|. o . . + |
| . . + |
| E o+ |
| o==o |
+----[SHA256]-----+
在~/.ssh
目录下有生成的公钥id_rsa.pub和私钥id_rsa。
将公钥导入ssh认证文件
$ cat ~/.ssh/id_rsa.pub > ~/.ssh/authorized_keys
文件权限设置:(必须设置,否则会提示输入密码,而不是ssh认证)
$ chmod 600 ~/.ssh/authorized_keys
$ chmod 700 ~/.ssh/
使用服务器上用户测试ssh连接是否OK,登录服务器上另一个别的用户然后执行如下命令:
$ ssh git@localhost
Last login: Wed Nov 2 18:24:48 2016 from 127.0.0.1
Welcome to Service!
git>
测试连接成功。如果这里连接报错,或者让输入密码,那就是上面的步骤设置错误了。文件权限一定要记得设置。
但是这里连接并没有输出上面no-interactive-login里面的
printf '%s\n' "Hi $USER! You've successfully authenticated, but I do not"
printf '%s\n' "provide interactive shell access."
而且有一个命令行
git>
这里不知道什么原因,使用root编辑/etc/passwd文件,将git后面的登录shell修改
/home/git:/usr/bin/git-shell
// 上面的修改为
/home/git:/home/git/git-shell-commands/no-interactive-login
然后再次测试ssh命令:
$ ssh git@localhost
Last login: Thu Nov 3 18:14:51 2016 from ip
Welcome to Service!
Hi git! You've successfully authenticated, but I do not
provide interactive shell access.
Connection to localhost closed.
现在ssh连接已经输出shell命令里面设置的输出语句,并且直接关闭连接,没有这个命令行
git>
这个估计是因为git版本的原因,/usr/bin/git-shell这个文件的问题。
创建git项目
切换到项目仓库路径
$ cd /repertory/git/
$ git init --bare sample.git
Initialized empty Git repository in /repertory/git/sample.git/
客户端配置私钥
将上面生成SSH key生成的私钥内容配置到客户端~/.ssh/keyname
中。
譬如window系统中,C:\Users\Administrator\.ssh
,这个文件中新建一个私钥文件sample,然后将上面的私钥内容放入其中。然后如果客户端是多私钥配置的就要再去配置下~/.ssh/config
文件。
配置完成之后测试:
$ ssh git@host
Last login: Wed Nov 2 19:55:06 2016 from 127.0.0.1
Welcome to aliyun Elastic Compute Service!
git>
测试ssh连接成功。
之后测试clone命令:
$ git clone host:/repertory/git/sample.git
Cloning into 'sample'...
warning: You appear to have cloned an empty repository.
Checking connectivity... done.
$ ls
lumen/ sample/ testapps/
可以看到sample项目已经clone到客户端了。