511

我将公共 SSH 密钥添加到了authorized_keys文件中。 ssh localhost应该让我登录而不要求输入密码。

我这样做并尝试输入ssh localhost,但它仍然要求我输入密码。我必须通过其他设置才能使其正常工作吗?

我已按照更改权限的说明进行操作:

如果我这样做,下面是结果ssh -v localhost

debug1: Reading configuration data /home/john/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug1: Connecting to localhost [127.0.0.1] port 22.
debug1: Connection established.
debug1: identity file /home/john/.ssh/identity type 1
debug1: identity file /home/john/.ssh/id_rsa type -1
debug1: identity file /home/john/.ssh/id_dsa type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_4.7p1 Debian-8ubuntu3
debug1: match: OpenSSH_4.7p1 Debian-8ubuntu3 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_4.7p1 Debian-8ubuntu3
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-cbc hmac-md5 none
debug1: kex: client->server aes128-cbc 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 'localhost' is known and matches the RSA host key.
debug1: Found key in /home/john/.ssh/known_hosts:12
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,password
debug1: Next authentication method: publickey
debug1: Offering public key: /home/john/.ssh/identity
debug1: Server accepts key: pkalg ssh-rsa blen 149
debug1: PEM_read_PrivateKey failed
debug1: read PEM private key done: type <unknown>

然后它在上述日志之后要求一个 passphase。为什么没有密码就不能登录?

4

30 回答 30

1238

您需要验证authorized_keys文件及其所在文件夹/父文件夹的权限。

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

有关详细信息,请参阅此页面

您可能还需要更改/验证主目录的权限以删除组和其他人的写访问权限。

chmod go-w ~
于 2011-06-16T18:55:02.133 回答
169

SELinux也可能导致 authorized_keys 无法工作。特别是对于CentOS 6 和 7中的 root 。虽然没有任何必要禁用它。

一旦你确认你的权限是正确的,你可以像这样解决这个问题:

chmod 700 /root/.ssh
chmod 600 /root/.ssh/authorized_keys
restorecon -R -v /root/.ssh
于 2014-02-07T19:42:56.597 回答
118

设置ssh authorized_keys似乎很简单,但它隐藏了一些我试图弄清楚的陷阱。

- 服务器 -

/etc/ssh/sshd_config中,设置passwordAuthentication yes让服务器暂时接受密码认证

- 客户 -

Cygwin视为 Linux 仿真并安装和运行 OpenSSH

1.生成私钥和公钥(客户端) # ssh-keygen

在这里按下 just Enter,您会在~/.ssh/中获得默认的两个文件,“ id_rsa ”和“ id_rsa.pub ”,但如果您提供name_for_the_key,则生成的文件将保存在您当前的工作目录中。

2.your_key.pub文件传输到目标机器,ssh-copy-id user_name@host_name

如果您没有创建默认密钥,这是出错的第一步......您应该使用:

ssh-copy-id -i path/to/key_name.pub user_name@host_name

3.日志记录ssh user_name@host_name仅适用于默认的id_rsa文件,因此这里是第二个陷阱。你需要做ssh -i path/to/key_name user@host

(使用ssh -v ...选项查看发生了什么。)

如果服务器仍然要求输入密码,那么您提供了一些东西。输入密码:当您创建密钥时(这很正常)。

如果 ssh 未在默认端口 22 上侦听,则必须使用ssh -p port_nr.

- 服务器 - - -

4.修改文件/etc/ssh/sshd_config

RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile  %h/.ssh/authorized_keys

(如果情况,请取消注释)

这告诉 ssh 接受文件authorized_keys并在用户主目录中查找.ssh/authorized_keys文件中写入的key_name字符串。

5在目标机器上设置权限

chmod 755 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

同时关闭通过身份验证,

passwordAuthentication no

关闭所有 ssh root/admin/....@your_domain 尝试的大门。

6.确保所有非根主目录的所有权和组所有权是适当的。

chown -R ~ usernamehere
chgrp -R ~/.ssh/ user

================================================

7.考虑优秀的ht​​tp ://www.fail2ban.org

8.额外 的SSH隧道来访问 MySQL (bind = 127.0.0.1) 服务器

于 2014-07-13T15:59:24.730 回答
40

还要确保您的主目录不能被其他人写入:

chmod g-w,o-w /home/USERNAME

这个答案是从这里偷来的。

于 2014-03-06T13:36:55.100 回答
15

由于从混乱的终端复制文件id_rsa.pub的文本,绝望的人还可以确保他们在authorized_keys文件中没有额外的换行符。

于 2014-10-30T03:39:08.867 回答
8

在.ssh/authorized_keys中列出公钥是必要的,但不足以让 sshd(服务器)接受它。如果您的私钥受密码保护,则每次都需要向 ssh(客户端)提供密码。或者,您可以使用ssh-agent或 GNOME 等价物。

更新的跟踪与受密码保护的私钥一致。请参阅 ssh-agent,或使用ssh-keygen -p.

于 2011-06-16T18:54:02.010 回答
8

在下面,user是您的用户名。

mkdir -p /home/user/.ssh
ssh-keygen -t rsa
touch /home/user/.ssh/authorized_keys
touch /home/user/.ssh/known_hosts
chown -R user:user /home/user/.ssh
chmod 700 /home/user/.ssh
chmod 600 /home/user/.ssh/id*
chmod 644 /home/user/.ssh/id*.pub
chmod 644 /home/user/.ssh/authorized_keys
chmod 644 /home/user/.ssh/known_hosts
于 2015-11-14T11:55:47.663 回答
7

Beware that SELinux can trigger this error as well, even if all permissions seem to be OK. Disabling it did the trick for me (insert usual disclaimers about disabling it).

于 2013-10-18T09:39:49.177 回答
6

/var/log/auth.log在服务器上的文件中查找sshd身份验证错误。

如果一切都失败了,那么sshd在调试模式下运行服务器:

sudo /usr/sbin/sshd -ddd -p 2200

然后从客户端连接:

ssh user@host -p 2200

就我而言,我在最后发现了错误部分:

    debug1: userauth_pubkey: test whether pkalg/pkblob are acceptable for RSA SHA256:6bL+waAtghY5BOaY9i+pIX9wHJHvY4r/mOh2YaL9RvQ [preauth]
==> debug2: userauth_pubkey: disabled because of invalid user [preauth]
    debug2: userauth_pubkey: authenticated 0 pkalg ssh-rsa [preauth]
    debug3: userauth_finish: failure partial=0 next methods="publickey,password" [preauth]
    debug3: send packet: type 51 [preauth]
    debug3: receive packet: type 50 [preauth]

有了这些信息,我意识到我的sshd_config文件限制了ssh组成员的登录。以下命令修复了此权限错误:

sudo usermod -a -G ssh NEW_USER
于 2018-10-21T21:11:56.230 回答
5

尝试对我有用的“ssh-add”。

于 2014-03-05T00:42:39.703 回答
5

最终对我有用的是确保所有者/组不是root,而是user

chown -R ~/.ssh/ user
chgrp -R ~/.ssh/ user
于 2014-11-08T09:37:55.557 回答
4

在命令行上发出这些:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

完成此操作后,请确保您的目录如下所示:

drwx------ 2 lab lab 4.0K Mar 13 08:33 .
drwx------ 8 lab lab 4.0K Mar 13 08:07 ..
-rw------- 1 lab lab  436 Mar 13 08:33 authorized_keys
-rw------- 1 lab lab 1.7K Mar 13 07:35 id_rsa
-rw-r--r-- 1 lab lab  413 Mar 13 07:35 id_rsa.pub
于 2014-03-13T08:42:19.827 回答
3

您必须注意的另一个问题:如果您生成的文件名不是默认的id_rsa并且id_rsa.pub.

您必须创建.ssh/config文件并手动定义要用于连接的id文件。

这里有一个例子:

Host remote_host_name
    HostName 172.xx.xx.xx
    User my_user
    IdentityFile /home/my_user/.ssh/my_user_custom
于 2017-02-02T09:41:59.480 回答
3

另一个要记住的提示:从 v7.0 开始,OpenSSH 默认禁用 DSS/DSA SSH 密钥,因为它们继承了弱点。因此,如果您有 OpenSSH v7.0+,请确保您的密钥不是ssh-dss.

sshd_config如果你被 DSA 密钥卡住了,你可以通过更新你的和~/.ssh/config文件来在本地重新启用支持,如下所示:PubkeyAcceptedKeyTypes=+ssh-dss

于 2016-04-29T12:04:12.320 回答
3

就我而言,我需要将authorized_keys文件放入.openssh.

此位置/etc/ssh/sshd_config在选项下指定AuthorizedKeysFile %h/.ssh/authorized_keys

于 2017-02-22T18:00:53.293 回答
2

确保目标用户设置了密码。运行passwd username设置一个。即使禁用了密码 SSH 登录,这对我来说也是必需的。

于 2014-11-15T06:17:55.640 回答
2

只需查看服务器上的文件/var/log/auth.log 即可在客户端使用-vv设置额外的详细信息无济于事,因为服务器不太可能向可能的攻击者提供太多信息。

于 2018-01-19T16:19:33.377 回答
1

我从先前的答案中发出sudo chmod 700 ~/.sshchmod 600 ~/.ssh/authorized_keyschmod go-w $HOME $HOME/.ssh它解决了我在尝试使Samba共享工作时弄乱了权限的CentOS 7 机器上的问题。

于 2015-05-01T00:53:12.590 回答
1

It seems like a permission problem. Usually it happens if the permission of some file/directory is not correctly set up. In most case they are ~/.ssh and ~/.ssh/*. In my case they are /home/xxx.

You can change the log level of sshd by modifying file /etc/ssh/sshd_config(search for LogLevel, and set it to DEBUG) and then check the output in file /var/log/auth.log to see what happened exactly.

于 2015-05-08T01:09:59.390 回答
1

这解决了我的问题:

ssh-agent bash

ssh-add
于 2015-06-21T14:57:57.503 回答
1

我的问题是修改后的 AuthorizedKeysFile,当自动填充 /etc/ssh/authorized_keys 尚未运行时。

$sudo grep AuthorizedKeysFile /etc/ssh/sshd_config
#AuthorizedKeysFile .ssh/authorized_keys
AuthorizedKeysFile  /etc/ssh/authorized_keys/%u
于 2016-03-28T19:07:21.680 回答
1

确保您已将整个公钥复制到authorized_keys; ssh rsa前缀是密钥工作所必需的。

于 2018-01-09T08:15:46.193 回答
1

You need to verify the properties of the files.

To assign the required property, use:

$ chmod 600 ~/.ssh/sshKey
$ chmod 644 ~/.ssh/sshKey.pub
于 2018-05-03T23:29:59.430 回答
0

请注意,确保您的sshd配置具有以下行:

PermitRootLogin without-password

如上设置,然后重启sshd ( /etc/init.d/sshd restart)。

注销并尝试再次登录!

我相信默认设置是:

PermitRootLogin no
于 2015-02-03T09:59:15.937 回答
0

从以前开始我就遇到了同样的问题,但是今天我不得不设置一台新服务器。这段时间能学到什么...

允许无密码身份验证的基本过程如下:

  1. 在服务器上,验证您的主文件夹是否有该.ssh文件夹。如果它不存在,您可以使用mkdir命令手动创建它,然后使用 分配正确的权限chmod,或者您可以使用相同的实用程序 ,ssh-keygen创建私钥/公钥,但在服务器上为您的用户创建。此过程将创建所需的.ssh文件夹。

  2. 在本地机器上ssh-keygen,您还需要使用该实用程序创建私钥/公钥。

  3. 您需要将公钥移动.ssh/authorized_keys到服务器上。为此,您可以使用该实用程序,也可以使用and命令ssh-copy-id手动完成。catscp

  4. 在最好的情况下,这将允许在没有密码的情况下连接到您的服务器。

好的,现在是我今天发现的问题:首先有几个密钥生成算法:rsa, dsaecdsa并且ed25519OpenSSH 有很多版本(你可以在本地机器上拥有一个版本,在你的服务器上拥有旧版本):

提示:当您连接到服务器时,使用ssh -v有助于查看附加信息。

OpenSSH_8.2p1 Ubuntu-4, OpenSSL 1.1.1f 31 Mar 2020

debug1: Remote protocol version 2.0, remote software version OpenSSH_5.3

我今天的错误是我试图使用具有“较新”生成算法的密钥,而服务器上安装的 OpenSSH 版本不支持该密钥。当我检查了支持的算法时,我发现另一个错误是服务器拒绝了我的算法:

debug1: Skipping ssh-dss key /home/user/.ssh/id_dsa - not in PubkeyAcceptedKeyTypes

之后,我不得不改变我的密钥算法,然后我才能成功连接到服务器。

OpenSSH 发布说明:链接

于 2020-07-17T19:16:05.620 回答
0

我遇到了这个问题,其他答案都没有解决它,尽管其他答案当然正确的。

就我而言,结果证明/root目录本身(而不是 eg /root/.ssh)具有错误的权限。我需要:

chown root.root /root
chmod 700 /root

当然,chmod 770无论如何,这些权限都应该是这样的(也许)。但是,它特别阻止sshd了工作,即使/root/.ssh两者/root/.ssh/authorized_keys都具有正确的权限和所有者。

于 2018-09-02T15:24:11.937 回答
0

我用这种方式。

cat ~/.ssh/id_rsa.pub| ssh user@remote-system 'umask 077; cat >>~/.ssh/authorized_keys'
于 2020-06-22T08:36:57.233 回答
0

我的主目录位于非标准位置,并且在sshd日志中我有以下行,即使所有权限都很好(请参阅其他答案):

无法打开授权密钥“/data/home/user1/.ssh/authorized_keys”:权限被拒绝

我在这里找到了一个解决方案:Trouble with ssh public key authentication to RHEL 6.5

在我的特殊情况下:

  • 在中添加了一个新行/etc/selinux/targeted/contexts/files/file_contexts.homedirs

  • 这是常规主目录的原始行:

    /home/[^/]*/\.ssh(/.*)? unconfined_u:object_r:ssh_home_t:s0

  • 这是我的新线路:

    /data/home/[^/]*/\.ssh(/.*)? unconfined_u:object_r:ssh_home_t:s0

  • 紧随其后的是一个restorecon -r /data/和一个sshd重新启动。

于 2016-11-25T09:00:05.697 回答
0

当我将登录用户的组添加到另一个用户时,我遇到了这个问题。

假设有一个名为 userA 的 SSH 登录用户和一个非 SSH 登录用户 userB。userA 也有组 userA。我修改了 userB 以拥有组 userA 。导致所描述的行为,因此 userA 无法在没有提示的情况下登录。

从 userB 中删除组 userA 后,没有提示的登录再次起作用。

于 2019-05-27T16:52:22.003 回答
0

在我的情况下,这是因为用户组未在配置文件/etc/ssh/sshd_config的 AllowGroups 中设置。添加后,一切正常。

于 2016-01-26T06:39:19.100 回答