SSH 使用密钥登录
SSH 是最常见的远程登录工具,而使用密钥登录是一种比密码登录更安全、更高效的方式。密钥登录依赖于非对称加密算法,包含一对密钥:公钥 和 私钥。通过将公钥放置在目标服务器的授权文件中,客户端使用对应的私钥即可安全登录服务器,从而避免明文密码暴露的风险。
ssh-keygen
使用 ssh-keygen 创建加密方式为 rsa,字节为 2048 文件名为 jimmy,这时提示需要输入密码,为使用密钥登录的密码
[root@localhost test]# ssh-keygen -t rsa -b 2048 -f jimmy
Generating public/private rsa key pair.
Enter passphrase for "jimmy" (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in jimmy
Your public key has been saved in jimmy.pub
The key fingerprint is:
SHA256:wdJZ0VHR/Ic6UAA417rM8nO9zYIfLp/+7wvnaUa0pjE root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
| ..o++.oo+ |
| oo.o. o o|
| .o=. . ..|
| .... ..o|
| oS. . .. o|
| . + oE + |
| o o.o*. |
| o +.o*+o.|
| o =B+**+|
+----[SHA256]-----+
出现两个文 .pub 是公钥,另一个就是私钥
[root@localhost]# ls -l
total 16
-rw------- 1 root root 1876 Dec 23 20:48 jimmy
-rw-r--r-- 1 root root 408 Dec 23 20:48 jimmy.pub
需要开启ssh配置,重启 ssh 服务
[root@localhost]# cat /etc/ssh/sshd_config
# 没有就添加
RSAAuthentication yes
PubkeyAuthentication yes
[root@localhost]# systemctl restart sshd
把公钥放到 ~/.ssh/authorized_keys 下
[root@localhost]# cat jimmy.pub >> ~/.ssh/authorized_keys
此时使用密钥登录就需要密码+密钥文件
[root@localhost]# ssh root@192.168.89.129 -i jimmy
/etc/ssh/ssh_config.d/05-redhat.conf line 7: Unsupported option "gssapiauthentication"
/etc/ssh/ssh_config.d/05-redhat.conf line 7: Unsupported option "gssapiauthentication"
Enter passphrase for key 'jimmy':
Last login: Mon Dec 23 05:05:52 2024 from 192.168.89.129
[root@localhost ~]#