rdist命令同步文件或目录

1.        概述

        两台Unix机器之间复制文件的方法有很多种:

rcp         remote copy,需要注意权限问题;

scp         secure copy,需要注意权限问题;

rsync      一些OS缺省没有,可以通过rshd或者ssh(rsync -e ssh)作为传输通道;

rdist              类似于rsync,可以通过rshd或者ssh作为传输通道;

tar          使用tar和untar方式,可以通过rshd或者ssh作为传输通道;

       本文讨论如何使用rdist同步文件或者目录。

 

2.        命令行方式格式

# rdist -Rc <filename> remotehost:<Destination>

-R                 将删除目标主机上其它的多于的文件。那些在目标主机目录中的文件,如果在源主机的目录中不存在,将被删除。-R参数用来同步两台主机的目录。

-c                 将使用<filename> remotehost:<filename>命令方式

filename       本地文件或者文件列表

remotehost   远程主机名(/etc/hosts或者DNS需要有对应的地址解析)

Destination   远程主机文件或者目录

 

3.        文件拷贝例子

# rdist -c /home/support/rdistest.tar s70:/home/support/temp/rdistest.tar

rdist: Updating the host s70.

rdist: installing: /home/support/rdistest.tar

       将文件/home/support/rdistest.tar 复制到一台名为s70 的远程主机,并且将该文件放在/home/support/temp/ 目录下

 

4.        目录拷贝例子

# rdist -Rc /home/support/scripts s70:/home/support/scripts

      将从源主机的/home/support/scripts目录同步到目的主机(s70)的/home/support/scripts目录

 

5.        批量文件方式

# rdist -f distfile 

  •  Distfile常用格式:

[Label:] SourceList -> DestinationList SubcommandList

HOSTS =( host1 host2 )           #定义主机列表(DestinationList)

FILES = ( /bin                           #定义更新的目录和文件(SourceList)

/usr/games                 

/usr/include/{*.h,{stand,sys,vax*,pascal,machine}/*.h}

/usr/man/man?

)

PartLabel:

${FILES} ->${HOSTS}              

       install -R

       except /usr/lib/${EXLIB}  ;

       except /usr/games/lib  ;

       special /usr/sbin/sendmail "/usr/sbin/sendmail.bz"  ;

 

  • 常用SubCommand:

install Options [OptionalDestName];        这里的install选项替代了rdist中的命令选项,例如install -R和rdist -R的作用一样;

except NameList;                                   表示不同步NameList中的所有文件和目录;

special NameList "String";                       如果NameList文件被同步到远程主机后,在远程主机执行String对应的脚本;

 

  • 例子

# rdist -R -f distfile

# cat diskfile

HOSTS =( host1 host2 )

FILES = ( /bin

/usr/games

/usr/include/{*.h,{stand,sys,vax*,pascal,machine}/*.h}

/usr/man/man?

)

Part1Label:

${FILES} ->${HOSTS}

       except /usr/lib/${EXLIB}  ;

       except /usr/games/lib  ;

       special /usr/sbin/sendmail "/usr/sbin/sendmail.bz"  ;

 

  • Label的作用

       If no name arguments are specified, rdist will update all of the files and directories listed in distfile. Otherwise, the argument is taken to be the name of a file to be updated or the label of a command to execute. If the label and file names conflict, it is assumed to be a label. These may be used together to update specific files using specific commands.

 

6.        rdist的注意事项

      需要在目的主机上在 /etc/hosts.equiv或者/.rhosts加对方主机的ip,在/etc/inetd.conf放开rshd服务.例如:

# cat /.rhosts

sourcehost    root

# grep rshd /etc/inetd.conf

shell   stream  tcp6    nowait  root    /usr/sbin/rshd         rshd

 

7.        rdist使用sshd通道

  • 通过-P的参数提供相关的传输通道,命令格式

# rdist -R -P /usr/bin/ssh -f distfile

       必须让接收方主机上的 sshd 信任你正在上面分发文件的主机。要做到这一点,可以为主控机生成一个纯文本的密钥,并保存每台接收方主机上 ~root/.ssh/authorized_keys 文件中的公共部分的副本。

  • 在源主机上创建SSH2的DSA密钥对:

$ ssh-keygen -t dsa

Generating public/private dsa key pair.

Enter file in which to save the key (/home/support/.ssh/id_dsa):

Created directory '/home/support/.ssh'.

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

Your identification has been saved in /home/support/.ssh/id_dsa.

Your public key has been saved in /home/support/.ssh/id_dsa.pub.

The key fingerprint is:

4f:c5:00:9a:bf:c7:67:97:87:8c:bf:3a:9c:a6:e7:53 support@hostname

       使用默认的文件路径~/.ssh/id_dsa.输入不同于你帐号密码的passphrase,再输入一遍以便确认.警告:当提示输入 passphrase的时候,可以直接输入回车,不使用passphrase保护DSA密钥.但并不推荐这么做.无保护的DSA密钥对使得获得你帐号权限的入侵者能够进入到所有罗列在~/.ssh/id_dsa文件中的机器.公钥被写入到~/.ssh/id_dsa.pub文件.私钥被写入到~/.ssh /id_dsa文件.切记不要把私钥给任何人.

  • 改变~/.ssh文件的权限

$ chmod 755 ~/.ssh

  • 修改~/.ssh/authorized_keys文件

      将源主机文件~/.ssh/id_dsa.pub文件的内容拷贝到目标主机的~/.ssh/authorized_keys.如果目标主机上已经存在~ /.ssh/authorized_keys文件,将~/.ssh/id_dsa.pub的内容追加到~/.ssh/authorized_keys.

  • 修改目标主机上authorized_keys文件的权限

$ chmod 600 ~/.ssh/authorized_keys

 

 

 

详细参考:http://www.kuqin.com/aixcmds/aixcmds4/rdist.htm

posted @ 2009-12-10 17:08  hibernate3例子  阅读(394)  评论(0编辑  收藏  举报