如何在本地主机上使用ssh无需密码登录?

79

编辑:具体操作如下

我需要在不输入密码的情况下通过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:~$ 

正如您在最后一个命令中看到的那样,它仍然要求密码!我该怎么办?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

编辑3: 添加$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: 

在Super User上也发布了:https://superuser.com/questions/336226/how-to-ssh-to-localhost-without-password - Flux
14个回答

195
我采取了以下三个步骤来创建无需密码登录:
1. ssh-keygen -t rsa
Press enter for each line 
2. cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
3. chmod og-wx ~/.ssh/authorized_keys 

2
我可以在不使用第三步的情况下进行无密码ssh吗?我什么时候需要执行第三步呢? - notapatch
2
@Rich:这只是为了防止@shipr在下面的回答中提到的问题。如果您的~/.ssh/authorized_keys已经存在并且具有正确的权限,则不需要步骤(3.),但也不会造成任何损害。 - akavel
2
除了将id_rsa.pub附加到authorized_keys(这在我的Mac上之前不存在)之外,我还将其附加到known_hosts。但是我仍然被要求输入密码 - y2k-shubham
在macOS Mojave上,对于MacBook非常好用... +1 - Romeo Sierra
我所要做的就是执行 ssh-copy-id localhost 命令,然后它就可以工作了。 - Max
显示剩余3条评论

21

另一个可能的答案是:authorized_keys文件可能存在并且可读。但是如果该文件是组或全局可写的,则仍会提示输入密码。这个问题的解决方案是

chmod og-wx ~/.ssh/authorized_keys

21

已经发现了问题。

使用调试模式运行服务器:

$sshd -Dd

我发现无法读取auth_key

$chmod 750 $HOME

已解决。


3
为什么你们不使用符号化的 chmod 模式?现在已经不是90年代了,是吗? - Maxim Egorushkin
9
因为符号模式很令人困惑,我无法立即想出750代表的意思,类似于'u=rwx,g=rx,o='这样的东西。不管是什么,都不会比750更容易!顺便说一句,755权限也可以正常工作。 - Ian Kelling
1
非常有用。我使用了 /usr/sbin/sshd -ddddD 命令,并得到了以下信息:Authentication refused: bad ownership or modes for directory /home/... - Leonardo
家目录修复了它。有类似的调试消息关于错误的所有权。我运行 ssh -v localhost 后会看到调试日志。错误是 $HOME 目录的错误所有权或模式。我之前已经为其他用户执行了上述步骤。 - Joyoyoyoyoyo

10
两个简单的步骤:
ssh-keygen -t rsa <Press enter for each line>
ssh-copy-id localhost

输入密码即可完成。

6

请执行以下步骤:

ssh-keygen -t rsa -C "your_email@example.com"
# Creates a new ssh key, using the provided email as a label
# Generating public/private rsa key pair.

使用默认文件和空白密码(在接下来的2个步骤中只需按回车键即可)

# start the ssh-agent in the background
eval "$(ssh-agent -s)"
# Agent pid 59566
ssh-add 

将~/.ssh/id_rsa.pub的内容复制到~/.ssh/authorized_keys中。

请确保以下权限:

 ls -l .ssh/
 total 20
-rw-r--r--. 1 swati swati  399 May  5 14:53 authorized_keys
-rw-r--r--. 1 swati swati  761 Jan 12 15:59 config
-rw-------. 1 swati swati 1671 Jan 12 15:44 id_rsa
-rw-r--r--. 1 swati swati  399 Jan 12 15:44 id_rsa.pub
-rw-r--r--. 1 swati swati  410 Jan 12 15:46 known_hosts 

此外,请确保.ssh目录的权限设置正确。这一点非常重要。
drwx------.   2 swati swati    4096 May  5 14:56 .ssh

2
在Centos 7上
解决方案
1 create rsa key
2 vim /etc/ssh/ssh_config
3
#   IdentityFile ~/.ssh/identity
uncoment this line > IdentityFile ~/.ssh/id_rsa
#   IdentityFile ~/.ssh/id_dsa
#   IdentityFile ~/.ssh/id_ecdsa

请注意*,这是在复制密钥和其他答案之前完成的。但我非常确信这就是您需要做的全部内容,如果不是的话,我将把RSA密钥附加到authorized_keys中,并运行ssh-copy-id到用户名@localhost。

1
作为被接受的答案所说,如果遇到

问题,请执行以下操作:
    Agent admitted failure to sign using the key.

你需要:
    ssh-add

1

在Docker容器(golang:1.13-alpine)上运行单元测试时,我遇到了相同的问题。

经过sshd -Ddssh -vv root@localhost 调试,我找到了原因:

用户root不允许登录,因为账户被锁定

因此,我们需要通过passwd -u解锁帐户或设置密码。


1
正确且安全的方法是按照此处所说复制密钥。
在其他情况下,sshpass可能会很方便。
sshpass -p raspberry ssh pi@192.168.0.145

请注意,这并不安全。虽然在安全环境下使用它并不明智,但在脚本编写和自动化测试中却很有用...
这可以与其他内容结合使用。
ssh -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no pi@192.168.0.145

为了避免确认问题阻止脚本的运行。
再次强调,仅在开发系统中使用,其中不同的计算机共享一个IP并且安全性不重要。

https://ownyourbits.com/2017/02/22/easy-passwordless-ssh-with-sshh/


1
我是这样解决ssh登录问题的。
我在服务器端生成密钥对,然后将私钥scp回我的Windows 10电脑,现在我可以无需密码登录。
之前我使用了由我的Windows 10笔记本生成的密钥对,但完全没有成功。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接