Ubuntu 搭建 Git 服务器后客户端公钥错误

mymbrooks 发布于 2017/03/24 13:48
阅读 235
收藏 0

我在 Ubuntu 16.04 上安装了 Git Server,具体步骤是:

1、更新服务器

apt-get update

apt-get dist-upgrade

 

2、生成服务器秘钥

ssh-keygen -t rsa

备份私钥、公钥,转换Windows可用格式

 

3、服务器禁用密码登录,客户端使用服务器私钥登录

vi /etc/ssh/sshd_config

PasswordAuthentication no //禁止使用基于口令认证的方式登陆

PubkeyAuthentication yes //允许使用基于密钥认证的方式登陆

 

4、服务器创建 git 用户、组

adduser --system --group git

 

5、添加 git 用户到 ssh 组

adduser git ssh

 

6、若不存在,创建

mkdir /home/git/.ssh

vi /home/git/.ssh/authorized_keys

 

7、设置权限

chmod 700 /home/git/.ssh

chmod 600 /home/git/.ssh/authorized_keys

chown -R git:git /home/git

 

8、客户端生成密钥

ssh-keygen -t rsa

备份私钥、公钥,转换Windows可用格式

 

9、追加客户端公钥至服务器

cat id_rsa.pub >> /home/git/.ssh/authorized_keys

 

10、初始化git仓库

mkdir /home/repos

git init --bare sample.git

chown -R git:git /home/repos

 

客户端使用 TortoiseGit clone 时出现错误:

服务器 Git 版本: 2.7.4

客户端 Git 版本:Git Windows:2.12.1、TortoiseGit:2.4.0.2

git 测试结果如下:

root@xxxxxx :/mnt/d/Repos# ssh -v git@xxxxxx
OpenSSH_7.2p2 Ubuntu-4ubuntu2.1, OpenSSL 1.0.2g  1 Mar 2016
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to xxxxxx [xxxxxx] port 22.
debug1: Connection established.
debug1: permanently_set_uid: 0/0
debug1: identity file /root/.ssh/id_rsa type 1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu2.1
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.2p2 Ubuntu-4ubuntu2.1
debug1: match: OpenSSH_7.2p2 Ubuntu-4ubuntu2.1 pat OpenSSH* compat 0x04000000
debug1: Authenticating to xxxxxx:22 as 'git'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256@libssh.org
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:tHNwm0NkVOwvQ7nAQ3ZgqyeNQKl3SbPOqjBXBSMY6xw
debug1: Host 'xxxxxx' is known and matches the ECDSA host key.
debug1: Found key in /root/.ssh/known_hosts:1
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<rsa-sha2-256,rsa-sha2-512>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /root/.ssh/id_rsa
debug1: Authentications that can continue: publickey
debug1: Trying private key: /root/.ssh/id_dsa
debug1: Trying private key: /root/.ssh/id_ecdsa
debug1: Trying private key: /root/.ssh/id_ed25519
debug1: No more authentication methods to try.
Permission denied (publickey).

加载中
0
mymbrooks
mymbrooks

已解决,属于权限问题,6、7 步骤应该切换到 git 用户操作,我之前都用了 root。

返回顶部
顶部