300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > Linux系统中的文件传输(scp命令 rsync命令)

Linux系统中的文件传输(scp命令 rsync命令)

时间:2018-09-09 17:27:26

相关推荐

Linux系统中的文件传输(scp命令 rsync命令)

实验环境

需要2台主机并且保证这两台主机是可以通信的

linux_westos : 172.25.254.10

westos_lue: 172.25.254.20

systemctl disable firewalld

systemctl stop firewalld

1.scp命令

scp 本地文件 远程主机用户@远程主机ip:远程主机目录的绝对路径scp 远程主机用户@远程主机ip:远程主机文件的绝对路径 本地文件

实验:

1.在172.25.254.10主机中建立实验素材

touch westosfile

mkdir westosdir

touch westosdir/test{1..5}

echo "hello" > westosfile

2.测试

a)把本地文件复制到远程主机 (上传)(172.25.254.10)

scp westosfile root@172.25.254.20:/root/Desktop

scp -r westosdir root@172.25.254.20:/root/Desktop -r 表示复制 目录

scp -q westosfile root@172.25.254.20:/root/ Desktop -q 传输文件时不显示进度

scp westosfile root@172.25.254.20:/root/Desktop

scp -r westosdir root@172.25.254.20:/root/Desktop -r 表示复制 目录

scp -q westos root@172.25.254.20:/root/ Desktop -q 传输文件时不显示进度

b)把远程文件复制到本地(下载)

scp root@172.25.254.20:/root/Desktop/testdir /root/Desktop

2.rsync

rsync和scp命令的对比

实验素材:

1)172.25.254.10:(du -sh *)

dd if=/dev/zero of=/root/Desktop/westosfile1 bs=1M count=10

dd=截取

if=inputfile

of=outputfile

bs=blocksize

count=个数

dd if=/dev/zero of=/root/Desktop/westosfile2 bs=1M count=20

dd if=/dev/zero of=/root/Desktop/westosfile3 bs=1M count=30

2)在主机之间建立免密登陆使远程文件传输可以直接执行

rhel8中:

ssh-keygen 生成密钥

ssh-copy-id -i /root/.ssh/id_rsa.pubroot@172.25.254.20

3)创建测试脚本

vim /mnt/test_scp.sh 检测scp传输时间

time scp -qr /root/Desktop root@172.25.254.20:/root/Desktop

time scp -qr /root/Desktop root@172.25.254.20:/root/Desktop

time scp -qr /root/Desktop root@172.25.254.20:/root/Desktop

vim /mnt/test_rsync.sh 检测rsync的传输时间

time rsync -racq /root/Desktop root@172.25.254.20:/root/Desktop

time rsync -racq /root/Desktop root@172.25.254.20:/root/Desktop

time rsync -racq /root/Desktop root@172.25.254.20:/root/Desktop

4)执行

scp

sh /mnt/test_scp.sh

real 0m1.334s

user 0m0.210s

sys 0m0.490s 第一次系统执行时间

real 0m1.642s

user 0m0.412s

sys 0m0.383s 第二次系统执行时间

real 0m1.586s

user 0m0.309s

sys 0m0.497s 第三次系统执行时间

以上执行效果我们可以看出scp三次执行时间几乎一致

rsync执行

sh /mnt/test_rsync.sh

real 0m1.603s

user 0m0.399s

sys 0m0.557s 第一次系统执行时间

real 0m0.329s

user 0m0.012s

sys 0m0.010s 第二次系统执行时间

real 0m0.348s

user 0m0.014s

sys 0m0.022s 第三次系统执行时间

以上执行效果我们可以看出rsync三次执行时间后两次远远小与第一次

rsync用法

rsync 文件 远程用户@远程主机ip:远程主机目录

rsync 远程用户@远程主机ip:远程主机目录 文件路径

rsync

-r 复制目录

-l 复制链接

-p 复制权限

-t 复制时间戳

-o 复制拥有者

-g 复制拥有组

-D 复制设备文件

实验环境

在linux_westos中

watch -n 1 ls -lR /mnt/

在westos_lue中

touch westosfile{1..5}

ln -s /root/Desktop/westosfile1 /root/Desktop/westoslink

chown westos.westos *(chown westos /root/Desktop *)

chmod 777 *

执行命令看效果:(linux_westos)

rsync -r root@172.25.254.20:/root/ Desktop /mnt同步目录本身其目录中的文件

rsync -r root@172.25.254.20:/root/ Desktop/ /mnt 只同步目录中的文件

rsync -rl root@172.25.254.20:/root/ Desktop /mnt 同步链接

rsync -rlp root@172.25.254.20:/root/Desktop /mnt 同步权限

rsync -rlpog root@172.25.254.20:/root/Desktop /mnt 同步用户组

rsync -rlpogt root@172.25.254.20:/root/Desktop /mnt 同步时间

rsync -rD /dev/pts root@172.25.254.20: /dev/pts /mnt同步设备文件

rsync -r root@172.25.254.20:/root/ Desktop /mnt同步目录本身其目录中的文件

rsync -r root@172.25.254.20:/root/ Desktop/ /mnt 只同步目录中的文件

rsync -rl root@172.25.254.20:/root/ Desktop /mnt 同步链接

rsync -rlp root@172.25.254.20:/root/Desktop /mnt 同步权限

rsync -rlpog root@172.25.254.20:/root/Desktop /mnt 同步用户组

rsync -rlpogt root@172.25.254.20:/root/Desktop /mnt 同步时间

rsync -rD /dev/pts root@172.25.254.20: /dev/pts /mnt同步设备文件

文件的归档压缩

1.文件归档

tar

c 创建

f 指定文件名称

x 解档

t 查看

-v 显示过程

r 向归档文件中添加文件

--get 解档指定文件

--delete 删除指定文件

-C 指定解档路径

实验步骤:

tar cf mnt.tar /mnt/

tar tf etc.tar

tar rf etc.tar westos_rhel8

tar xf etc.tar

tar f etc.tar --get westos_rhel8

tar f etc.tar --delete westos_rhel8

tar xf etc.tar -C /root/Desktop

实验素材

tar cf mnt.tar /mnt/

tar tvf mnt.tar

tar rf mnt.tar westosfile

tar xf mnt.tar

tar f mnt.tar --get westosfile

tar f mnt.tar --delete westosfile

tar xf mnt.tar -C /root/Desktop

2.文件的压缩

zip

zip -r mnt.tar.zip mnt.tar zip格式压缩

unzip tar mnt.zip zip格式解压缩

zip -r mnt.tar.zip mnt.tar zip格式压缩

unzip tar mnt.zip zip格式解压缩

gzip

gzip mnt.tar gzip格式压缩

gunzip mnt.tar.gz gzip格式解压缩

bzip2 mnt.tarbzip2格式压缩

bunzip2 mnt.tar.bz2 bzip2格式解压缩

xz mnt.tarxz格式压缩

unxz mnt.tar.xzxz格式解压缩

gzip mnt.tar gzip格式压缩

gunzip mnt.tar.gz gzip格式解压缩

bzip2 mnt.tarbzip2格式压缩

bunzip2 mnt.tar.bz2 bzip2格式解压缩

xz mnt.tarxz格式压缩

unxz mnt.tar.xzxz格式解压缩

3.tar+压缩

gzip

tar zcf mnt.tar.gz /mnt

tar zxf mnt.tar.gz 解档

bzip2

tar jcf mnt.tar.bz2 /mnt

tar jxf mnt.tar.bz2

xz

tar Jcf mnt.tar.xz /mnt

tar Jxf mnt.tar.xz

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。