ssh密钥远程登录服务器

gkh 发布于 2022-11-23 13270 次阅读


首先密钥登录是解决安全性的问题

一般的密码方式登录,容易有密码被暴力破解的问题。所以,一般我们会将 SSH 的端口设置为默认的 22 以外的端口,或者禁用 root 账户登录。其实,有一个更好的办法来保证安全,而且让你可以放心地用 root 账户从远程登录——那就是通过密钥方式登录。直接原因,vscode使用密钥登录更方便

制作密钥

[root@host ~]$ 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:
0f:d3:e7:1a:1c:bd:5c:03:f1:19:f1:22:df:9b:cc:08 root@host

使用密钥

一般使用环境是windows环境。通过上述过程生成了两个文件一个是id_rsa和id_rsa.pub。密钥是分为公钥和私钥的。公钥是可以在网络上传播的,私钥是秘密的禁止泄露的。因而一般给设备分发公钥。windows上留私钥,服务器上留公钥。将私钥放置在 c:/user/用户名/.ssh文件夹下。将公钥放置在服务器的 ~/.ssh下。

[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
[root@host .ssh]$ sudo service sshd restart
此作者没有提供个人介绍。
最后更新于 2022-11-23