活到老学到老  

记录遇到问题的点点滴滴。

ssh密匙登录方法及rsync加密传输同步文件设置

7年前发布  · 2532 次阅读
  ssh  rsync 

首先建立本机的公私密钥

[root@host1 root]# ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/root/.ssh/id_dsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_dsa.
Your public key has been saved in /root/.ssh/id_dsa.pub.
The key fingerprint is:
9b:40:4c:a1:9c:d0:10:d6:bf:1b:5f:0d:8c:6b:0c:0a root@host1

注意不要输入passphrase

然后将公钥文件传送到需要登陆的服务器

[root@host1 root]# scp .ssh/id_dsa.pub root@host2:id_dsa.pub
root@host2.verycd.com's password:
id_dsa.pub           100% |*****************************|   600       00:00

登录那台服务器

[root@host1 root]# ssh -v root@host2

将刚才的公钥文件内容添加到用户的.ssh目录下的authorized_keys文件中

[root@host2 root]# cat id_dsa.pub >> /root/.ssh/authorized_keys

然后更改该文件权限

[root@host2 root]# chmod 600 /root/.ssh/authorized_keys

退出

[root@host2 root]# exit
Connection to host2 closed.
[root@host1 root]#

再次登陆即不再需要密码了

[root@host1 root]# ssh host2
Last login: Wed Nov 17 17:55:34 2016 from 102.129.xxx.xxx
[root@host2 root]#

 配置好ssh密匙验证后,现在记录一下rsync的设置。

一般采用源码安装,安装后可能没有配置文件(只有服务器端才需要配置文件,客户端是不需要配置配置文件的),需要在/etc下创建rsyncd.conf文件,其内容:

secrets file=/etc/rsyncd.secrets
motd file=/etc/rsyncd.motd
read only=yes
list=yes
uid=root
gid=root
use chroot=no
max connections=3
log file=/var/log/rsyncd.log
pid file=/var/log/rsyncd.pid
lock file=/var/run/rsync.lock
[share]
comment=share file
path=/home/www

以上配置都是指在服务器端配置,配置完毕后还需要在客户端安装该软件但是不用配置,只需要在客户端执行rsync命令,如下:

rsync -vzrtopg --delete --progress root@192.168.20.20:/home/www/ /home/bakup

说明:

/home/www/  指服务器端要同步的根目录,记得一定要在后面加"/";

/home/bakup 指同步服务器端文件到客户端的目录;

-vzrtopg:
             v是verbose,
             z是压缩,
             r是recursive,
             topg都是保持文件原有属性如属主、时间的参数
 --progress  是指显示出详细的进度情况
 --delete    是指如果服务器端删除了这一文件,那么客户端也相应把文件删除,保持真正的一致

以上就完成了rsync加密传输同步文件的设置。