Ubuntu

ssh 共享密鑰認證要求輸入密碼,除非 sshd 使用 -d 選項執行

  • August 14, 2014

我的客戶端是 Ubuntu,伺服器是 CentOS。我已將共享密鑰身份驗證設置為從客戶端登錄到伺服器。當我像這樣執行伺服器時:

sudo `which sshd` -d

一切都按預期工作;我沒有收到使用者登錄提示。我認為這意味著一切都在正確的位置,具有正確的權限等。當我以“正常”模式重新啟動伺服器時:

sudo service sshd restart

我的客戶提示我輸入使用者密碼。

這是成功的詳細輸出(sshd -d):

anregen@ba-vm-atf-01:~/workspace$ ssh -v web@test.lab.network.com
OpenSSH_6.0p1 (CentrifyDC build 5.1.1-814) (CentrifyDC build 5.1.1-814), OpenSSL 0.9.8w 23 Apr 2012
debug1: Reading configuration data /etc/centrifydc/ssh/ssh_config
debug1: /etc/centrifydc/ssh/ssh_config line 52: Applying options for *
debug1: Connecting to test.lab.network.com [172.20.80.45] port 22.
debug1: Connection established.
debug1: identity file /home/MyNetwork.local/anregen/.ssh/id_rsa type 1
debug1: identity file /home/MyNetwork.local/anregen/.ssh/id_rsa-cert type -1
debug1: identity file /home/MyNetwork.local/anregen/.ssh/id_dsa type -1
debug1: identity file /home/MyNetwork.local/anregen/.ssh/id_dsa-cert type -1
debug1: identity file /home/MyNetwork.local/anregen/.ssh/id_ecdsa type -1
debug1: identity file /home/MyNetwork.local/anregen/.ssh/id_ecdsa-cert type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.3
debug1: match: OpenSSH_5.3 pat OpenSSH_5*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.0
debug1: Miscellaneous failure
No credentials cache found

debug1: Miscellaneous failure
No credentials cache found

debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Server host key: RSA 1e:57:9c:69:d2:09:29:0b:5b:4d:e4:f7:98:ee:f5:94
debug1: Host 'test.lab.network.com' is known and matches the RSA host key.
debug1: Found key in /home/MyNetwork.local/anregen/.ssh/known_hosts:7
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Next authentication method: gssapi-keyex
debug1: No valid Key exchange context
debug1: Next authentication method: gssapi-with-mic
debug1: Miscellaneous failure
No credentials cache found

debug1: Miscellaneous failure
No credentials cache found

debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/MyNetwork.local/anregen/.ssh/id_rsa
debug1: Server accepts key: pkalg ssh-rsa blen 279
debug1: read PEM private key done: type RSA
debug1: Authentication succeeded (publickey).
Authenticated to test.lab.network.com ([172.20.80.45]:22).
debug1: channel 0: new [client-session]
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
Last login: Tue Jun 17 16:03:39 2014 from ba-vm-atf-01.lab.network.com
Environment:
 USER=web
 LOGNAME=web
 HOME=/var/www
 PATH=/usr/local/bin:/bin:/usr/bin
 MAIL=/var/mail/web
 SHELL=/bin/bash
 SSH_CLIENT=172.20.80.27 32973 22
 SSH_CONNECTION=172.20.80.27 32973 172.20.80.45 22
 SSH_TTY=/dev/pts/1
 TERM=xterm
 SELINUX_ROLE_REQUESTED=
 SELINUX_LEVEL_REQUESTED=
 SELINUX_USE_CURRENT_RANGE=
-bash-4.1$

這是“失敗”的詳細輸出sudo service sshd restart

anregen@ba-vm-atf-01:~/workspace$ ssh -v web@test.lab.network.com
OpenSSH_6.0p1 (CentrifyDC build 5.1.1-814) (CentrifyDC build 5.1.1-814), OpenSSL 0.9.8w 23 Apr 2012
debug1: Reading configuration data /etc/centrifydc/ssh/ssh_config
debug1: /etc/centrifydc/ssh/ssh_config line 52: Applying options for *
debug1: Connecting to test.lab.network.com [172.20.80.45] port 22.
debug1: Connection established.
debug1: identity file /home/MyNetwork.local/anregen/.ssh/id_rsa type 1
debug1: identity file /home/MyNetwork.local/anregen/.ssh/id_rsa-cert type -1
debug1: identity file /home/MyNetwork.local/anregen/.ssh/id_dsa type -1
debug1: identity file /home/MyNetwork.local/anregen/.ssh/id_dsa-cert type -1
debug1: identity file /home/MyNetwork.local/anregen/.ssh/id_ecdsa type -1
debug1: identity file /home/MyNetwork.local/anregen/.ssh/id_ecdsa-cert type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.3
debug1: match: OpenSSH_5.3 pat OpenSSH_5*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.0
debug1: Miscellaneous failure
No credentials cache found

debug1: Miscellaneous failure
No credentials cache found

debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Server host key: RSA 1e:57:9c:69:d2:09:29:0b:5b:4d:e4:f7:98:ee:f5:94
debug1: Host 'test.lab.network.com' is known and matches the RSA host key.
debug1: Found key in /home/MyNetwork.local/anregen/.ssh/known_hosts:7
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Next authentication method: gssapi-keyex
debug1: No valid Key exchange context
debug1: Next authentication method: gssapi-with-mic
debug1: Miscellaneous failure
No credentials cache found

debug1: Miscellaneous failure
No credentials cache found

debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/MyNetwork.local/anregen/.ssh/id_rsa
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Trying private key: /home/MyNetwork.local/anregen/.ssh/id_dsa
debug1: Trying private key: /home/MyNetwork.local/anregen/.ssh/id_ecdsa
debug1: Next authentication method: password
web@test.lab.network.com's password:

有什麼想法-d可以使這項工作有效嗎?還是在工具之外執行 sshd service

-d編輯:根據要求,這裡是帶有選項的伺服器端日誌。我添加#comments以顯示遠端/客戶端活動。

[anregen@ba-vm-web-01 ~]$ clear
[anregen@ba-vm-web-01 ~]$ sudo service sshd stop
Stopping sshd:                                             [  OK  ]
[anregen@ba-vm-web-01 ~]$ sudo `which sshd` -d
debug1: sshd version OpenSSH_5.3p1
debug1: read PEM private key done: type RSA
debug1: private host key: #0 type 1 RSA
debug1: read PEM private key done: type DSA
debug1: private host key: #1 type 2 DSA
debug1: rexec_argv[0]='/usr/sbin/sshd'
debug1: rexec_argv[1]='-d'
Set /proc/self/oom_score_adj from 0 to -1000
debug1: Bind to port 22 on 0.0.0.0.
Server listening on 0.0.0.0 port 22.
debug1: Bind to port 22 on ::.
Server listening on :: port 22.

# At this point, I log in from remote as "web"
debug1: Server will not fork when running in debugging mode.
debug1: rexec start in 5 out 5 newsock 5 pipe -1 sock 8
debug1: inetd sockets after dupping: 3, 3
Connection from 172.20.80.27 port 37828
debug1: Client protocol version 2.0; client software version OpenSSH_6.0
debug1: match: OpenSSH_6.0 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-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 hmac-md5 none
debug1: kex: server->client aes128-ctr hmac-md5 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 web service ssh-connection method none
debug1: attempt 0 failures 0
debug1: PAM: initializing for "web"
debug1: PAM: setting PAM_RHOST to "ba-vm-atf-01.lab.network.com"
debug1: PAM: setting PAM_TTY to "ssh"
debug1: userauth-request for user web service ssh-connection method publickey
debug1: attempt 1 failures 0
debug1: test whether pkalg/pkblob are acceptable
debug1: temporarily_use_uid: 500/500 (e=0/0)
debug1: trying public key file /var/www/.ssh/authorized_keys
debug1: fd 4 clearing O_NONBLOCK
debug1: matching key found: file /var/www/.ssh/authorized_keys, line 1
Found matching RSA key: 6b:b4:a1:41:e2:7c:0a:c6:2b:6d:27:87:f4:3c:6e:0c
debug1: restore_uid: 0/0
Postponed publickey for web from 172.20.80.27 port 37828 ssh2
debug1: userauth-request for user web service ssh-connection method publickey
debug1: attempt 2 failures 0
debug1: temporarily_use_uid: 500/500 (e=0/0)
debug1: trying public key file /var/www/.ssh/authorized_keys
debug1: fd 4 clearing O_NONBLOCK
debug1: matching key found: file /var/www/.ssh/authorized_keys, line 1
Found matching RSA key: 6b:b4:a1:41:e2:7c:0a:c6:2b:6d:27:87:f4:3c:6e:0c
debug1: restore_uid: 0/0
debug1: ssh_rsa_verify: signature correct
debug1: do_pam_account: called
Accepted publickey for web from 172.20.80.27 port 37828 ssh2
debug1: monitor_child_preauth: web has been authenticated by privileged process
debug1: temporarily_use_uid: 500/500 (e=0/0)
debug1: ssh_gssapi_storecreds: Not a GSSAPI mechanism
debug1: restore_uid: 0/0
debug1: SELinux support enabled
debug1: PAM: establishing credentials
debug1: temporarily_use_uid: 500/500 (e=0/0)
debug1: ssh_gssapi_storecreds: Not a GSSAPI mechanism
debug1: restore_uid: 0/0
User child is on pid 19221
debug1: PAM: establishing credentials
debug1: permanently_set_uid: 500/500
debug1: Entering interactive session for SSH2.
debug1: server_init_dispatch_20
debug1: server_input_channel_open: ctype session rchan 0 win 1048576 max 16384
debug1: input_session_request
debug1: channel 0: new [server-session]
debug1: session_new: session 0
debug1: session_open: channel 0
debug1: session_open: session 0: link with channel 0
debug1: server_input_channel_open: confirm session
debug1: server_input_global_request: rtype no-more-sessions@openssh.com want_reply 0
debug1: server_input_channel_req: channel 0 request pty-req reply 1
debug1: session_by_channel: session 0 channel 0
debug1: session_input_channel_req: session 0 req pty-req
debug1: Allocating pty.
debug1: session_new: session 0
debug1: session_pty_req: session 0 alloc /dev/pts/1
debug1: server_input_channel_req: channel 0 request shell reply 1
debug1: session_by_channel: session 0 channel 0
debug1: session_input_channel_req: session 0 req shell
debug1: Setting controlling tty using TIOCSCTTY.

# at this point I send "exit" from remote to logout
debug1: Received SIGCHLD.
debug1: session_by_pid: pid 19222
debug1: session_exit_message: session 0 channel 0 pid 19222
debug1: session_exit_message: release channel 0
debug1: session_by_tty: session 0 tty /dev/pts/1
debug1: session_pty_cleanup: session 0 release /dev/pts/1
Received disconnect from 172.20.80.27: 11: disconnected by user
debug1: do_cleanup
debug1: do_cleanup
debug1: PAM: cleanup
debug1: PAM: closing session
debug1: PAM: deleting credentials

TLDR;SELinux

我回去在兩台機器上使用相同的使用者帳戶。更重要的是,這意味著我使用的是 SELinux 自帶的主目錄。您可以在日誌中看到“web”使用者具有非標準主目錄/var/www/(它旨在成為 web 內容管理帳戶)

然後我刪除了~/.ssh/伺服器機器上的目錄,回到客戶端並使用ssh-copy-id. 這顯然以~/.ssh/與 SELinux 兼容的方式重新創建了目錄和身份驗證文件。請注意,僅僅為“web”使用者刪除並重新創建 .ssh 目錄是不夠的。似乎當我更改“web”使用者主目錄時,SELinux 沒有正確參與……

-d對於為什麼在伺服器端更改此行為,我沒有答案,但如果其他人遇到此問題,請嘗試刪除~.ssh/目錄,並讓ssh-copy-id腳本為您完成工作。

更新顯然,因為執行它sudo會更改程序的安全權限。所以,即使 aps看起來一樣,我發現ps -Z(這是 SELinux-context-aware 參數)不一樣。

在這一點上剩下的唯一事情是研究如何以/var/wwwSELinux 批准的方式創建具有主目錄的使用者,以便它尊重該帳戶authorized_keys文件

我用的更新chcon -R --reference=/home/anregen/.ssh /var/www/.ssh。這就是將正確的安全上下文放入使用者重新定位的“主”目錄的技巧:web. 請記住,這是暫時的,這意味著您不應只使用 chcon,而應制定新策略。

引用自:https://serverfault.com/questions/605803