设置 SSH 通过密钥登录
ssh 设置 登录 通过 密钥
2023-06-13 09:14:13 时间
1. 制作密钥对
首先在服务器上制作密钥对。首先用密码登录到你打算使用密钥登录的账户,然后执行以下命令:
[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
密钥锁码在使用私钥时必须输入,这样就可以保护私钥不被盗用。当然,也可以留空,实现无密码登录。
现在,在 root 用户的家目录中生成了一个 .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 文件,进行如下设置:
RSAAuthentication yes
PubkeyAuthentication yes
另外,请留意 root 用户能否通过 SSH 登录:
PermitRootLogin yes
当你完成全部设置,并以密钥方式登录成功后,再禁用密码登录:
PasswordAuthentication no
最后,重启 SSH 服务:
[root@host .ssh]$ service sshd restart
4. 将私钥下载到客户端,登陆时使用
其他操作
#显示远程登录失败的记录
[root@host .ssh]$ lastb
#用 w 命令看看当前是否其他人远程登录到服务器
[root@host .ssh]$ w
相关文章
- nginx需要修改服务端口,需要修改哪个配置文件_centos7 ssh端口修改
- h3c 配置ssh登录_H3交换机SSH跳转
- 使用RDP和SSH远程访问Windows PC
- SSH无密登录Linux:实现安全稳妥的连接(ssh无密登陆linux)
- Linux SSH登录失败:解决方案(linux无法ssh登录)
- 重启Linux服务器,恢复SSH连接(linux重启ssh)
- 如何使用Linux安全登录SSH(linux怎么使用ssh)
- Linux安装SSH配置: 一步一步指引(linux安装ssh配置)
- 配置 Linux 服务器 SSH 远程登录(linux配置ssh)
- Securely Access Linux Server with SSH Login(ssh登陆linux)
- 「用SSH登录Linux:步骤及注意事项」(怎么用ssh登录linux)
- 安全登录Linux SSH安全登录指南(linux如何ssh)
- Linux SSH: 无线网络安全的重要保障(linux的ssh是什么)
- Linux系统中使用SSH的快捷方式(linux系统使用ssh)
- 如何在CentOS/RHEL上设置SSH免密码登录
- Linux之神秘:SSH的奥秘(linux软件ssh)
- Linux启用SSH远程访问实现安全连接(linux启动ssh)
- 安全Linux系统升级SSH安全保障(linux升级ssh)
- 如何使用SSH连接Mysql数据库(ssh连接mysql数据库)
- 如何在CentOS/RHEL上设置SSH免密码登录
- Linux 下安全登录:从 SSH 开始(Linux 下ssh登录)
- 安全SSH中Redis运维安全指南(ssh中redis)
- SSH与Redis技术搭建高效项目(ssh redis项目)