一、安装sshpass
yum install sshpass -y
二、生成rsa公钥和私钥
[root@glusterfs03 .ssh]# ssh-keygen -t rsa -f /root/.ssh/id_rsa
[root@glusterfs03 .ssh]# ll /root/.ssh/ -a total 12 drwx------ 2 root root 57 Aug 22 16:34 . dr-xr-x---. 5 root root 252 Aug 22 16:22 .. -rw------- 1 root root 1675 Aug 22 16:25 id_rsa -rw-r--r-- 1 root root 398 Aug 22 16:25 id_rsa.pub -rw-r--r-- 1 root root 358 Aug 22 10:20 known_hosts [root@glusterfs03 .ssh]#
三、将公钥id_rsa.pub复制到目标服务器上
[root@glusterfs03 .ssh]# sshpass -phl95test123 ssh-copy-id -i /root/.ssh/id_rsa.pub -o StrictHostKeyChecking=no -f root@172.28.17.221
-p:ssh密码
-i:指定公钥文件
-f:强制覆盖
[root@localip .ssh]# ll /root/.ssh/ -a total 12 drwx------. 2 root root 48 Aug 22 16:39 . dr-xr-x---. 5 root root 4096 Aug 22 09:15 .. -rw------- 1 root root 398 Aug 22 16:39 authorized_keys -rw-r--r--. 1 root root 541 Aug 22 10:30 known_hosts
目标服务器多了一个authorized_keys文件
四、执行sync命令,不需要输入密码即可同步文件
[root@glusterfs03 synctest]# rsync -P -artzhe 'ssh -p 22' --delete root@172.28.17.221:/home/hl95/webapps/fs1/100000/audio/ /home/synctest/ receiving incremental file list ./ 202206071350491001123456789011234561779163625719623.mp3 59.66K 100% 56.90MB/s 0:00:00 (xfr#1, to-chk=32/34) 202206071401451001123456789011234561779163625719623.mp3 35.76K 100% 5.68MB/s 0:00:00 (xfr#2, to-chk=31/34) 202206071559191001123456789011234561779163625719623.mp3 50.73K 100% 6.91MB/s 0:00:00 (xfr#3, to-chk=30/34) 20220607/ 20220607/202206071130121001123456789011234561779163625719623.mp3
rsync -P -artzhe 'ssh -p 22' --delete /home/synctest/ root@172.28.17.221:/home/synctest-1/
将本地的指定目录同步到目标服务器上指定目录