如何在没有密码的情况下 ssh 到 localhost?

Posted

技术标签:

【中文标题】如何在没有密码的情况下 ssh 到 localhost?【英文标题】:How to ssh to localhost without password? 【发布时间】:2011-11-18 08:49:39 【问题描述】:

编辑:准确描述所做的事情

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

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

EDIT2:

添加一些关于 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

EDIT3:来自 $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: 

【问题讨论】:

交叉发布在超级用户:superuser.com/questions/336226/… 【参考方案1】:

RHEL8

就我而言,在成功配置密钥后,它仍然无法正常工作。我在 /var/log/secure 中发现了以下错误:

pam_access(sshd:account): access denied for user `username' from `::1'

所以我不得不编辑:

/etc/security/access.conf

并通过添加一行将“::1”添加到允许的主机:

+:<username>:LOCAL ::1

它立即开始工作,即使没有重新启动 sshd 服务。

【讨论】:

【参考方案2】:

如果你有一个已知良好的 ssh 配置,要仔细检查你的 /etc/hosts.allow 是否包含对 localhost 的引用,因为 localhost 连接的源 IP 来自 127.0.0.1 而不是你的网络知识产权。我对此感到困惑了一段时间,但在将以下内容添加到 /etc/hosts.allow 后,我的配置立即生效。

ALL: 127.0.0.1/32

我想我会添加这个,因为其他答案都没有提到它,这是我搜索相同错误时的热门搜索。

【讨论】:

【参考方案3】:

我解决了在 sshd_config 文件上设置 AllowUsers 的问题。

通过调试运行服务器:

$sshd -Dd

我发现我的用户不允许这样做

$sudo vi /etc/ssh/sshd_config

在#Authentication 之后添加一行:

允许用户我的用户

【讨论】:

【参考方案4】:

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

经过sshd -Ddssh -vv root@localhost调试,发现原因:

由于帐户被锁定,不允许使用 root 用户

所以,我们应该通过passwd -u解锁帐户 或设置密码。

【讨论】:

【参考方案5】:

在 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

【讨论】:

【参考方案6】:

我通过这种方式解决了 ssh 登录问题。

我在服务器端生成密钥对,然后通过 scp 将私钥返回到我的 Windows 10 计算机,现在我无需密码即可登录。

以前我使用的是我的 windows 10 笔记本电脑生成的密钥对,但一点运气都没有。

【讨论】:

【参考方案7】:

即使遵循了所有建议,我也遇到了同样的问题,但发现问题在于 gnome-keyring 干扰。

解决方案:

    去搜索,寻找“启动应用程序” 如果您看到“SSH 密钥代理”,请取消选中该框 重启机器并连接到本地主机。

【讨论】:

【参考方案8】:

我做了以下 3 个步骤来创建无密码登录

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 

【讨论】:

像魅力一样工作!谢谢 我可以在不使用第 3 步的情况下进行无密码 ssh 吗?我什么时候需要执行第 3 步? @Rich:这只是为了防止 @shipr mentions in his answer below 发生什么。如果您的 ~/.ssh/authorized_keys 已经存在并且具有正确的权限,则不需要 (3.),但也没有害处。 除了将id_rsa.pub 附加到authorized_keys(在此之前在我的Mac 上不存在)之外,我还将它附加到known_hosts。我仍然被要求输入 密码 好的,我明白了。对于那些因为已经有之前生成的rsa密钥对而感到困惑的人(像我一样),只需将现有id_rsa.pub文件的内容附加到~/.ssh/authorized_keys即可(不要生成新的)。如果authorized_keys 文件不存在,别担心,它会自动创建(您可能已经知道了),但更重要的是,将其附加到Mac 上的~/.ssh/known_hosts 文件将无济于事。如果您在iTerm 上使用ZSH,您可能需要重新启动终端【参考方案9】:

作为公认的答案, 如果你遇到了

的问题
    Agent admitted failure to sign using the key.

你需要

    ssh-add

【讨论】:

【参考方案10】:

正确且安全的方法是复制这里所说的密钥。

在其他情况下,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/

【讨论】:

【参考方案11】:

两个简单的步骤:

ssh-keygen -t rsa <Press enter for each line>
ssh-copy-id localhost

输入密码就完成了。

【讨论】:

【参考方案12】:

执行以下步骤

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 步中只需按 Enter)

# 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

【讨论】:

【参考方案13】:

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

chmod og-wx ~/.ssh/authorized_keys

【讨论】:

【参考方案14】:

已经发现问题了。

通过调试运行服务器:

$sshd -Dd

我发现它无法读取 auth_key

$chmod 750 $HOME

修复它。

【讨论】:

你们为什么不使用符号 chmod 模式?现在已经不是 90 年代了吧? 因为符号模式令人困惑。我不知道什么 750 会脱离我的脑海。类似'u=rwx,g=rx,o=' 不管它是什么,它并不比750容易!顺便说一句,755 次烫发也可以。 非常有用。我使用/usr/sbin/sshd -ddddD 并收到以下消息Authentication refused: bad ownership or modes for directory /home/... 发现也是这个。谢谢 :) 主目录修复了它。有关于不良所有权的类似调试消息。运行ssh -v localhost 后,我会看到调试日志。错误是bad ownership or modes for directory $HOME。我之前已经从其他用途中完成了上述步骤。

以上是关于如何在没有密码的情况下 ssh 到 localhost?的主要内容,如果未能解决你的问题,请参考以下文章

SSH是否允许用户在没有密码和密钥的情况下登录?

如何在没有提示的情况下执行 ssh-keygen

如何在没有服务器的情况下在本地测试我的 ssh 密钥

如何在不覆盖 TTY 的情况下将密码传递给 su/sudo/ssh?

如何使用私钥/公钥在不提供密码的情况下登录 ssh 服务器-imported-openssh-key

如何在没有 IDP 的情况下使用 ssh 将 xcode 应用程序复制到 iphone 设备