9单元-Linux系统中SSH服务的进阶知识——文件传输

                                  Linux系统中的文件传输

1.文件复制与下载

实验环境

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

  • client_westos.test.com          172.25.254.10
  • sever_node1.westos.com     172.25.254.20

scp命令 

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

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

实验步步骤

  1. 在node1 主机建立实验素材

    touch westos

    mkdir westosdir

  2. 测试

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

scp westos 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/westos_rhel8  /root/Desktop

rsync 命令

a) rsync和scp命令的对比

实验素材:

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

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)在主机之间建立免密登陆使远程文件传输可以直接执行

ssh-keygen           # 生成密钥   (node1中)

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

3)创建测试脚本

vim check_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 check_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 传输运行脚本
rsync 传输 运行脚本

 

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

b)rsync

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

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

rsync 命令

  • -r    #制目录
  • -l    #制链接
  • -p   #制权限
  • -t    #制时间戳
  • -o   #制拥有者
  • -g   #制拥有组
  • -D   #制设备文件

实验环境:

rhel8

watch -n 1 ls -lR /root/Desktop

rhel7

touch /root/Desktop/file{1..5}

chmod 777 /root/Desktop/*

useradd westos

chown westos /root/Desktop/*

ln -s /root/Desktop/file1 /root/Desktop/file

执行命令看效果

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

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

rsync -rl /root/Desktop/ root@172.25.254.20:/root/Desktop          ##链接

rsync -rlp /root/Desktop/ root@172.25.254.20:/root/Desktop        ##权限

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

rsync -rlpogt /root/Desktop/ root@172.25.254.20:/root/Desktop   ##步时间戳

rsync -rD /dev/pts root@172.25.254.20:/root/Desktop                    ##步设备文件

 

2.文件的归档压缩

1)归档

tar 命令

  • c           #
  • f             #定文件名称
  • x            #
  • t             #
  • r             #归档文件中添加文件
  • --get       #档指定文件
  • --delete  #除指定文件
  • -C           #定解档路径

实验步骤

  1. tar cf etc.tar /etc/
  2. tar tf etc.tar
  3. tar rf etc.tar westos_rhel8
  4. tar xf etc.tar
  5. tar f etc.tar --get westos_rhel8
  6. tar f etc.tar --delete westos_rhel8
  7. tar xf etc.tar -C /root/Desktop

2)的压缩

zip 命令

zip -r etc.tar.zip etc.tar        #zip式压缩

unzip etc.tar.zip                   #zip式解压缩

gzip 命令

gzip etc.tar                           #gzip式压缩

gunzip etc.tar.gz                  #gzip式解压缩

bzip2 etc.tar                         #bzip2格压缩

bunzip2 etc.tar.bz2              #bzip2格解压缩

xz  etc.tar                              #xz式压缩

unxz etc.tar.xz                      #xz式解压缩

3)tar+压缩

gzip

tar zcf etc.tar.gz /etc

tar zxf etc.tar.gz

bzip2

tar jcf etc.tar.bz2 /etc tar jxf etc.tar.bz2

xz

tar Jcf etc.tar.xz /etc tar Jxf etc.tar.xz

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值