rsync 实现 ssh 免密码登陆同步文件

安装环境

服务端:192.168.1.113
客户端:192.168.1.114
同步方式:rsync
需求:114主机上 /opt/image/ 自动同步113主机上 /home/image/

服务端与客户端都要安装 rsync

yum install rsync –y
 
systemctl start rsyncd    #启动
systemctl enable rsyncd   #增加开机启动

服务端 192.168.1.113

1、服务器 创建同步账号

useradd rsync

passwd rsync   #设置密码

New password: 
BAD PASSWORD: The password fails the dictionary check - it is based on a dictionary word
Retype new password: 
passwd: all authentication tokens updated successfully.

2、创建同步文件夹 

mkdir -p /home/image
chown rsync:rsync /home/image  #授权

客户端 192.168.1.114

1、生成密钥

ssh-keygen -t rsa
#结果
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): #回车
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): #回车
Enter same passphrase again: #回车
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:
d2:53:35:52:53:fa:f3:e3:5b:89:0d:1b:0c:d4:2d:9c root@kvm114
The key's randomart image is:
+--[ RSA 2048]----+
|          .oB.+  |
|          .o E . |
|          ... .  |
|       . .  o.   |
|      . S    +o  |
|       . .    *o.|
|             o +o|
|              . o|
|               o.|
+-----------------+

2、查看生成的文件 id_rsa(私钥)、id_rsa.pub(公钥)

ll /root/.ssh/

#结果
-rw------- 1 root root 1679 8月  28 11:44 id_rsa
-rw-r--r-- 1 root root  393 8月  28 11:44 id_rsa.pub

3、把公钥复制到 服务端上 192..168.1.113

ssh-copy-id rsync@192.168.1.113
#显示结果 成功
rsync@192.168.1.113's password:  #输入rsync登陆密码

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'rsync@192.168.1.113'"
and check to make sure that only the key(s) you wanted were added.

服务端 192.168.1.113 修改权限

chmod 700 /home/rsync/.ssh
chmod 600 /home/rsync/.ssh/authorized_keys


#修改配置文件
vi /etc/rsyncd.conf
 
uid = rsync
gid = rsync
use chroot = no
read only = no
max connections = 200
transfer logging = yes
log file =/var/log/rsyncd.log
timeout = 900

4、客户端 远程登入

ssh rsync@192.168.1.113

#结果
Last login: Wed Aug 28 11:22:17 2019 from 192.168.1.114  #成功登入

6、开启同步

rsync -avz /opt/image/ rsync@192.168.1.113:/home/image/

查看系统日志

tail /var/log/secure -n 50

 

©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值