Linux
SSH 公鑰認證被拒絕
我正在嘗試通過 SSH 連接到虛擬機。
我可以使用密碼登錄帳戶。
我創建了另一個沒有密碼的使用者,在我的主機(Windows)上生成了一個 ssh 密鑰,並將公鑰放在 user/.ssh/authorized_keys 中,並將權限設置為只讀,並擁有使用者的完全所有權。
但是,當我嘗試從 VM 上的 localhost 或從我的主機進行 ssh 時,公鑰總是被拒絕。我已將 ssh 伺服器配置為僅接受公鑰身份驗證。
這是我執行時遇到的錯誤
ssh -vv user@localhost -i privatekey
PS C:\Users\gicum\.ssh> ssh -vv mironica@192.168.230.129 -p 2222 -i mironica OpenSSH_for_Windows_7.7p1, LibreSSL 2.6.5 debug1: Reading configuration data C:\\Users\\gicum/.ssh/config debug1: C:\\Users\\gicum/.ssh/config line 1: Applying options for 192.168.230.129 debug2: resolve_canonicalize: hostname 192.168.230.129 is address debug2: ssh_connect_direct: needpriv 0 debug1: Connecting to 192.168.230.129 [192.168.230.129] port 2222. debug1: Connection established. debug1: identity file mironica.pub type 0 debug1: key_load_public: No such file or directory debug1: identity file mironica.pub-cert type -1 debug1: identity file C:\\Users\\gicum/.ssh/mironica type 0 debug1: key_load_public: No such file or directory debug1: identity file C:\\Users\\gicum/.ssh/mironica-cert type -1 debug1: Local version string SSH-2.0-OpenSSH_for_Windows_7.7 debug1: Remote protocol version 2.0, remote software version OpenSSH_7.6p1 Ubuntu-4ubuntu0.3 debug1: match: OpenSSH_7.6p1 Ubuntu-4ubuntu0.3 pat OpenSSH* compat 0x04000000 debug2: fd 3 setting O_NONBLOCK debug1: Authenticating to 192.168.230.129:2222 as 'mironica' debug1: SSH2_MSG_KEXINIT sent debug1: SSH2_MSG_KEXINIT received debug2: local client KEXINIT proposal debug2: KEX algorithms: curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha256,diffie-hellman-group14-sha1,ext-info-c debug2: host key algorithms: ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ssh-ed25519-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,ssh-ed25519,rsa-sha2-512,rsa-sha2-256,ssh-rsa debug2: ciphers ctos: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com debug2: ciphers stoc: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com debug2: MACs ctos: 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 debug2: MACs stoc: 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 debug2: compression ctos: none debug2: compression stoc: none debug2: languages ctos: debug2: languages stoc: debug2: first_kex_follows 0 debug2: reserved 0 debug2: peer server KEXINIT proposal debug2: KEX algorithms: curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha256,diffie-hellman-group14-sha1 debug2: host key algorithms: ssh-rsa,rsa-sha2-512,rsa-sha2-256,ecdsa-sha2-nistp256,ssh-ed25519 debug2: ciphers ctos: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com debug2: ciphers stoc: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com debug2: MACs ctos: 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 debug2: MACs stoc: 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 debug2: compression ctos: none,zlib@openssh.com debug2: compression stoc: none,zlib@openssh.com debug2: languages ctos: debug2: languages stoc: debug2: first_kex_follows 0 debug2: reserved 0 debug1: kex: algorithm: curve25519-sha256 debug1: kex: host key algorithm: ecdsa-sha2-nistp256 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: ecdsa-sha2-nistp256 SHA256:gS6dFKtHfhm8j4RkJ41ISAn0XS5KLrGlAD+QCHMp8qc debug1: checking without port identifier debug1: Host '192.168.230.129' is known and matches the ECDSA host key. debug1: Found key in C:\\Users\\gicum/.ssh/known_hosts:10 debug1: found matching key w/out port debug2: set_newkeys: mode 1 debug1: rekey after 134217728 blocks debug1: SSH2_MSG_NEWKEYS sent debug1: expecting SSH2_MSG_NEWKEYS debug1: SSH2_MSG_NEWKEYS received debug2: set_newkeys: mode 0 debug1: rekey after 134217728 blocks debug2: key: mironica.pub (00000266CA181500), explicit, agent debug2: key: C:\\Users\\gicum/.ssh/mironica (00000266CA181030), explicit debug1: SSH2_MSG_EXT_INFO received debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521> debug2: service_accept: ssh-userauth debug1: SSH2_MSG_SERVICE_ACCEPT received debug1: Authentications that can continue: publickey debug1: Next authentication method: publickey debug1: Offering public key: RSA SHA256:JbiX5u8cO0CxDtwgv258/TLUWyG/xh4XwTvGIQjdXrc mironica.pub debug2: we sent a publickey packet, wait for reply debug1: Authentications that can continue: publickey debug1: Offering public key: RSA SHA256:JbiX5u8cO0CxDtwgv258/TLUWyG/xh4XwTvGIQjdXrc C:\\Users\\gicum/.ssh/mironica 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. mironica@192.168.230.129: Permission denied (publickey).
如上所述,通過 localhost 嘗試時問題類似。我無法弄清楚我的問題可能在這裡?
公鑰
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDPPRMQ3Z+aYhluLi0iY9atBjliwNCvZb3FjywsbiVDYotuWrO8Ee/dRrCVaNF9fRU44JdMm9rZ1FH6HRbLejQiUpATiOxUIno6kKfa8GZjFwrf0Q80H0G2n8zqkZMdP4DbDI1PMLpNT60n/LWv1FOgw7dr/TRlNCzb/J4socm1kXKDRcE5B4xkh8uCuK9QevwMcJ3iqkZ9v3bxBI/N+VyBPE3WfBwzNKX3xZPB9BQirRSRyAffh3DLaEik4tMtDTmYqbpsLXcHnG9rUh1vrCxoB56RtphMR3D3ORNl0XBy3E5mXEo5jqvRMMG7kRyVv9jSnDPM6UiO+sF2vawjRZzouXZHZKLOsOycMbx/lX/jB4TGcLRs+BiRyfMEYvrbr5SzhFPikyn15lFFQSVl+rTnoW3xg05CpEqVTwZqoWPfEUar1UzbRex7QJN9BKqCFeoBH/jTaUvw+8a3T+ELV4co+PI3J7+RhdOsiZGFxJ5D20+Ik2BbBw0qMsFnhmprX+ltDP6D3hXd5vCj3EqK6blgjfQofsHdZsLGem6lxYCoF41FmNLJIR0S7luz30ewUw+3Haiks1ti0Vmgle6RNLyrddRkwWrO0pkyRkDA1Q5BOO34EHvGUXb/FKGrMr0ngccTBINY2G3AMeHOzpCSO/9BnK+YrikLjEj86TxqIB9gEQ== gicum@bosslife
/var/log/auth.log
Sep 15 14:03:45 ubuntu sshd[9423]: Received SIGHUP; restarting. Sep 15 14:03:45 ubuntu sshd[9423]: Server listening on 0.0.0.0 port 22. Sep 15 14:03:45 ubuntu sshd[9423]: Server listening on :: port 22. Sep 15 14:17:02 ubuntu CRON[11586]: pam_unix(cron:session): session opened for user root by (uid=0) Sep 15 14:17:02 ubuntu CRON[11586]: pam_unix(cron:session): session closed for user root Sep 15 14:24:49 ubuntu sshd[11884]: Connection reset by authenticating user mironica 192.168.230.1 port 50138 [preauth] Sep 15 14:25:16 ubuntu sshd[11904]: Connection reset by authenticating user mironica 192.168.230.1 port 50141 [preauth] Sep 15 14:26:47 ubuntu sshd[11955]: Connection reset by authenticating user mironica 192.168.230.1 port 50173 [preauth] Sep 15 14:27:01 ubuntu sshd[11965]: Connection reset by authenticating user mironica 192.168.230.1 port 50182 [preauth] Sep 15 14:27:20 ubuntu sshd[11980]: Connection reset by authenticating user mironica 192.168.230.1 port 50194 [preauth] Sep 15 14:42:48 ubuntu gdm-password]: gkr-pam: unlocked login keyring
發現了問題。
在 VM 上,我創建了使用者 /home/user 並將公鑰儲存在 ./ssh/authorized_keys 中。
我還確保我正確設置了權限:
1. sudo chmod 700 /home/user 2. sudo chmod 400 authorized_keys
但是,我從來沒有檢查過誰擁有這些權限,顯然,他們被分配給了 root。所以我做了以下事情:
1. sudo chown user:user /home/user 2. sudo chown user:user authorized_keys
您必須為要用於無密碼連接的使用者設置密碼,然後您可以設置公鑰認證。如果您能夠連接到遠端機器,您可以在調試模式下執行 ssh 來查找問題。
在遠端機器中
#ddd is debug mode level. $ stopsrc -s sshd $ /usr/sbin/sshd -ddd
然後嘗試從您的電腦連接到遠端電腦,當連接發佈時,消息將列印在您啟動 sshd 的遠端電腦終端上。