Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
向~/.ssh/authorized_keys添加公钥不会自动让我登录_Ssh_Public Key_Authorized Keys - Fatal编程技术网

向~/.ssh/authorized_keys添加公钥不会自动让我登录

向~/.ssh/authorized_keys添加公钥不会自动让我登录,ssh,public-key,authorized-keys,Ssh,Public Key,Authorized Keys,我将公共SSH密钥添加到authorized_keys文件中ssh localhost应该在不询问密码的情况下让我登录 我这样做了,并尝试键入ssh localhost,但它仍然要求我键入密码。是否有其他设置,我必须通过,使其工作 我已按照说明更改权限: 下面是我执行ssh-v localhost的结果 debug1: Reading configuration data /home/john/.ssh/config debug1: Reading configuration data /etc

我将公共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>
debug1:读取配置数据/home/john/.ssh/config
debug1:读取配置数据/etc/ssh/ssh\u config
debug1:将选项应用于*
debug1:连接到本地主机[127.0.0.1]端口22。
debug1:已建立连接。
debug1:identity file/home/john/.ssh/identity类型1
debug1:identity file/home/john/.ssh/id\u rsa type-1
debug1:identity file/home/john/.ssh/id_dsa type-1
debug1:远程协议版本2.0,远程软件版本OpenSSH_4.7p1 Debian-8ubuntu3
debug1:match:OpenSSH_4.7p1 Debian-8ubuntu3 pat OpenSSH*
debug1:启用协议2.0的兼容模式
debug1:本地版本字符串SSH-2.0-OpenSSH_4.7p1 Debian-8ubuntu3
debug1:SSH2\u MSG\u KEXINIT已发送
debug1:SSH2\u MSG\u KEXINIT已收到
debug1:kex:server->client aes128 cbc hmac-md5无
debug1:kex:client->server aes128 cbc hmac-md5无
debug1:SSH2_MSG_KEX_DH_GEX_请求(1024在.ssh/authorized_keys中列出公钥是必需的,但不足以让sshd(服务器)接受它。如果您的私钥受密码短语保护,则每次都需要给ssh(客户端)密码短语。或者您可以使用,或GNOME等同物


更新的跟踪与受密码保护的私钥一致。请参阅ssh代理,或使用
ssh-keygen-p
您需要验证
授权密钥文件及其所在的文件夹/父文件夹的权限

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
有关更多信息,请参阅

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

chmod go-w ~
请注意,即使所有权限看起来都正常,也可能触发此错误。禁用它对我来说是个好办法(插入关于禁用它的常见免责声明)。

还可能导致授权密钥不起作用。特别是对于root in 6和7。不过,没有任何必要禁用它

验证权限是否正确后,可以按如下方式解决此问题:

chmod 700 /root/.ssh
chmod 600 /root/.ssh/authorized_keys
restorecon -R -v /root/.ssh

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

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

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

此答案是从中窃取的。

在命令行中发出以下命令:

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

设置ssh-authorized\u密钥似乎很简单,但它隐藏了一些我试图找出的陷阱

--服务器--

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

--客户--

将其视为Linux仿真,并安装和运行OpenSSH

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

在这里,只需按Enter键,就可以在~/.ssh/中获得默认的两个文件“id\u rsa”和“id\u rsa.pub”,但如果您为\u键指定一个名称,则生成的文件将保存在当前工作目录中

2.您的_key.pub文件传输到目标计算机,
ssh copy id user_name@host_name

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

ssh copy id-i path/to/key\u name.pub用户_name@host_name

3.记录
ssh用户_name@host_name
将仅适用于默认的id\u rsa文件,因此这里是第二个陷阱user@host

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

如果服务器仍然要求输入密码,那么您提供了一些信息。要输入密码,请在创建密钥时输入密码:(这是正常的)

如果ssh未在默认端口22上侦听,则必须使用
ssh-p port\u nr

--服务器-----

4.修改文件/etc/ssh/sshd\u config

RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile  %h/.ssh/authorized_keys
(如有,则取消注释)

这告诉ssh接受文件authorized_key,并在用户主目录中查找写入.ssh/authorized_key文件的key_名称

5在目标计算机上设置权限

chmod 755 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
同时关闭pass身份验证

passwordAuthentication no

关闭所有ssh root/admin/…@域尝试的入口

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

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

<强> 7。< /强>考虑优秀

8.额外
为了访问MySQL(bind=127.0.0.1)服务器,绝望者还可以确保他们在授权密钥文件中没有额外的换行符,因为他们从一个混乱的终端复制了文件id\u rsa.pub的文本。

最后为我做的诀窍是确保所有者/组不是root,而是用户:


确保目标用户设置了密码。运行
passwd username
设置密码。即使禁用了密码SSH登录,我也需要这样做。

在这一点上,请确保您的sshd配置有以下行:

PermitRootLogin without-password
如上设置,然后重新启动sshd(
/etc/init.d/sshd restart

注销并再次尝试登录

我认为默认情况是:

PermitRootLogin no

我发布了
sudo chmod 700~/.ssh
chmod 600~/.ssh/authorized_key
chmod go-w$HOME$HOME/.sshPermitRootLogin no
ssh-agent bash

ssh-add
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
$sudo grep AuthorizedKeysFile /etc/ssh/sshd_config
#AuthorizedKeysFile .ssh/authorized_keys
AuthorizedKeysFile  /etc/ssh/authorized_keys/%u
Host remote_host_name
    HostName 172.xx.xx.xx
    User my_user
    IdentityFile /home/my_user/.ssh/my_user_custom
$ chmod 600 ~/.ssh/sshKey
$ chmod 644 ~/.ssh/sshKey.pub
chown root.root /root
chmod 700 /root
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]
sudo usermod -a -G ssh NEW_USER
cat ~/.ssh/id_rsa.pub| ssh user@remote-system 'umask 077; cat >>~/.ssh/authorized_keys'