如何在Linux服务器上配置SSH密钥验证

如何在Linux服务器上配置SSH密钥验证,第1张

客户端设置

客户需要生成密钥对(公共和私有)。 稍后我们会将公钥上传到SSH服务器。

ssh-keygen

生成SSH密钥时,系统将提示您输入密码。 我们建议在此步骤中使用新密码。 这将避免别人使用你的钥匙。

将公钥上传到服务器

现在我们将使用ssh-copy-id命令来上传密钥。 您可以手动将公钥(~/.ssh/id_rsa.pub)附加到服务器上的 ~/.ssh/authorized_keys。

ssh-copy-id user@hostname

更安全的SSH设置

此步骤是可选的,但建议禁用密码验证(特别是root用户)打开文件 /etc/ssh/sshd_config 并将PasswordAuthentication更改为“no”值。

PasswordAuthentication no

确保在重新启动SSH服务器之前测试您的ssh密钥验证!

sudo service ssh restart

1. 制作密钥

在服务器上制作密钥对。首先用密码登录到你打算使用密钥登录的账户,然后执行以下命令:

[root@host ~]$ ssh-keygen <== 下面是相应的 *** 作提示,以及密钥生成的位置都会告诉我们。

Generating public/private rsa key pair.Enter file in which to save the key (/root/.ssh/id_rsa): <== 按 EnterCreated directory '/root/.ssh'.Enter passphrase (empty for no passphrase): <== 输入密钥锁码,或直接按 Enter 留空Enter same passphrase again: <== 再输入一遍密钥锁码Your identification has been saved in /root/.ssh/id_rsa. <== 私钥Your public key has been saved in /root/.ssh/id_rsa.pub. <== 公钥The key fingerprint is:0f:d3:e7:1a:1c:bd:5c:03:f1:19:f1:22:df:9b:cc:08 root@host

在 当前 用户的家目录中生成了一个 .ssh 的隐藏目录,内含两个密钥文件。id_rsa 为私钥,id_rsa.pub 为公钥。

2. 在服务器上安装公钥

键入以下命令,在服务器上安装公钥:

[root@host ~]$ cd .ssh

[root@host .ssh]$ cat id_rsa.pub >>authorized_keys

如此便完成了公钥的安装。为了确保连接成功,请保证以下文件权限正确:

[root@host .ssh]$ chmod 600 authorized_keys[root@host .ssh]$ chmod 700 ~/.ssh

3. 设置 SSH,打开密钥登录功能

编辑 /etc/ssh/sshd_config 文件,添加如下设置:

sudo vi /etc/ssh/sshd_config

RSAAuthentication yesPubkeyAuthentication yes

接着保存后 另外,请留意 root 用户能否通过 SSH 登录:PermitRootLogin yes

当你完成全部设置,并以密钥方式登录成功后,再禁用密码登录:

PasswordAuthentication no

最后,重启 SSH 服务:

[root@host .ssh]$ service sshd restart


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/7550965.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-07
下一篇2023-04-07

发表评论

登录后才能评论

评论列表(0条)

    保存