突然覺(jué)得服務(wù)器ssh密碼登錄總是浪費(fèi)一定量的時(shí)間,就想試試用sshKey進(jìn)行登錄普筹。
- 生成服務(wù)器sshkey和本地sshkey
$ ssh-keygen
- 在服務(wù)器上生成一個(gè)authorized_keys文件,然后將本地public Key 存到生成的文件中呻待。
- 設(shè)置 /etc/ssh/sshd.config
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
- 重啟ssh服務(wù)
service sshd restart
- 將~/.ssh 文件權(quán)限設(shè)置為其他用戶不可讀寫(xiě)薇组。也就是700、600郑兴、400權(quán)限
以上這樣全部設(shè)置完之后犀斋,發(fā)現(xiàn)還是無(wú)法ssh免密登錄,name是為什么呢情连,我打開(kāi)了openssh的官網(wǎng)開(kāi)始尋找答案叽粹。
- 我開(kāi)始尋找ssh日志,查找問(wèn)題所在
我們先看ssh客戶端的日志
ssh root@xxxx -v
執(zhí)行之后輸出以下日志
OpenSSH_7.5p1, LibreSSL 2.5.4
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 52: Applying options for *
debug1: Connecting to 121.42.42.155 [121.42.42.155] port 22.
debug1: Connection established.
debug1: identity file /Users/yard/.ssh/id_rsa type 1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/yard/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/yard/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/yard/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/yard/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/yard/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/yard/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/yard/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.5
debug1: Remote protocol version 1.99, remote software version OpenSSH_5.3
debug1: match: OpenSSH_5.3 pat OpenSSH_5* compat 0x0c000000
debug1: Authenticating to 121.42.42.155:22 as 'root'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: diffie-hellman-group-exchange-sha256
debug1: kex: host key algorithm: ssh-rsa
debug1: kex: server->client cipher: aes128-ctr MAC: umac-64@openssh.com compression: none
debug1: kex: client->server cipher: aes128-ctr MAC: umac-64@openssh.com compression: none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(2048<3072<8192) sent
debug1: got SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: got SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Server host key: ssh-rsa SHA256:tp4/4hwf0Q39NLrFmXNC438rpEPBEqy4C+CXCDy91xE
debug1: Host '121.42.42.155' is known and matches the RSA host key.
debug1: Found key in /Users/yard/.ssh/known_hosts:1
debug1: rekey after 4294967296 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 4294967296 blocks
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /Users/yard/.ssh/id_rsa
debug1: Authentications that can continue: publickey,password
debug1: Trying private key: /Users/yard/.ssh/id_dsa
debug1: Trying private key: /Users/yard/.ssh/id_ecdsa
debug1: Trying private key: /Users/yard/.ssh/id_ed25519
debug1: Next authentication method: password
我發(fā)現(xiàn)本機(jī)電腦提供了id_rsa文件去服務(wù)器認(rèn)證却舀,但是認(rèn)證沒(méi)有通過(guò)虫几。
- 我們?cè)賮?lái)看服務(wù)端的日志
我們重新開(kāi)一個(gè)ssh的服務(wù)器端口
/usr/sbin/sshd -d -p 2222
然后再通過(guò)本機(jī)ssh去連接服務(wù)器
ssh root@xxxx -p 2222 -v
以下是服務(wù)器sshd日志
debug1: Server will not fork when running in debugging mode.
debug1: rexec start in 4 out 4 newsock 4 pipe -1 sock 7
debug1: inetd sockets after dupping: 3, 3
Connection from 122.234.57.180 port 50213
debug1: Client protocol version 2.0; client software version OpenSSH_7.5
debug1: match: OpenSSH_7.5 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-1.99-OpenSSH_5.3
debug1: permanently_set_uid: 74/74
debug1: list_hostkey_types: ssh-rsa,ssh-dss
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: client->server aes128-ctr umac-64@openssh.com none
debug1: kex: server->client aes128-ctr umac-64@openssh.com none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST received
debug1: SSH2_MSG_KEX_DH_GEX_GROUP sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_INIT
debug1: SSH2_MSG_KEX_DH_GEX_REPLY sent
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: KEX done
debug1: userauth-request for user root service ssh-connection method none
debug1: attempt 0 failures 0
debug1: PAM: initializing for "root"
debug1: PAM: setting PAM_RHOST to "122.234.57.180"
debug1: PAM: setting PAM_TTY to "ssh"
debug1: userauth-request for user root service ssh-connection method publickey
debug1: attempt 1 failures 0
debug1: test whether pkalg/pkblob are acceptable
debug1: temporarily_use_uid: 0/0 (e=0/0)
debug1: trying public key file /root/.ssh/authorized_keys
debug1: fd 4 clearing O_NONBLOCK
Authentication refused: bad ownership or modes for directory /root
debug1: restore_uid: 0/0
debug1: temporarily_use_uid: 0/0 (e=0/0)
debug1: trying public key file /root/.ssh/authorized_keys
debug1: fd 4 clearing O_NONBLOCK
Authentication refused: bad ownership or modes for directory /root
debug1: restore_uid: 0/0
Failed publickey for root from 122.234.57.180 port 50213 ssh2
我發(fā)現(xiàn)在認(rèn)證的時(shí)候出現(xiàn)了這句話
Authentication refused: bad ownership or modes for directory /root
這個(gè)錯(cuò)誤提示的意識(shí)是/root文件夾的文件權(quán)限有問(wèn)題
在google上查找這個(gè)錯(cuò)誤提示后知道是因?yàn)閪/.ssh文件需要只供root讀寫(xiě),其他用戶都不可以用寫(xiě)的權(quán)限挽拔,然后導(dǎo)致/root/.ssh/authorized_keys無(wú)法讀取辆脸,也就導(dǎo)致了認(rèn)證不通過(guò)。
解決方法也很簡(jiǎn)單螃诅,給/root文件加上700權(quán)限就好了啡氢。
雖然寫(xiě)的很簡(jiǎn)單,但整個(gè)debug時(shí)間超過(guò)2小時(shí)术裸,主要是因?yàn)閛penssh軟件比較古老倘是,資料相對(duì)難找。袭艺。搀崭。開(kāi)始又懶得去看文檔,導(dǎo)致沒(méi)有找到正確的debug方式匹表,一直在查關(guān)于sshconfig的問(wèn)題门坷。 找到正確debug方式之后,解決起來(lái)就非撑鄱疲快默蚌。
PS:無(wú)論什么工具,debug一定要好好看文檔苇羡。绸吸。。。