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 条回复
fourstring
2016-08-09 19:41:44 +08:00
@larsenlouis 没有啊- -都是 pts ?
larsenlouis
2016-08-09 20:04:18 +08:00
@fourstring 贴一下本地的.ssh/config 和 服务器的 sshd_config
实在不知道是什么问题 这个时候就要 sudo apt remove openssh-server --purge && rm -rf ~/.ssh/,祭出此文,重装。 https://help.ubuntu.com/community/SSH/OpenSSH/Keys

客户端本地生成密匙, ssh-copy-id(不知道 macOS 有没有)上传 publickey ,再改本地.ssh/config ,再改服务器 sshd_config 关闭密码登录和禁止 root 登录,最后装上 fail2ban 。
fourstring
2016-08-09 20:07:02 +08:00
@larsenlouis sshd_config:
# Package generated configuration file
# See the sshd_config(5) manpage for details

# What ports, IPs and protocols we listen for
Port 63842
# Use these options to restrict which interfaces/protocols sshd will bind to
#ListenAddress ::
#ListenAddress 0.0.0.0
Protocol 2
# HostKeys for protocol version 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
#Privilege Separation is turned on for security
UsePrivilegeSeparation yes

# Lifetime and size of ephemeral version 1 server key
KeyRegenerationInterval 3600
ServerKeyBits 1024

# Logging
SyslogFacility AUTH
LogLevel INFO

# Authentication:
LoginGraceTime 120
PermitRootLogin yes
StrictModes yes

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

# Don't read the user's ~/.rhosts and ~/.shosts files
IgnoreRhosts yes
# For this to work you will also need host keys in /etc/ssh_known_hosts
RhostsRSAAuthentication no
# similar for protocol version 2
HostbasedAuthentication no
# Uncomment if you don't trust ~/.ssh/known_hosts for RhostsRSAAuthentication
#IgnoreUserKnownHosts yes

# To enable empty passwords, change to yes (NOT RECOMMENDED)
PermitEmptyPasswords no

# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication no

# Change to no to disable tunnelled clear text passwords
PasswordAuthentication no

# Kerberos options
#KerberosAuthentication no
#KerberosGetAFSToken no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes

# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes

X11Forwarding yes
X11DisplayOffset 10
PrintMotd no
PrintLastLog yes
TCPKeepAlive yes
#UseLogin no

#MaxStartups 10:30:60
#Banner /etc/issue.net

# Allow client to pass locale environment variables
AcceptEnv LANG LC_*

Subsystem sftp /usr/lib/openssh/sftp-server

# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication. Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
UsePAM yes

ssh config :
Host hexo
HostName xxx
Port 63842
User git
IdentityFile ~/.ssh/hexo
fourstring
2016-08-09 20:07:40 +08:00
@larsenlouis 另外由客户端生成密钥和服务端生成密钥都试过,均失败
abmin521
2016-08-09 20:15:28 +08:00
试一下 ssh localhost ?
fourstring
2016-08-09 20:20:46 +08:00
@abmin521 为什么要这么用。。。?是在服务器上么?
abmin521
2016-08-09 20:22:24 +08:00
@fourstring 不是服务器 要登陆 root 的机器 有些服务器可能有限制
abmin521
2016-08-09 20:22:49 +08:00
看错了 是服务器
hsyu53
2016-08-09 20:33:25 +08:00
印象中是在本地生产密钥,再将公钥拷贝到服务端
larsenlouis
2016-08-09 20:36:08 +08:00
@fourstring UserPAM 之后放 subsystem ,而且 subsystem 在配置最后,没有其他配置。应该是这个的问题,另外 sftp 组里有哪些用户?
fourstring
2016-08-09 20:44:57 +08:00
@larsenlouis 我刚刚用 VNC 连接,开启 sshd -d 看了一下 debug 信息,还请您看一下?(只有图片,没法复制出来- -)

另外按您说的那样改了,也没有用= =
fourstring
2016-08-09 20:50:42 +08:00
@larsenlouis ヾ(。`Д´。)!被这个问题坑了一下午,我有点想爆粗口了。。。
我在服务端又重新生成了一对密钥,然后把私钥下载回本地,一开始仍然失败。
当我把私钥名字从 hexo 改掉之后……连上了!连上了!神 TM 连上了……
abmin521
2016-08-09 21:22:50 +08:00
呵呵 上面的当我没说
winstars
2018-04-09 13:08:43 +08:00
@fourstring #32 同学,我也是一样的问题,私钥名改掉是什么意思?

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

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

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

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

© 2021 V2EX