(server主机172.25.254.101,client主机172.25.254.201)

系统中的文件传输

一、基于sshd服务的scp远程传输命令:

scp    file     username@ip:/dir    ##上传
scp    username@ip:/dir/file  /dir    ##下载

1、将server端桌面下的fig目录上传到client主机的桌面上。这里使用了client主机的sshd服务。

[root@server1 Desktop]# scp -r /root/Desktop/fig/ root@172.25.254.201:/root/Desktop/
root@172.25.254.201's password: 
   
[root@client1 Desktop]# pwd
/root/Desktop
[root@client1 Desktop]# ls
fig

2、在client端下载server端的fid目录到当前目录。这里使用了server主机的sshd服务。

[root@client1 mnt]# scp -r root@172.25.254.101:/mnt/fid/ .
root@172.25.254.101's password: 
[root@client1 mnt]# ls
fid

tip1:如果上传、下载的是文件则去掉-r。

tip2:scp在上传、下载目录时会将目录中的软链接对应的所有文件一起上传、下载过去,如果原文件有链接文件,可能会使传过去的文件很大。具体问题可见我这篇博客:排错练习:No space left on device

二、rsync同步传输


rsync [参数]    file    username@ip:/dir
rsync    -r    ##同步目录
    -l    ##不忽略链接
    -p    ##不忽略文件权限
    -t    ##不忽文件时间戳
    -g    ##不忽文件所有组
    -o    ##不忽文件所有人
    -D    ##不忽略设备文件

tip:用rsync -r同步目录时候发现未和scp一样把目录中的链接对应的文件也传输过去

[root@server1 mnt]#  rsync -r /etc root@172.25.254.201:/mnt/            #将/etc目录同步到client1主机/mnt/下

tip:注意这里:/etc后面加‘/’和不加‘/’是有区别的:不加‘/’会将/etc目录本身同步过去,加上‘/’则只会将/etc这个目录下的所有文件同步过去而不包含etc目录名本身

[root@server1 mnt]# du -sh /etc/
 33M    /etc/[root@client1 mnt]# ls
 etc
 [root@client1 mnt]# du -sh /mnt/etc/
 33M    /mnt/etc/

测试:

1.创建一些特征文件

[root@server1 mnt]# touch /mnt/file{1..5}
 [root@server1 mnt]# ln -s /mnt/file5 /mnt/vaon
 [root@server1 mnt]# chmod 777 /mnt/*
 [root@server1 mnt]# chown server1.server1 /mnt/*
 [root@server1 mnt]# ls -l
 总用量 0
 -rwxrwxrwx. 1 server1 server1  0 10月 25 11:20 file1
 -rwxrwxrwx. 1 server1 server1  0 10月 25 11:20 file2
 -rwxrwxrwx. 1 server1 server1  0 10月 25 11:20 file3
 -rwxrwxrwx. 1 server1 server1  0 10月 25 11:20 file4
 -rwxrwxrwx. 1 server1 server1  0 10月 25 11:20 file5
 lrwxrwxrwx. 1 root    root    10 10月 25 11:20 vaon -> /mnt/file5
 2.执行同步[root@server1 mnt]# rsync /mnt/ -rlptgo root@172.25.254.201:/mnt
 root@172.25.254.201's password: 
 [root@client1 mnt]# ll
 总用量 0
 -rwxrwxrwx. 1 server1 server1  0 10月 25 11:20 file1
 -rwxrwxrwx. 1 server1 server1  0 10月 25 11:20 file2
 -rwxrwxrwx. 1 server1 server1  0 10月 25 11:20 file3
 -rwxrwxrwx. 1 server1 server1  0 10月 25 11:20 file4
 -rwxrwxrwx. 1 server1 server1  0 10月 25 11:20 file5
 lrwxrwxrwx. 1 root    root    10 10月 25 11:20 vaon -> /mnt/file5
 现在在client1端查看同步过来的文件属性、时间、所属用户、权限等和server1端的都是一样的,这是scp做不到的。
 3.同步设备文件一般设备文件是不能传输的,但可以加上-D参数同步
[root@server1 mnt]# rsync -r /dev/pts root@172.25.254.201:/mnt/
 root@172.25.254.201's password: 
 skipping non-regular file "pts/0"
 skipping non-regular file "pts/ptmx"
 [root@server1 mnt]# rsync -rD /dev/pts root@172.25.254.201:/mnt/
 root@172.25.254.201's password: 
 [root@client1 mnt]# ls
 file1  file2  file3  file4  file5  pts  vaon
 [root@client1 mnt]# ll pts/
 总用量 0
 crw-------. 1 root root 136, 0 10月 25 12:05 0
 c---------. 1 root root   5, 2 10月 25 12:05 ptmx

一般系统中的文件传输都会配合tar、zip等打包、压缩使用以加快传输效率。