目录
一.密钥文件解释
二.FinalShell使用密钥登录虚拟机
(1.)打开windows中cmd命令终端使用下述命令生成密钥对
注意:
拓展ssh-keygen参数
(2.)将公钥发送给想要远程连接的虚拟机
(3.)Finallshell使用密钥登录
三.使用ssh命令远程登录别的服务器
四.配置sshd_config禁用密码登录,仅使用密钥登录
一.密钥文件解释
- id_rsa.pub:这是 SSH 密钥对中的公钥文件。它包含了用于身份验证的公钥部分。你可以将这个公钥分发给你希望访问的服务器或其他系统,以便它们可以验证你的身份。
- id_rsa:这是 SSH 密钥对中的私钥文件。私钥用于对与公钥相关联的任何系统进行身份验证。它必须保持机密。
- known_hosts:这个文件包含了你连接过的远程主机的公钥。当你首次连接到一个主机时,它的公钥将被添加到这个文件中。这样,下次你连接到同一个主机时,SSH 客户端将检查主机的公钥是否与之前存储的公钥匹配,以确保连接的安全性。
- authorized_keys:这个文件包含了被授权访问该服务器的用户的公钥。每行一个公钥,当用户尝试连接到服务器时,他们提供的私钥将与这个文件中的公钥进行匹配,如果匹配成功,则允许用户访问服务器。
以上四个文件存放路径如下述:
windows默认存放路径 C:\Users\Administrator\.ssh
虚拟机root用户默认存放路径 /root/.ssh/
注意:以 . 开头的文件为隐藏文件,若看不到记得选择常看所有文件
二.FinalShell使用密钥登录虚拟机
(1.)打开windows中cmd命令终端使用下述命令生成密钥对
ssh-keygen
执行ssh-keygen默认生成的时RSA算法,2048位长度的密钥对。
输入ssh-keygen后连续按三次回车键盘即可生成
成功后类似下图:
#生成的密钥对默认在如下路径:
C:\Users\Administrator\.ssh
注意:
如果提示命令不可执行,是因为windows系统中没有安装ssh服务,需要进行安装。
安装步骤: 左下角打开设置——系统——在查找设置里搜索 添加可选功能——添加功能里输入openssh ——将搜索出来的open客户端 和 open服务器进行下载安装。
安装好后打开PowerShell,在电脑左下角任务栏搜索即可打开。
Powershell则是一个强大的自动化脚本环境,除了可以执行CMD的所有命令,还支持. NET framework,可以调用和管理Windows系统和应用的各种对象。
#启动 SSHD 服务
PS C:\Users\Administrator> Start-Service sshd
警告: 正在等待服务“OpenSSH SSH Server (sshd)”启动...
#设置SSHD服务开机自启动
PS C:\Users\Administrator> Set-Service -Name sshd -StartupType 'Automatic'
#查看“OpenSSH-Server-In-TCP”的防火墙规则
PS C:\Users\Administrator> Get-NetFirewallRule -Name *ssh*
Name : OpenSSH-Server-In-TCP
DisplayName : OpenSSH SSH Server (sshd)
Description : Inbound rule for OpenSSH SSH Server (sshd)
DisplayGroup : OpenSSH Server
Group : OpenSSH Server
Enabled : True
Profile : Any
Platform : {}
Direction : Inbound
Action : Allow
EdgeTraversalPolicy : Block
LooseSourceMapping : False
LocalOnlyMapping : False
Owner :
PrimaryStatus : OK
Status : 已从存储区成功分析规则。 (65536)
EnforcementStatus : NotApplicable
PolicyStoreSource : PersistentStore
PolicyStoreSourceType : Local
PS C:\Users\Administrator>
此处如果使用ssh命令还不成功,可以尝试重启电脑,我就是通过重启才可以使用该命令
拓展ssh-keygen参数
- 指定密钥类型 (-t):通过
-t
参数指定生成的密钥类型。常见的类型包括 RSA、DSA 和 ECDSA。例如:
ssh-keygen -t rsa
- 指定密钥位数 (-b):通过
-b
参数指定生成的密钥的位数。例如:
ssh-keygen -t rsa -b 4096
- 指定保存路径 (-f):通过
-f
参数指定生成的密钥的保存路径和文件名。例如:
ssh-keygen -t rsa -f /path/to/save/keys/id_rsa
- 添加注释 (-C):通过
-C
参数添加一个注释,用于标识密钥对的用途。例如:
ssh-keygen -t rsa -C "user@hostname"
- 设置密码保护 (-P):通过
-P
参数设置生成的私钥的密码。例如:
ssh-keygen -t rsa -P "your_password"
- 生成仅包含公钥的文件 (-y):通过
-y
参数生成一个只包含公钥的文件。例如:
ssh-keygen -y -f /path/to/private_key > /path/to/public_key
- 指定哈希算法 (-a):通过
-a
参数指定用于生成密钥的哈希算法。例如:
ssh-keygen -t rsa -a 100
(2.)将公钥发送给想要远程连接的虚拟机
在windows中cmd终端输入下述命令
语法:ssh-copy-id IP地址
会提示确认提示,输入y即可
再输入密码即可成功。
ssh-copy-id 192.168.102
注意:
使用ssh-copy-id在windows中执行不成功就手动复制公钥到目标虚拟机的authorized_keys的文件中,虚拟机的root用户保存密钥对的路径如下:authorized_keys就是要在这个目录
/root/.ssh/
在想要远程的虚拟机存放密钥对的路径下如果不存在authorized_keys,则使用touch命令创建这个文件,并将windows电脑的公钥复制到该文件里。
配置好后就可以在windows终端使用ssh命令连接虚拟机啦
(3.)Finallshell使用密钥登录
打开Finalshell,使用ssh协议添加连接,进行信息配置,选择密钥连接,导入私钥并设置一个私钥保护密码,应用即可成功连接。
三.使用ssh命令远程登录别的服务器
有啦上面的基础知识与步骤详情,那这里就会很简单啦。
1、在服务器A上生成一对密钥
[root@server-1 ~]# ssh-keygen
Generating public/private rsa key pair.
# 生成的密钥对默认保存的位置
Enter file in which to save the key (/root/.ssh/id_rsa):
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:y8NF/DbgK9m1S+JahBVlGSIP7qXGlFo0Cgk79i18SUA root@server-1
The key's randomart image is:
+---[RSA 2048]----+
| .oE. = o.+o |
| ..o + B +. |
| + o = B |
| . + o B B o |
| + = S + = |
| o + * + o |
| B = o |
| = o . |
| ... . |
+----[SHA256]-----+
[root@server-1 ~]# ls /root/.ssh/ #查看密钥对路径下的文件
id_rsa id_rsa.pub known_hosts
id_rsa.pub 生成的公钥文件
id_rsa 生成的私钥文件
known_hosts 首次远程登录其他服务器保存的远程服务器的公钥
2、将公钥拷贝给另一个服务器
[root@server-1 ~]# ssh-copy-id 192.168.1.102
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.1.102 (192.168.1.102)' can't be established.
ECDSA key fingerprint is SHA256:FOjAVNiEZDE1Fsyc2IlnrjfC1G4Z3UpWthHVqBki5uo.
ECDSA key fingerprint is MD5:bb:7a:73:a8:f7:ab:52:eb:ab:cc:b6:93:39:f4:11:56.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.1.102's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '192.168.1.102'"
and check to make sure that only the key(s) you wanted were added.
[root@server-1 ~]# ssh 192.168.1.102
Last login: Thu Mar 21 20:59:38 2024 from bogon
# 查看第二台服务器中保存的公钥信息
[root@bogon ~]# cat /root/.ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDBMsxREekrxcAtfJzbJmRi373Qsm/Y6MIsaVg9iiUudmV7Z5WKiqyHiamu/e1/2aBRNcVy0LYeJxPhpkCPBgke34Cah7z7JqLeMLOByatCl6EvIibnIQhWytkGqE5zsPE6kcuxY+J+HysLwDrOfv1CMw/I/gJLJ3QiViF1Lmt2fQEQ7CnWtEBv038aJ8ieiuzxcpIUHEsJu6GYjF+BAOwiZ7WvnQxHUKrkv7aPhLBf/kNc72qGGkviFYAw3FBBHvOyaO69SoBmjM4fejhnwypTCyn7nbWirlDz8OWWiUvEFGdxLtCs0AiZ/2m0twYzZFQRJJymAbOmEJpn4iQF7/Gd root@server-1
四.配置sshd_config禁用密码登录,仅使用密钥登录
以下配置可大大提高安全性
#1.修改sshd配置文件
vim /etc/ssh/sshd_config
#2.以下选项部分找不到则添加
RSAAuthentication yes #开启秘钥登录
PubkeyAuthentication yes #允许使用公钥认证
#上面两个我没有找到就新增上去,下面三项一般可以找到对应的开启
AuthorizedKeysFile .ssh/authorized_keys #公钥所在位置
PermitRootLogin yes #这个配置项控制是否允许 root 用户通过 SSH 登录到服务器
PasswordAuthentication no # 关闭密码登录(修改参数yes为no)
#3.重启sshd服务
systemctl restart sshd