1.实验环境
本次实验需要两台虚拟机rhel7_node1和rhel8_node1相当于两台独立的电脑并且这两台主机是可以相互通信的
rhel7_node1设置的网络ip为192.168.43.24
rhel8_node1设置的网络ip为192.168.43.25
首先通过在rhel7_node1中键入命令ssh root 192.168.43.25来远程访问rhel8_node1这台主机这样可以更容易看到实验结果
2.scp命令
用法:
scp 本地文件 远程主机用户@远程主机ip:远程主机目录 ##用来复制本地文件到远程用户
scp -q westos [email protected]:/mnt/ ##-q表示传输文件时不显示进度
scp 远程主机用户@远程主机ip:远程主机目录 本地文件 ##复制远程文件到本用户
图示:
传输文件
-r
传输目录
传输目录需添加参数-r
scp -r westosdir [email protected]:/mnt/ ##-r表示复制目录
复制远程用户文件到本地
3.rsync命令
rsync 文件 远程用户@远程主机ip:远程主机目录
rsync 远程用户@远程主机ip:远程主机目录 文件路径
rsync 参数:
| -q | 静默方式,不显示进度条 |
|---|---|
| -r | 复制目录 |
| - l | 复制链接 |
| - o | 复制拥有者 |
| -g | 复制拥有组 |
| -t | 复制时间戳 |
| -p | 复制权限 |
| -D | 复制设备文件 |
例如:rsync -raCq /mnt [email protected]/mnt
实验环境:
在rhel8中 watch -n 1 ls -lR /mnt
在rhel7 中touch /mnt/file{1…5}
chmod 777 /mnt/*
useradd westos
chown westos /mnt/**
ln -s /mnt/file1 /mnt/file
效果:复制所有文件
复制文件和链接
复制文件链接和用户时间等
-D同步设备文件
## 4.rsync和scp命令的对比
实验素材:
1)在rhel7中建立文件
dd if=/dev/zero of =/mnt/file1 bs=1M count10 ##dd=截取,if =inputfile of=outputfile bs=blocksize doun=块的个数
dd if=/dev/zero of =/mnt/file2 bs=1M count20
dd if=/dev/zero of =/mnt/file3 bs=1M count30
2)在主机之间建立免密登录使文件可以直接执行
ssh-****** ##生成**
ssh-copy-id -i /root/.ssh/id_rsa.pub. [email protected]
3)创建测试脚本
在文件check_scp.sh中输入
time scp -qr /mnt [email protected]:/mnt
time scp -qr /mnt [email protected]:/mnt
time scp -qr /mnt [email protected]:/mnt
在文件check_rsync.sh中输入
time rsync -raCq /mnt [email protected]/mnt
time rsync -raCq /mnt [email protected]/mnt
time rsync -raCq /mnt [email protected]/mnt
执行效果
从图中可以看出scp三次执行时间几乎一致,rsync执行时间后两次执行时间远远小于第一次
4.文件的归档压缩
1.文件的归档
tar
| -c | 创建 |
|---|---|
| -f | 指定文件名称 |
| -x | 解档 |
| -t | 查看 |
| -r | 向归档文件中添加文件 |
| –get | 解档指定文件 |
| –delete | 删除指定文件 |
| -c | 指定解档路径 |
创建归档
查看tar tf etc.tar
复制文件到归档tar rf etc/.tar rhdhat将文件取出
取出单个文件
删除归档中的文件
tar xf etc.tar -C /mnt 指定解档路径
5.文件的压缩
zip
zip -r etc.tar.zip etc.tar ##zip格式的压缩
unzip etc.tar.zip ##zip格式的解压缩
gzip格式的压缩gzip etc.tar
gunzip etc.tar.gz gzip格式的解压缩
bzip2 格式的压缩
bziip2 etc.tar.bz2
bunzip2 etc.tar.bz2 解压缩
xz etc.tar
unxz etc.tar解压缩
6.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