实现目标
现在实现虚拟机A(192.168.0.169)远程操作虚拟机B(192.168.0.187)
- 在虚拟机A上执行
ssh-keygen -t rsa
,此时如图1、图2所示
图1
图2
- 查看并且复制虚拟机A上的
id_rsa.pub
[root@localhost .ssh]# cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDSja7k47Dh64IdMxm38yyvVbECaOwByAUQfhScRjhV/uqx4aPKfRTSOSxxwnY2YVI60j6J/Wh4le7xM6DESZH2jlsvbdmcNk91HwyqIpRe3tBJ4J7z3H58SiqVkcl4D99LhfOZg0bK5j3fsB5ehjnyUJV93GjUG4M1o5bXPyc8RYYj9/e81+tnSYhFejgYHIB9lVecLR+Tf8oWOHuRa3Y/z5WuFoy5jJj7VGtogQDRE5D+Gv1m8m4pjv115g9BjooO4Z21n3eJniPr0TJDDKnUJeycvCRyoihNskpuXqtP3xwRSTtkOG+/6Gzs1Oha19sImYtyZItISkxTX38NlwL5 root@localhost.localdomain
- 在虚拟机B上粘贴刚才复制的
id_rsa.pub
的内容到/root/.ssh
的authorized_keys
文件里
[root@localhost .ssh]# echo "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDSja7k47Dh64IdMxm38yyvVbECaOwByAUQfhScRjhV/uqx4aPKfRTSOSxxwnY2YVI60j6J/Wh4le7xM6DESZH2jlsvbdmcNk91HwyqIpRe3tBJ4J7z3H58SiqVkcl4D99LhfOZg0bK5j3fsB5ehjnyUJV93GjUG4M1o5bXPyc8RYYj9/e81+tnSYhFejgYHIB9lVecLR+Tf8oWOHuRa3Y/z5WuFoy5jJj7VGtogQDRE5D+Gv1m8m4pjv115g9BjooO4Z21n3eJniPr0TJDDKnUJeycvCRyoihNskpuXqtP3xwRSTtkOG+/6Gzs1Oha19sImYtyZItISkxTX38NlwL5 root@localhost.localdomain">>authorized_keys
- 此时在虚拟机A上执行
scp
命令进行测试,执行成功之后虚拟机A上会多出一个文件known_hosts
,如图3、图4所示
scp /home/aa.txt root@192.168.0.187:/home/aa.txt
图3
图4
- 执行到这里的时候会发现,虚拟机A再次执行
scp
或者ssh
都可以直接执行,不用再输入密码确认,如图5所示
图5
- 这个时候我们就实现了虚拟机A跟虚拟机B的免密码传输文件和远程操作指令,我们把此时虚拟机A和虚拟机B上需要的文件整理一下,以下文件都在
/root/.ssh
目录下。
虚拟机A
id_rsa
-----BEGIN RSA PRIVATE KEY----- MIIEowIBAAKCAQEA0o2u5OOw4euCHTMZt/Msr1WxAmjsAcgFEH4UnEY4Vf7qseGj yn0U0jksccJ2NmFSOtI+if1oeJXu8TOgxEmR9o5bL23ZnDZPdR8MqiKUXt7QSeCe 89x+fEoqlZHJeA/fS4XzmYNGyuY937AeXoY58lCVfdxo1BuDNaOW1z8nPEWGI/f3 vNfrZ0mIRXo4GByAfZVXnC0fk3/KFjh7kWt2P8+VrhaMuYyY+1RraIEA0ROQ/hr9 ZvJuKY79deYPQY6KDuGdtZ93iZ4j69EyQwyp1CXsnLwkcqIoTbJKbl6rT98cEUk7 ZDhvv+hs7NToWtfbCJmLcmSLSEpMU19/DZcC+QIDAQABAoIBADNEBiJ7AfVwF0Uu zQNJ9UZXsfqpO1WPsxP57uDCBDOEFvbqvqut2uT4wMaOF6RT++aQjGOizM4Xi481 B97jHuQOc0tQDt0HKD+XKMlAFCopTEPAqlMsC6wNjuLOFWTjUHYeDlsBaLo8ZzJQ uoQ/YFqMnml/whJrBH6J709HdDErNz9gXiTSbIB1hvGx7A1qfuN8WUf3GLIfGBZW 2sa4Ct8Zg/DAnTqtFpAqni+Po/K2U/11A5OjXa9Bfe9+H5GBE/2rdRVCIvRKKdKV 1T7gAUnqfQGt0sVLwCKApit1SwnGatGcKryVWaYnrUMEkaMf127TpWdYF6kCFvXY qTDoxHUCgYEA8fcuIXhtUHdDu+QPnJp07/kOgaeRlZKns2x92XJwXnlOANVA59yI Hu9zHRe/PeTfhHD5d9NfyQtU7ID78t5IQAYBsnSvJ7Gb6Eq6wEbYO9EJBpDwRjNC wRyLHZRKc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DKnUJeycvCRyoihNskpuXqtP3xwRSTtkOG+/6Gzs1Oha19sImYtyZItISkxTX38NlwL5 root@localhost.localdomain
known_hosts
192.168.0.187 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBJe/b5BRbQatRGrnBgs3tY6WEZp+PFrdbB2XRiW1ZuV+YXK85SK8cfebG0c/x1AlJAoi/cfnB2+0VC4XrpbG0Pk=
虚拟机B
authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDSja7k47Dh64IdMxm38yyvVbECaOwByAUQfhScRjhV/uqx4aPKfRTSOSxxwnY2YVI60j6J/Wh4le7xM6DESZH2jlsvbdmcNk91HwyqIpRe3tBJ4J7z3H58SiqVkcl4D99LhfOZg0bK5j3fsB5ehjnyUJV93GjUG4M1o5bXPyc8RYYj9/e81+tnSYhFejgYHIB9lVecLR+Tf8oWOHuRa3Y/z5WuFoy5jJj7VGtogQDRE5D+Gv1m8m4pjv115g9BjooO4Z21n3eJniPr0TJDDKnUJeycvCRyoihNskpuXqtP3xwRSTtkOG+/6Gzs1Oha19sImYtyZItISkxTX38NlwL5 root@localhost.localdomain
- 资料整理完成,开始验证该方案在新的虚拟机上是否可行,把虚拟机A和虚拟机B的
/root/.ssh
目录下的文件都删除,然后再进行测试。删除完成之后,执行ssh 192.168.0.187 echo 'hello'
,需要重新输入密码认证,结果如图6所示
图6
- 把刚才整理好的
id_rsa
、id_rsa.pub
、known_hosts
恢复到虚拟机A,把authorized_keys
恢复到虚拟机B,然后再执行ssh 192.168.0.187 echo 'hello'
测试,发现理想很美满,现实很骨感~,如图7所示
图7
吓得我赶紧问了一下ChatGPT,如图8
图8
修改id_rsa
的权限
[root@localhost .ssh]# chmod 600 id_rsa
[root@localhost .ssh]# ll
总用量 12
-rw-------. 1 root root 1711 6月 24 09:38 id_rsa
-rw-r--r--. 1 root root 408 6月 24 09:38 id_rsa.pub
-rw-r--r--. 1 root root 175 6月 24 09:38 known_hosts
再次测试ssh 192.168.0.187 echo 'hello'
和scp /home/aa.txt root@192.168.0.187:/home/aa.txt
,发现已经可以成功了,如图9所示。(如果这个时候还是一直提示输入rsa
的密钥,可以重新生成ssh-keygen -t rsa
试一下)
图9
- 这个时候我们两台服务器之间免密码传文件的环境就已经搭建好了