来访~101771 文章~106 评论~25
2023年3月20日 作者 张临志

Ubuntu设置使用SSH密钥远程登陆管理

普通密码登录方式容易被暴力破解存在安全风险,通过设置密钥登录 SSH 可以提高服务器的安全性能,便于更好的管理。

在服务器上生成新的密钥对

生成密钥

控制台输入

$ ssh-keygen  <== 建立密钥对

输出内容

Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): <== 按 Enter
Created 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:
****************************

此时生成的两个密钥文件id_rsaid_rsa.pub在 root 目录下的.ssh文件夹中。

安装公钥

控制台输入

$ cd .ssh
$ cat id_rsa.pub >> authorized_keys
$ chmod 600 authorized_keys
$ chmod 700 ~/.ssh

即可完成服务器端公钥的绑定

保存私钥到本地

.ssh文件夹中将公钥文件pub_rsa.pub下载到本地,即可在 xshell 、final shell等软件中使用。

设置 SSH 使用密钥验证方式

编辑 /etc/ssh/sshd_config

$ vim /etc/ssh/sshd_config

文件添加以下内容,

RSAAuthentication yes
PubkeyAuthentication yes

注意其中的几个配置选项

PermitRootLogin yes              ####  允许以root身份登录
PasswordAuthentication no        ####  在SSH密钥登入测试完成后再修改此项!

重启 SSH 服务,完成配置

$ service sshd restart