Linux 配置远程SSH服务(密码+密钥)
环境准备:将虚拟机1恢复快照,然后手动配置一个NAT模式IP为192.168.200.100,hostname设置为fuwu1将虚拟机1复制为虚拟机2,然后手动配置一个NAT模式IP为192.168.200.200,hostname设置为fuwu2windows准备 xshell 或 putty 或 MobaXterm 或 SecureCRT,建议xshell。
环境准备:
将虚拟机1恢复快照,然后手动配置一个NAT模式IP为192.168.200.100,hostname设置为fuwu1
将虚拟机1复制为虚拟机2,然后手动配置一个NAT模式IP为192.168.200.200,hostname设置为fuwu2
windows准备 xshell 或 putty 或 MobaXterm 或 SecureCRT,建议xshell
一、ssh命令登录以及Xshell登录
1、在虚拟机2上ssh登录虚拟机1
[root@fuwu2 ~]# ssh root@192.168.200.100
The authenticity of host '192.168.200.100 (192.168.200.100)' can't be established.
ECDSA key fingerprint is SHA256:LKt/z3vCmOSq+tKERl7omW+7L4kC0ngZTi7kimfHf5g.
ECDSA key fingerprint is MD5:2f:be:5b:f6:4b:d7:dc:44:ef:0e:c1:c4:ab:cd:a1:a4.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.200.100' (ECDSA) to the list of known hosts.
root@192.168.200.100's password:
Last login: Wed Aug 17 20:27:22 2022
[root@fuwu1 ~]#
2、虚拟机1上退出登录
[root@fuwu1 ~]# exit
登出
Connection to 192.168.200.100 closed.
3、Xshell登录虚拟机1
----界面演示----
二、验证配置禁止root用户远程登录后效果
1、虚拟机1上修改ssh配置文件,禁用root远程登录
[root@fuwu1 ~]# vi /etc/ssh/sshd_config
找到 #PermitRootLogin yes 一行,将 # 删除,并将 yes 修改为 no ,保存退出
2、重启sshd服务
[root@fuwu1 ~]# systemctl restart sshd
3、虚拟机2上以root尝试ssh登录虚拟机1验证
[root@fuwu2 ~]# ssh root@192.168.200.100
root@192.168.200.100's password:
Permission denied, please try again.
root@192.168.200.100's password:
发现root用户无法登录
4、Xshell上通过root用户登录尝试虚拟机1,发现也无法登录
5、恢复虚拟机1的ssh配置
[root@fuwu1 ~]# vi /etc/ssh/sshd_config
将 PermitRootLogin no 一行的 no 改回 yes,保存退出
重启sshd
[root@fuwu1 ~]# systemctl restart sshd
服务器2以及xshell以root用户重新登录可以成功登录
三、配置通过密钥登录
配置虚拟机2可以通过密钥的方式远程登录虚拟机1而不需要输入密码。
1、在虚拟机2中生成密钥文件
一路回车即可
[root@fuwu2 ~]# 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:1E+B1rW5+VDzBf+Hy+Ri7saFwwhsVpwSiTHGyWuX1gE root@fuwu2
The key's randomart image is:
+---[RSA 2048]----+
| o++E= +..o |
| .=.o.B .. = |
| o.*... o.+|
| o.O .o ==|
| . =S. o..* +|
| . ++.+.|
| .oo+ .|
| oo. |
| oo |
+----[SHA256]-----+
2、将公钥文件拷贝到虚拟机1中
[root@fuwu2 ~]# ssh-copy-id root@192.168.200.100
/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.200.100's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.200.100'"
and check to make sure that only the key(s) you wanted were added.
3、在虚拟机2上远程登录虚拟机1验证
[root@fuwu2 ~]# ssh root@192.168.200.100
Last login: Wed Aug 17 20:40:50 2022 from 192.168.200.1
[root@fuwu1 ~]#
发现这次没有输入密码即可自动登录,这就是密钥方式登录
4、Xshell配置密钥登录
先在windows上通过Xshell生成一个密钥,然后把密钥的公钥部分添加到虚拟机1的相关文件中
[root@fuwu1 ~]# vi /root/.ssh/authorized_keys
然后就可以密钥登录
-----界面演示-----
四、scp远程拷贝命令演示
scp即 ssh + cp ,实现与远程机器之间的文件拷贝
在虚拟机2上创建一个文件a.txt,并将该文件远程传输到虚拟机1的 /root/ 目录下
[root@fuwu2 ~]# touch a.txt
[root@fuwu2 ~]# scp a.txt root@192.168.200.100:/root/a.txt
a.txt
在虚拟机1上验证
[root@fuwu1 ~]# ll /root/ |grep a.txt
-rw-r--r--. 1 root root 0 8月 17 21:12 a.txt
更多推荐
所有评论(0)