首先你所操作的各台linux机器间必须设置了ssh免密码登录,具体方法可上网查看。将文件从一台linux机器拷贝到多台分为以下几个步骤:
第一步:创建脚本文件remotecopy.sh
#!/bin/bash while getopts f: OPT; do case $OPT in f|+f) files="$OPTARG $files" ;; *) echo "usage: `basename $0` [-f hostfile] <from> <to>" exit 2 esac done shift `expr $OPTIND - 1` if [ "" = "$files" ]; then echo "usage: `basename $0` [-f hostfile] <from> <to>" exit fi for file in $files do if [ ! -f "$file" ]; then echo "no hostlist file:$file" exit fi hosts="$hosts `cat $file`" done for host in $hosts; do echo "do $host" scp $1 root@$host:$2 done
第二步:创建主机列表文件hosts
vim hosts 192.16.8.103 192.16.8.101
第三步:在传输文件所在节点上运行脚本命令
./remotecopy.sh -f /usr/local/host /usr/local/test /usr/local/
就可以将文件/usr/local/test拷贝到其他主机的/usr/local/目录下了