Skip to content

ssh 相关操作

1 生成密钥对

用法: ssh-keygen [-q] [-a rounds] [-b bits] [-C comment] [-f output_keyfile]

参数作用
-t指定生成密钥格式:dsa / ecdsa / ecdsa-sk / ed25519 / ed25519-sk / rsa
-b指定密钥长度
-C说明,会追加在密钥中
-f文件保存路径
-P私钥加密密码

例如:

shell
[root@more .ssh]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y        #文件已存在,y表示覆盖
Enter passphrase (empty for no passphrase):   # 输入密钥加密密码,无密码直接回车
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:
SHA256:PsQDo2Dq3c1ms05sjcrcFkGuTl/jbldS+uM9uHot57U root@more.top
The key's randomart image is:
+---[RSA 3072]----+
|                 |
|       .         |
|  o   =          |
| o . . *     .   |
|.   . . S   o    |
|. . .o++o+ o .   |
| . .o..@*.. + o .|
|    o.Boo+ . *.+o|
|     +o+o...+o*E.|
+----[SHA256]-----+
[root@more .ssh]#

2 扫描指定主机信息

用法: ssh-keyscan [-p port] [-T timeout] [host | addrlist namelist]

shell
[root@more .ssh]# ssh-keyscan -p 22 192.168.1.26
# 192.168.1.26:22 SSH-2.0-OpenSSH_8.8
# 192.168.1.26:22 SSH-2.0-OpenSSH_8.8
192.168.1.26 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC6BDdgX1hKPXlitr0xCP7kOffF7uXp9mdyfY2Qgie4nunI4Q7qDFlFDFBXfdHKBgvpSe7oDSGei6JwDOhK5BTjBcO6nrdXbtBUIyJ4tev+V2xqOSPEukkDz51x+01Yv8scC25phXcJPTsVLTbaQCDmoKnYO0QrzRpptJU2yLgps75mLhEZXa5QdEPg9MfKTBz+WuIgGvI7beIImch5UTZLkbxfmuSwllfBIdJ/zLjaaieW55hd5DX5fjLuu1HWlYgLdMkW4F7vyjD5DTfLRXmfK+PYuLk0LkOuYhYlIwwJnpU3WIg8pOIa7PaaiLpZs4NHO+LEK8E9iQb7zSYvBYyaw/U8UnpTLT7QiuYcDNuH6CmT/ZKYHwmDiHlCPENGAWm/E32cNVuHr6Fc7DGJii25nf4KFAu9WhZJmBSdlhpBEtSm9Nb1423o7cfUNDrGTeOQVZi9/tIZ5XJIO8MOB/Gu2/aKOhjTq04tDroDTMArlh4WFWA9G4srQzM5MDpywZc=
# 192.168.1.26:22 SSH-2.0-OpenSSH_8.8
192.168.1.26 ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAID+naT7cLc9IBvBTFGRZpTwTgVXnMJtqqaLdHUegny33
# 192.168.1.26:22 SSH-2.0-OpenSSH_8.8
# 192.168.1.26:22 SSH-2.0-OpenSSH_8.8
[root@more .ssh]#

提示

可以用它来扫描添加信任主机

shell
ssh-keyscan 192.168.1.26 >> ~/.ssh/known_hosts