問(wèn)題描述
- 環(huán)境:ubuntu 20.04
- 問(wèn)題描述:在已經(jīng)設(shè)置免密登錄后,ssh登錄時(shí)(例如
ssh localhost
)還需要輸入密碼,并且輸入密碼后出現(xiàn)下面一系列的報(bào)錯(cuò):
xxx(用戶名)@localhost's password:
Permission denied, please try again.
xxx@localhost's password:
Permission denied, please try again.
xxx@localhost's password:
xxx@localhost: Permission denied (publickey,password).
問(wèn)題排查
首先使用命令ssh localhost -v
查看登錄時(shí)的日志:
OpenSSH_8.2p1 Ubuntu-4ubuntu0.5, OpenSSL 1.1.1f 31 Mar 2020
debug1: Reading configuration data /home/yhy/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files
debug1: /etc/ssh/ssh_config line 21: Applying options for *
debug1: Connecting to localhost [127.0.0.1] port 22.
debug1: Connection established.
debug1: identity file /home/yhy/.ssh/id_rsa type 0
debug1: identity file /home/yhy/.ssh/id_rsa-cert type -1
debug1: identity file /home/yhy/.ssh/id_dsa type 1
debug1: identity file /home/yhy/.ssh/id_dsa-cert type -1
debug1: identity file /home/yhy/.ssh/id_ecdsa type -1
debug1: identity file /home/yhy/.ssh/id_ecdsa-cert type -1
debug1: identity file /home/yhy/.ssh/id_ecdsa_sk type -1
debug1: identity file /home/yhy/.ssh/id_ecdsa_sk-cert type -1
debug1: identity file /home/yhy/.ssh/id_ed25519 type -1
debug1: identity file /home/yhy/.ssh/id_ed25519-cert type -1
debug1: identity file /home/yhy/.ssh/id_ed25519_sk type -1
debug1: identity file /home/yhy/.ssh/id_ed25519_sk-cert type -1
debug1: identity file /home/yhy/.ssh/id_xmss type -1
debug1: identity file /home/yhy/.ssh/id_xmss-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.2p1 Ubuntu-4ubuntu0.5
debug1: Remote protocol version 2.0, remote software version OpenSSH_8.2p1 Ubuntu-4ubuntu0.5
debug1: match: OpenSSH_8.2p1 Ubuntu-4ubuntu0.5 pat OpenSSH* compat 0x04000000
debug1: Authenticating to localhost:22 as 'yhy'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ssh-ed25519
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: ssh-ed25519 SHA256:KgNVsJSzDmK9KHR57LTY2Xarsui27l0FIm9CY2fUO6o
debug1: Host 'localhost' is known and matches the ED25519 host key.
debug1: Found key in /home/yhy/.ssh/known_hosts:1
debug1: rekey out after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey in after 134217728 blocks
debug1: Will attempt key: /home/yhy/.ssh/id_dsa DSA SHA256:G/HRSEJY5SzAo4ejVp+GVHkIp+UjzMTETklcP2P0ym4 agent
debug1: Will attempt key: /home/yhy/.ssh/id_rsa RSA SHA256:ysy/bD0PakYR49V2kxCs95GxfpHjLifTAPqNJ5yH480 agent
debug1: Will attempt key: /home/yhy/.ssh/id_ecdsa
debug1: Will attempt key: /home/yhy/.ssh/id_ecdsa_sk
debug1: Will attempt key: /home/yhy/.ssh/id_ed25519
debug1: Will attempt key: /home/yhy/.ssh/id_ed25519_sk
debug1: Will attempt key: /home/yhy/.ssh/id_xmss
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,sk-ssh-ed25519@openssh.com,ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,sk-ecdsa-sha2-nistp256@openssh.com>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Offering public key: /home/yhy/.ssh/id_dsa DSA SHA256:G/HRSEJY5SzAo4ejVp+GVHkIp+UjzMTETklcP2P0ym4 agent
debug1: Authentications that can continue: publickey,password
debug1: Offering public key: /home/yhy/.ssh/id_rsa RSA SHA256:ysy/bD0PakYR49V2kxCs95GxfpHjLifTAPqNJ5yH480 agent
debug1: Authentications that can continue: publickey,password
debug1: Trying private key: /home/yhy/.ssh/id_ecdsa
debug1: Trying private key: /home/yhy/.ssh/id_ecdsa_sk
debug1: Trying private key: /home/yhy/.ssh/id_ed25519
debug1: Trying private key: /home/yhy/.ssh/id_ed25519_sk
debug1: Trying private key: /home/yhy/.ssh/id_xmss
debug1: Next authentication method: password
從日志上看,應(yīng)該還是配置的問(wèn)題。
可能的原因
- 配置問(wèn)題:如果是ssh配置的問(wèn)題首先可以嘗試這里修改配置。(如果之前從未修改過(guò)相關(guān)配置,大概率不是這里的問(wèn)題)
進(jìn)行上面的方法后,如果還不行,說(shuō)明不是配置文件的問(wèn)題。 - 文件權(quán)限問(wèn)題:有時(shí)文件權(quán)限也可能會(huì)導(dǎo)致登錄失敗,按照上述方法查看日志后,如果日志中出現(xiàn)有關(guān)文件權(quán)限的錯(cuò)誤
badownership
,那么就要考慮.ssh
文件夾和authorized_keys
文件的權(quán)限問(wèn)題了。
進(jìn)入.ssh
文件夾下,使用如下命令查看文件夾的信息:
該目錄必須具有上面所示的讀(r)、寫(w)、執(zhí)行(x) 權(quán)限,如果沒(méi)有則需要使用命令:ll -ld drwx------ 3 xxx xxx 4096 5月 3 08:41 ./
修改權(quán)限;chmod 0700 /home/your_home/.ssh
使用命令:
查看文件/home/xxx/.ssh$ ls -ld authorized_keys -rw------- 1 yhy yhy 564 5月 3 07:08 authorized_keys
authorized_keys
的文件權(quán)限。該文件必須有上面所示的讀寫(rw) 權(quán)限,否則使用命令:
修改權(quán)限。chmod 0600 /home/[username]/.ssh/authorized_keys
- zsh問(wèn)題:如果上述方法都不奏效,且你正在/曾經(jīng)使用
zsh
那么恭喜你可能遇到了和我相同的問(wèn)題。這個(gè)問(wèn)題可以通過(guò)命令
查看ssh服務(wù)運(yùn)行狀態(tài)發(fā)現(xiàn),如果運(yùn)行日志中有:service sshd status
那么就是由于User xxx not allowed because shell /bin/zsh
zsh
使用問(wèn)題導(dǎo)致。我的問(wèn)題是之前使用過(guò)zsh
,卸載后忘記修改默認(rèn)終端,導(dǎo)致ssh出現(xiàn)問(wèn)題。
首先查看系統(tǒng)可用終端:
$ cat /etc/shells
# /etc/shells: valid login shells
/bin/sh
/bin/bash
/usr/bin/bash
/bin/rbash
/usr/bin/rbash
/bin/dash
/usr/bin/dash
/bin/zsh
/usr/bin/zsh
可以看到由于之前設(shè)置過(guò)該文件,所以里面有bin/zsh
和usr/bin/zsh
,如果zsh已經(jīng)卸載的話,可以刪除后面這兩行。
通過(guò)命令sudo nautilus
可以以管理員身份打開(kāi)文件夾,并且任意對(duì)里面的文件進(jìn)行修改。
接下來(lái)查看系統(tǒng)默認(rèn)shell
:
$ echo $SHELL
/bin/zsh
可以看到還是因?yàn)橹暗脑O(shè)置,默認(rèn)shell
是zsh
。通過(guò)命令:
grep xxx(你的用戶名) /etc/passwd #這條命令顯示/etc/passwd中用戶的終端設(shè)置
sudo chsh --shell /bin/bash xxx #這條命令修改用戶的默認(rèn)終端為/bin/bash
grep xxx /etc/passwd #再次查看,默認(rèn)終端已經(jīng)變成/bin/bash
在用戶登錄時(shí),系統(tǒng)會(huì)根據(jù)/etc/passwd
中的設(shè)置為用戶設(shè)置默認(rèn)終端,所以執(zhí)行完上述操作后重啟,再次檢查默認(rèn)終端:
echo $SHELL
/bin/bash
會(huì)發(fā)現(xiàn)終端已經(jīng)改成了/bin/bash
。
這時(shí)候再試一試ssh localhost
,會(huì)發(fā)現(xiàn)登錄成功:
問(wèn)題得到解決。
如果上面的方法都不能解決,就要根據(jù)日志和服務(wù)狀態(tài)信息中的其它內(nèi)容進(jìn)行排查了。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-489784.html
總結(jié)
這個(gè)ssh
登錄失敗問(wèn)題困擾了我很長(zhǎng)時(shí)間,試了網(wǎng)上很多種方法都沒(méi)有奏效,最后查看系統(tǒng)服務(wù)日志才發(fā)現(xiàn)問(wèn)題所在。以后刪除某個(gè)軟件的時(shí)候,一定要把相關(guān)的配置還原成為默認(rèn)的,否則不知道哪天會(huì)出問(wèn)題!文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-489784.html
到了這里,關(guān)于ssh登錄出現(xiàn)Permission denied, please try again可能的解決方案的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!