如何在没有密码的情况下通过 SSH 连接到本地主机?

Can*_*sin 17 ssh passwords openssh user localhost

编辑:准确地放置所做的事情。

我需要在localhost没有密码的情况下通过SSH 连接,通常的做法(使用公钥)不起作用。

user@PC:~$ rm -rf .ssh/*
user@PC:~$ ssh-keygen -t rsa > /dev/null 
Enter file in which to save the key (/home/user/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
user@PC:~$ ls .ssh/
id_rsa  id_rsa.pub
user@PC:~$ ssh-copy-id -i localhost 
The authenticity of host 'localhost (::1)' can't be established.
RSA key fingerprint is f7:87:b5:4e:31:a1:72:11:8e:5f:d2:61:bd:b3:40:1a.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'localhost' (RSA) to the list of known hosts.
user@localhost's password: 
Now try logging into the machine, with "ssh 'localhost'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

user@PC:~$ ssh-agent $SHELL
user@PC:~$ ssh-add -L
The agent has no identities.
user@PC:~$ ssh-add 
Identity added: /home/user/.ssh/id_rsa (/home/user/.ssh/id_rsa)
user@PC:~$ ssh-add -L
ssh-rsa ...MY KEY HERE

user@PC:~$ ssh-copy-id -i localhost 
user@localhost's password: 
Now try logging into the machine, with "ssh 'localhost'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

user@PC:~$ ssh localhost echo 'testing'
user@localhost's password: 

user@PC:~$ 
Run Code Online (Sandbox Code Playgroud)

所以正如你在最后一条命令中看到的,它仍然在询问密码!!!我该如何解决?Ubuntu-10.04 , OpenSSH_5.3p1

编辑2:

添加一些关于 sshd 的信息

user@PC:~$ cat /etc/ssh/sshd_config | grep Authentication
# Authentication:
RSAAuthentication yes
PubkeyAuthentication yes
RhostsRSAAuthentication no
HostbasedAuthentication no
# Uncomment if you don't trust ~/.ssh/known_hosts for RhostsRSAAuthentication
ChallengeResponseAuthentication no
# PasswordAuthentication yes
#KerberosAuthentication no
#GSSAPIAuthentication no
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication.  Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
Run Code Online (Sandbox Code Playgroud)

EDIT3: Ading 结果来自 $ssh -vv localhost

$ssh -vv localhost
...
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Trying private key: /home/user/.ssh/identity
debug1: Offering public key: /home/user/.ssh/id_rsa
debug2: we sent a publickey packet, wait for reply
debug1: Authentications that can continue: publickey,password
debug1: Trying private key: /home/user/.ssh/id_dsa
debug2: we did not send a packet, disable method
debug1: Next authentication method: password
user@localhost's password: 
Run Code Online (Sandbox Code Playgroud)

编辑4:

只需检查文件是否相同并且 md5sum 是否一致

Tor*_*ian 10

首先,你应该明白你在做什么:

user@PC:~$ cat .ssh/id_rsa.pub | ssh localhost 'cat >> .ssh/authorized_keys'
Run Code Online (Sandbox Code Playgroud)

您正在.ssh/id_rsa.pub通过 ssh将公钥复制到同一台主机(即 localhost 是同一台主机)。如果您替换localhost为其他主机,那会更有意义(但如果您是为了学习如何操作而尝试此操作,那没关系)。

一旦您在远程主机(或您所在的同一台主机)上拥有公钥的副本,您必须确保将其用于身份验证,在您的实际主机中,调用ssh-agent/ ssh-add

$ eval `ssh-agent`
$ ssh-add
Run Code Online (Sandbox Code Playgroud)

然后,如果您提供了密码,系统会要求您在 之后输入ssh-add。如果您生成了没有密码的私钥,那么就是这样。


Can*_*sin 3

已经发现问题了。

运行服务器并进行调试:

$sshd -Dd
Run Code Online (Sandbox Code Playgroud)

我发现它无法读取auth_key

$chmod 750 $HOME
Run Code Online (Sandbox Code Playgroud)

修复。