我需要自动化登录服务器的脚本。我制作了一个 RSA 密钥并将其上传到服务器的authorized_keys 文件:
desktop$ ssh-keygen -f ~/.ssh/id_rsa_marge -P '' -t rsa
desktop$ chmod 600 ~/.ssh/id_*
desktop$ scp ~/.ssh/id_rsa_marge.pub [email protected]:~/
password:
desktop$ ssh [email protected]
password:
marge$ cat ~/id_rsa_marge.pub >> ~/.ssh/authorized_keys
marge$ whoami
me
marge$ ls -la | grep ssh
drw------- 2 me me 4096 May 29 16:02 .ssh
marge$ ls -la .ssh
total 16
drwx------ 2 me me 4096 May 29 16:02 .
drwx--x--x. 9 me me 4096 Aug 11 05:32 ..
-rw------- 1 me me 398 Aug 11 04:14 authorized_keys
-rw-r--r-- 1 me me 414 May 29 16:02 known_hosts
marge$ cat .ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCzUvV0SMF+H7kwmEkJzXY1RqionS6DTtJiON3+9L6neKMaFb/FXc3UtK7oN/lsxNcHwv9s9GG4xp2UiUNk8NeT+/aFCjl7sMpDsW1+bn7sjhRFFw095qxvK52somSUKXlm9DP1pr0wj9r9Kxdph3HynxRsMkdPnWK2LVGhHzr3nh39mJ/3Fwaejl+sYnO8wgPfZbqZTA1xubhIQilv0XI8YAWUOM+Mn2QmyI12geTVKdD393wL0cDrkit/MXjMY6pimSFafrFJkaWV1BI4YOnkPRPo8oyoM4ywXhcCeb14JkOmKdSacUwSDsnCy/IDP51rcwItZrXxBiOHFZJW1Nlp dotancohen@desktop
marge$ exit
desktop$ cat .ssh/id_rsa_marge.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCzUvV0SMF+H7kwmEkJzXY1RqionS6DTtJiON3+9L6neKMaFb/FXc3UtK7oN/lsxNcHwv9s9GG4xp2UiUNk8NeT+/aFCjl7sMpDsW1+bn7sjhRFFw095qxvK52somSUKXlm9DP1pr0wj9r9Kxdph3HynxRsMkdPnWK2LVGhHzr3nh39mJ/3Fwaejl+sYnO8wgPfZbqZTA1xubhIQilv0XI8YAWUOM+Mn2QmyI12geTVKdD393wL0cDrkit/MXjMY6pimSFafrFJkaWV1BI4YOnkPRPo8oyoM4ywXhcCeb14JkOmKdSacUwSDsnCy/IDP51rcwItZrXxBiOHFZJW1Nlp dotancohen@desktop
desktop$ ls -la | grep .ssh
-rw------- 1 dotancohen dotancohen 689 Aug 12 11:03 .lesshst
drwx------ 3 dotancohen dotancohen 4096 Aug 12 11:05 .ssh
desktop$ ls -la .ssh | grep canary
-rw------- 1 dotancohen dotancohen 1675 Aug 11 12:13 id_rsa_marge
-rw------- 1 dotancohen dotancohen 398 Aug 11 12:13 id_rsa_marge.pub
desktop$ grep -A 5 marge .ssh/config
Host marge
User me
HostName 2.4.6.8
IdentityFile ~/.ssh/id_rsa_marge.pub
IdentitiesOnly yes
但是,当我尝试登录服务器时,它要求输入密码:
desktop$ ssh -v marge
OpenSSH_5.3p1, OpenSSL 1.0.0-fips 29 Mar 2010
debug1: Reading configuration data /home/desktop/.ssh/config
debug1: Applying options for marge
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug1: Connecting to 2.4.6.8 [2.4.6.8] port 22.
debug1: Connection established.
debug1: identity file /home/desktop/.ssh/id_rsa_marge.pub type 1
debug1: identity file /home/desktop/.ssh/id_rsa_marge.pub-cert type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.9p1 Debian-5ubuntu1.3
debug1: match: OpenSSH_5.9p1 Debian-5ubuntu1.3 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.3
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: Host '2.4.6.8' is known and matches the RSA host key.
debug1: Found key in /home/desktop/.ssh/known_hosts:2
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering public key: /home/desktop/.ssh/id_rsa_marge.pub
debug1: Server accepts key: pkalg ssh-rsa blen 277
debug1: PEM_read_PrivateKey failed
debug1: read PEM private key done: type <unknown>
Enter passphrase for key '/home/desktop/.ssh/id_rsa_marge.pub':
debug1: No more authentication methods to try.
Permission denied (publickey).
可以看出,该密钥没有密码。此外,从不同的桌面我可以使用此密钥而无需输入密码!为什么这台服务器要求我输入密码,而实际上没有密码?
答案1
Enter passphrase for key '/home/desktop/.ssh/id_rsa_marge.pub':
公钥文件的密码 ( .pub
)?
IdentityFile ~/.ssh/id_rsa_marge.pub
身份文件是客户端上的一个文件,可让您向服务器证明您可以登录。这是一个包含秘密值的文件。这是私钥文件,而不是公钥文件。
将此行更改为
IdentityFile ~/.ssh/id_rsa_marge
确保您~/.ssh/authorized_keys
在服务器上添加的内容(最好将其传递到ssh-copy-id
)是公钥文件 ( .pub
)。
答案2
您没有使用文件的默认键位置。 ssh 客户端不知道从哪里加载您的私钥(它只查找.ssh/id_dsa
和.ssh/id_rsa
),因此它根本不使用生成的密钥。也许您有另一个对同一主机有效的密钥,但带有密码?
尝试跑步。ssh -i .ssh/id_rsa_marge -v [email protected]