且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Rsync+ssh同步服务器

更新时间:2022-08-13 22:29:58

 环境:it168(客户端)192.168.0.11

           centos(服务器端) 192.168.0.40

第一步再it168和centos上创建相同的用户web

it168上执行

useradd -g 48 -u 100 -d /home/web/ -s /bin/bash web

mkdir -p /www/htdocs/

echo "test" >index.html

chown -R web /www

centos上执行相同的指令

useradd -g 48 -u 100 -d /home/web/ -s /bin/bash web

mkdir -p /www/htdocs/

 

第二步配置ssh的key认证

it168和centos上同时执行

vi /etc/ssh/sshd_config

PasswordAuthencation yes 修改成PasswordAuthencation no

注意:等配置完了sshkeygen以后再然后同时执行servcie sshd restart(****)

 

Centos上执行

su - web

ssh-keygen -t rsa

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 /home/web/.ssh//id_rsa.
Your public key has been saved in /home/web/.ssh/id_rsa.pub.
The key fingerprint is:
4d:dd:48:af:76:c2:ba:a8:bc:20:f3:28:1d:6a:28:53 web@centos

cd /home/web/.ssh/

mv id_rsa.pub authorizen_keys

scp ./id_rsa web@it168:/home/web/.ssh/id_rsa

此时候用root执行service sshd restart就可以了

 

第三步利用rsync同步数据

rsync -v -a -z -e ssh --delete /www/ centos:/www/

此时候复制完成!!

注意:如果遗漏了/www/后面的一个/的话,则将在放置源文件的目标系统上创建一个新的子目录,而不是更新目标系统的/www目录中的文件