1.场景描述
内网环境,需要同步17服务器的文件到10服务器进行备份。因为每次输入密码比较繁琐,如果实现免密登录后,即可简化脚本。
要求:需要2台服务器-免密登录
2.方案分析
(1)现状:登录需要输入密码
(2) 查看10和17密钥情况
cd ~/.ssh/
id_rsa 私钥文件
id_rsa.pub 公钥文件
authorized_keys 希望登录的机子的公钥信息粘贴进去,即可完成互信
[root@localhost file_backup]# cd ~/.ssh/
[root@localhost .ssh]# ll
total 16
-rw-r--r-- 1 root root 394 Oct 19 2023 authorized_keys
-rw------- 1 root root 1679 Oct 19 2023 id_rsa
-rw-r--r-- 1 root root 408 Oct 19 2023 id_rsa.pub
-rw-r--r-- 1 root root 1218 Dec 18 11:52 known_hosts
[root@localhost .ssh]#
(3)密钥已生成,只需要公钥给远端服务器即可
ssh-copy-id root@192.168.3.10
ssh-copy-id root@192.168.3.17
3.实验结果
命令:
ssh root@192.168.3.10
ssh root@192.168.3.17
ok,实现免密登录。
4.总结
1)生成密钥,存储位置:~/.ssh/
执行命令 cd ~/.ssh/
目录里面如果没有公钥和秘钥文件,则执行命令来生成: ssh-keygen -t rsa
执行后,目录中有3个文件:
id_rsa 生成的私钥文件
id_rsa.pub 生成的公钥文件
authorized_keys 将想登录该机器,它的id_rsa.pub里面的加密内容,即公钥信息粘贴进去,即可完成互信
2)复制公钥到远程登录机子:
将公钥复制给远端机器,远程才可登录。
方式:1)可用命令: ssh-copy-id root@目的ip
2)手动复制: vi打开粘贴 或追加即可
cat ~/.ssh/id_rsa.pub >>~/.ssh/authorized_keys
scp -r ~/.ssh/ root@ip:~/
相关内容:(以下aa 和bb是两个ip)
1)aa服务器-不可连bb信息:
[root@neptune .ssh]# ll
total 16
-rw-r--r-- 1 root root 408 Oct 19 2023 authorized_keys
-rw------- 1 root root 1679 Oct 19 2023 id_rsa
-rw-r--r-- 1 root root 394 Oct 19 2023 id_rsa.pub
-rw-r--r-- 1 root root 1047 Sep 13 15:14 known_hosts[root@neptune .ssh]# cat known_hosts
192.168.3.x09 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBJ8ZR3E6nxaNK+K89Oe/TrHUHboTpyNwTcz54iKmS+b3oJ+lHUYg3v43Xpp+S2EPUyUk4dCKqYmP94txy1axSU4=
60.205.154.xx1 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBCZr2QxIOUkDN5yokZYuky5uh4cwOnDWoELsXLxFdsaDHakfkI1uWS35WGR5QJ9yoZb98OSipmzHEWqJaw7a2Xw=[root@neptune .ssh]#
[root@neptune .ssh]# ssh root@192.168.3.bb
The authenticity of host '192.168.3.bb (192.168.3.bb)' can't be established.
ECDSA key fingerprint is SHA256:uu2ZFgvQEKVqePrdgp2LvNS7gLN9M7pSY+Jh2J0cbG4.
ECDSA key fingerprint is MD5:6a:48:27:74:b8:50:ee:17:ef:08:32:f8:74:bb:98:3c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.3.bb' (ECDSA) to the list of known hosts.
root@192.168.3.bb's password:
Permission denied, please try again.
root@192.168.3.bb's password:
Permission denied, please try again.
root@192.168.3.bb's password:
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
[root@neptune .ssh]#[root@neptune .ssh]# cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCzAap56BBnnXkUn7dr7ZXVKLyvK0CH58q7IopnrULHa0ZfLpiRAvG2vu+ZoH2LCDpbSFUnJfwcFqsx+Iwp4HAFXx2Z+pL5HAnzzCbzfC/sTxbMXQSREPLnFgbQbST7Iwf0dLpT8pyhgdJmEDl5okZvB+NDnGpLs7NP+RtW/eV0+axxUZvxUm+KX8iVvABQVH1GnFlrYvNVR6DLKXiLVPy+rxoXiexq+VbSwbjR9ASB5AJhdNnBZpcY0zP0ctUe7N3UVMEowrhJTZQMAL/rYg8+d4AljIOt4Rw198R0q7ZvniJHHL+3qt9EQxDyd6eAPMTZuI4wPAC9XHsyRGaWpqJH root@neptune
[root@neptune .ssh]#[root@neptune .ssh]# cat authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC0eQP71u4EdtSVqYYgUtGCP5+HHY0uhXkHQC0ZX33a0egaxzhzKFBFlklFky+gsAvn78G/w1bnQSWBDNLsEI/DrjXBYxFP++1tEzY6YIAEA0hykGuU7jvhw3hXBtEts8JyfugvQ33Rwz6wBrR6SYmC7LE92LpsLLb9qY/QzrRe7XKjg0iFLjU68kOqyBre4Biv+Pw3bIIun/RdcfkmmwyYV9x69+s6krXzp4KCpOhO4uDpvE3wzylrAtdAgtsAzWey/2DMUYGS9VEK2E1U9tH9vtShgWoXfvp59ZFG4XYU9mEinrNMRLS0dmzKuGGH31+ryEfPoXwfd0lq34HN4vjR root@localhost.localdomain
[root@neptune .ssh]#
2)bb服务器-可连aa
[root@localhost .ssh]# ssh root@192.168.3.aa
ssh_exchange_identification: read: Connection reset by peer
[root@localhost .ssh]# cat authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCX/JE3WC83QybarhzctFiO2OWv1dDlfKS/EXijDf9N6bTFSSmrdCfkCEORKt1xI0J/ODO6jgGfXYDNxSl smOtNyvk8qFppllBCPPyv5Dha8XaTWgTiM3OFHN8SdKUILaF4JxLe14MJ4008Z7YzZuHfaY9kj4BJFXDXwjNxLD7qcgKhmgu7PsS8Mr95/s5t6VSEKEz6uz 6LPO9G1onc184JXamdRK7xHj3LrgJj6/KydxLY0hs3iAbkpnWjF6rWPWTcuPFymOrghzxjSkufmG5Yjp/sCYFlxR4sEyO7KjFR6gXPIZKLh2WwPQKqeBLAf qccujGUU8JQ6Vmq+6yEZ7JP root@localhost.localdomain
[root@localhost .ssh]# cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCmM2NJRlyom+eImQ2zGItwOMckc01mMi7nWs8eZ7FWs8LSPU+DAj556xJrj1WjIcR8S/BeMqsc0PlXB2r eqdNfbtmaFFInd1p7oVcDy3BIMbF4f9oGa9d3VnPdO2P3ENEVYCPoDTYgYNwKKK/FeuppHqUNTj9sb8zG1eELyy7fcBVovSTVfFU1/hyIPGjFUK3FIoLEro 2F9vA5nPbaxkZTCWT8L0ci4ByJJlqofzba0RsydG8vpbvPgA4+MtbG7dSOiS215MpZZUYEPdPHEClTg4WWkplWwGTkT7ESVPeNzIhRxnJKFlJbjlvFWjU0/ GD2Fk4jvLUKvL35re8q/Lj1 root@localhost.localdomain
[root@localhost .ssh]#[root@localhost ~]# cd ~/.ssh
[root@localhost .ssh]# ll
total 16
-rw-r--r-- 1 root root 408 Oct 19 2023 authorized_keys
-rw------- 1 root root 1675 Oct 19 2023 id_rsa
-rw-r--r-- 1 root root 408 Oct 19 2023 id_rsa.pub
-rw-r--r-- 1 root root 870 Dec 16 11:57 known_hosts
[root@localhost .ssh]# cat known_hosts
192.168.3.aa ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBCH67mqngwZ1AiJf2kmi3dqwWcUz5i0iD7 LFfwPM1yn898THwTH8oZJEkUouuspyuZe/qDHwHzxJcsgWd94JpG0=