Ubuntu14.04 普通用户无法通过 sshkey 登陆

2016-08-09 17:51:17 +08:00
 fourstring

如题。目前的基本情况是:

本地:

服务器:

我新建了 git 用户,然后在/home/git下建立.ssh目录,设置权限为git:git 0700,并在.ssh目录下运行ssh-keygen,生成公私钥,将公钥命名为authorized_keys,设置权限git:git 0600。将私钥复制回本地,本地建立 ssh 配置,并且授予私钥权限 0600 。

之后,尝试使用 ssh 命令连接服务器,连接被拒绝。

我做的几点排查:

RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile      %h/.ssh/authorized_keys

运行ssh hexo(配置中的 Host 名) -vvv信息如下:

OpenSSH_6.9p1, LibreSSL 2.1.8
debug1: Reading configuration data /Users/fourstring/.ssh/config
debug1: /Users/fourstring/.ssh/config line 1: Applying options for hexo
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 21: Applying options for *
debug2: ssh_connect: needpriv 0
debug1: Connecting to xxx port xxx.
debug1: Connection established.
debug1: identity file /Users/fourstring/.ssh/hexo type 1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/fourstring/.ssh/hexo-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.9
debug1: Remote protocol version 2.0, remote software version OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.6
debug1: match: OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.6 pat OpenSSH_6.6.1* compat 0x04000000
debug2: fd 3 setting O_NONBLOCK
debug1: Authenticating to xxx as 'git'
debug3: put_host_port: xxx
debug3: hostkeys_foreach: reading file "/Users/fourstring/.ssh/known_hosts"
debug3: record_hostkey: found key type ECDSA in file /Users/fourstring/.ssh/known_hosts:22
debug3: load_hostkeys: loaded 1 keys from xxx
debug3: order_hostkeyalgs: prefer hostkeyalgs: ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug2: kex_parse_kexinit: curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1
debug2: kex_parse_kexinit: ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,ssh-ed25519-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,ssh-dss-cert-v01@openssh.com,ssh-rsa-cert-v00@openssh.com,ssh-dss-cert-v00@openssh.com,ssh-ed25519,ssh-rsa,ssh-dss
debug2: kex_parse_kexinit: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com,arcfour256,arcfour128,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,rijndael-cbc@lysator.liu.se
debug2: kex_parse_kexinit: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com,arcfour256,arcfour128,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,rijndael-cbc@lysator.liu.se
debug2: kex_parse_kexinit: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1,hmac-md5-etm@openssh.com,hmac-ripemd160-etm@openssh.com,hmac-sha1-96-etm@openssh.com,hmac-md5-96-etm@openssh.com,hmac-md5,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
debug2: kex_parse_kexinit: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1,hmac-md5-etm@openssh.com,hmac-ripemd160-etm@openssh.com,hmac-sha1-96-etm@openssh.com,hmac-md5-96-etm@openssh.com,hmac-md5,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
debug2: kex_parse_kexinit: none,zlib@openssh.com,zlib
debug2: kex_parse_kexinit: none,zlib@openssh.com,zlib
debug2: kex_parse_kexinit:
debug2: kex_parse_kexinit:
debug2: kex_parse_kexinit: first_kex_follows 0
debug2: kex_parse_kexinit: reserved 0
debug2: kex_parse_kexinit: curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1
debug2: kex_parse_kexinit: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256,ssh-ed25519
debug2: kex_parse_kexinit: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-gcm@openssh.com,aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,rijndael-cbc@lysator.liu.se
debug2: kex_parse_kexinit: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-gcm@openssh.com,aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,rijndael-cbc@lysator.liu.se
debug2: kex_parse_kexinit: hmac-md5-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-ripemd160-etm@openssh.com,hmac-sha1-96-etm@openssh.com,hmac-md5-96-etm@openssh.com,hmac-md5,hmac-sha1,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
debug2: kex_parse_kexinit: hmac-md5-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-ripemd160-etm@openssh.com,hmac-sha1-96-etm@openssh.com,hmac-md5-96-etm@openssh.com,hmac-md5,hmac-sha1,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
debug2: kex_parse_kexinit: none,zlib@openssh.com
debug2: kex_parse_kexinit: none,zlib@openssh.com
debug2: kex_parse_kexinit:
debug2: kex_parse_kexinit:
debug2: kex_parse_kexinit: first_kex_follows 0
debug2: kex_parse_kexinit: reserved 0
debug1: kex: server->client chacha20-poly1305@openssh.com <implicit> none
debug1: kex: client->server chacha20-poly1305@openssh.com <implicit> none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:jGsXPrH3Cl2EWrBRzd4rr72jDeWKugYGPySGAMFL1mU
debug3: put_host_port: xxx
debug3: put_host_port: xxx
debug3: hostkeys_foreach: reading file "/Users/fourstring/.ssh/known_hosts"
debug3: record_hostkey: found key type ECDSA in file /Users/fourstring/.ssh/known_hosts:22
debug3: load_hostkeys: loaded 1 keys from xxx
debug3: hostkeys_foreach: reading file "/Users/fourstring/.ssh/known_hosts"
debug3: record_hostkey: found key type ECDSA in file /Users/fourstring/.ssh/known_hosts:22
debug3: load_hostkeys: loaded 1 keys from xxx
debug1: Host 'xxx' is known and matches the ECDSA host key.
debug1: Found key in /Users/fourstring/.ssh/known_hosts:22
debug2: set_newkeys: mode 1
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug2: set_newkeys: mode 0
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug2: service_accept: ssh-userauth
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug2: key: /Users/fourstring/.ssh/hexo (0x7f9f3b013d70), explicit
debug1: Authentications that can continue: publickey
debug3: start over, passed a different list publickey
debug3: preferred publickey,keyboard-interactive,password
debug3: authmethod_lookup publickey
debug3: remaining preferred: keyboard-interactive,password
debug3: authmethod_is_enabled publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /Users/fourstring/.ssh/hexo
debug3: send_pubkey_test
debug2: we sent a publickey packet, wait for reply
debug1: Authentications that can continue: publickey
debug2: we did not send a packet, disable method
debug1: No more authentication methods to try.
Permission denied (publickey).

最离奇的是以我自己的了解,我竟然没有从中看出任何连接被拒绝的信息。。。

另外,查找资料说本地用户名须和远程要登陆的用户名一致,但我的实际体验中并非如此。我的root用户就是本地生成sshkey上传到服务器,可以正常登陆。而且从我描述的流程中,可以看出我是在服务器上生成的key然后下载到本地的。

还望各位大神不吝赐教,感谢!

5694 次点击
所在节点    Linux
34 条回复
sopato
2016-08-09 18:09:32 +08:00
看看 /var/log/auth.log 输出了什么信息给你,根据那个错误信息来检查排错一下呗。
shenqi
2016-08-09 18:09:51 +08:00
不知道。围观下,坐等其他人的回答。

我觉得是露珠描述不清楚。
ipeony
2016-08-09 18:21:35 +08:00
private key 没加载好?正常是启个 agent ,然后 ssh-add 吧
finian
2016-08-09 18:34:16 +08:00
1. 远程 sshd 改完配置重启了吗?
2. 本地私钥是这个 /Users/fourstring/.ssh/hexo 对吗?
wweir
2016-08-09 19:05:23 +08:00
对 $HOME 目录的权限也有要求,验了么?
之前搞 docker 把 /root 目录共享了一下,也是 ssh 方面出了一大堆问题
fourstring
2016-08-09 19:11:36 +08:00
@sopato auth.log 没有任何错误……而且我还运行过 tail -f /var/run/auth.log ,在连接失败时该日志没有任何变化
fourstring
2016-08-09 19:11:54 +08:00
@shenqi 您觉得是哪里描述不清楚呢?请指出
crazycen
2016-08-09 19:12:26 +08:00
.ssh 目录不需要手动建立,还有,确认你生成 key 的方式是否正确。这是最基本功能,也不可能会出问题!
fourstring
2016-08-09 19:14:15 +08:00
@ipeony .ssh 目录下 config 文件是用 IdentifyFile 选项加载 privatekey 么?如果是的话我应该没有配置错。
另外和能正常登陆的 root 用户的-vvv 比较了一下,好像就只有 debug 信息的最后几行,正常的是
debug2: we sent a publickey packet, wait for reply
之后就收到 auth succeed 的回复然后进入 shell
fourstring
2016-08-09 19:14:57 +08:00
@finian 实际上我没有改过配置,那个配置是默认就有的。我也尝试过重启 sshd ,没有效果。私钥是这个,没有出错。
fourstring
2016-08-09 19:16:06 +08:00
@wweir 我用的是 adduser 命令添加用户,自动建立了主目录,刚刚又去检查了一下,$HOME 的权限是正确的
fourstring
2016-08-09 19:16:40 +08:00
@crazycen 请问.ssh 目录不手动建立该如何建立呢?另外生成 key 的方式是不是运行 ssh-keygen 命令?
crazycen
2016-08-09 19:20:51 +08:00
larsenlouis
2016-08-09 19:21:11 +08:00
普通用户能用密码登录吗?普通用户的 Home 加密了?如果普通用户 home 目录加密,本机没有登录的情况下, Home 目录没有挂载,只能密码登录。而 root 用户的 home 目录不在加密的目录里。
oxoxoxox
2016-08-09 19:27:10 +08:00
远端 server 的 authorized_keys 权限设置为 644 试试看呢?
fourstring
2016-08-09 19:29:17 +08:00
@larsenlouis 密码可以登录。我用 adduser 命令添加的用户,应该不会自动加密吧? root 是可以正常访问 git 用户的$home 目录的
fourstring
2016-08-09 19:31:41 +08:00
@oxoxoxox 这个刚刚尝试了一下,貌似并没有什么用。。。/var/run/auth.log 里也没有报权限错误
larsenlouis
2016-08-09 19:33:05 +08:00
@fourstring 加密 home 是安装 ubuntu 系统时选的,你看看 /etc/fstab 有没有 encryptfs 的载点, root 可以正常访问时用 ssh 还是服务器本地,当时服务器本地有没有普通用户登录?
fourstring
2016-08-09 19:36:08 +08:00
@larsenlouis 对,安装 ubuntu 的时候确实会送这个。问题是我是 VPS 啊。。。 root 用 ssh 可以正常访问的。我都进不去服务器本地- -
larsenlouis
2016-08-09 19:38:54 +08:00
@fourstring 有些供应商会在虚拟机的控制台上模拟本地终端,你用普通用户登录后用`w`命令看看有没有 tty 的 session

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/298181

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX