rsync+ssh 无服务端无密码同步配置

rsync+ssh   无密码同步   系统服务  

免去rsync server端的繁琐配置,更加灵活易用

  • 使用ssh-keygen生成密匙 -P 快速生成 /root/.ssh/id_rsa.pub.
root@leoiceo#ssh-keygen -t rsa -P ''
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
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:
d7:e7:2a:0d:39:dd:0b:52:7f:91:4e:5c:10:7d:cc:4e root@iZ94bhk13ceZ
The key's randomart image is:
+--[ RSA 2048]----+
|              o* |
|                E|
|              .o+|
|           ..  =.|
|        S .+.o+ .|
|         .= ooo..|
|           = ..o |
|          . ...  |
|           ..    |
+-----------------+

  • 拷贝公钥到远程主机,如果已存在,则追加 >> ~/.ssh/authorized_keys
root@leoiceo#scp /root/.ssh/id\_rsa.pub root@192.168.1.1:/root/.ssh/authorized_keys
root@leoiceo#chmod 600 .ssh/authorized_keys

  • rsync免密码同步 localdir: *本地目录*
    remote
    dir:远程目录
root@leoiceo#rsync -auqz -zrtopg --delete --progress -e 'ssh -p port' local_dir/ root@192.168.1.1:/remote_dir/
root@leoiceo#rsync -auqz -zrtopg --delete --progress -e 'ssh -p port' root@192.168.1.1:/remote_dir/ local_dir/

其他常用参数:

--exclude-from="../conf/exclude.list" 排除一个目录或者文件列表

--exclude-from=FILE 排除一个文件或目录

有密码同步

tar zxf sshpass-1.06.tar.gz  
cd sshpass-1.06  
./configure && make && make install
  • 带密码同步
sshpass -p remotepasswd rsync -avz --delete local_dir -e ssh remoteuser@192.168.5.38:remote_dir  
remoteuser/remotepasswd 是远程的密码  
-avz是打包传送、显示明细、压缩
-e ssh是关键,即over ssh
我们要从本地同步到远程,如果要远程同步到本地,装local_dir换到最后即可
remote_dir 是远程服务器路径  
local_dir 是本地服务器路径