Ubuntu 传输文件方法

Ubuntu 传输文件方法

1 scp

secure copy,使用 ssh登陆服务器,可进行远程拷贝文件操作。因为是加密传输,会影响一些速度

cp 也能拷贝文件,但是不能跨服务器。

参数说明:

参数选项说明
-1强制scp命令使用协议ssh1
-2强制scp命令使用协议ssh2
-4强制scp命令只使用IPv4寻址
-6强制scp命令只使用IPv6寻址
-B使用批处理模式(传输过程中不询问传输口令或短语)
-C允许压缩。(将-C标志传递给ssh,从而打开压缩功能)
-p保留原文件的修改时间,访问时间和访问权限。
-q不显示传输进度条。
-r递归复制整个目录。
-v详细方式显示输出。scp和ssh(1)会显示出整个过程的调试信息。这些信息用于调试连接,验证和配置问题。
-c cipher以cipher将数据传输进行加密,这个选项将直接传递给ssh。
-F ssh_config指定一个替代的ssh配置文件,此参数直接传递给ssh。
-i identity_file从指定文件中读取传输时使用的密钥文件,此参数直接传递给ssh。
-l limit限定用户所能使用的带宽,以Kbit/s为单位。
-o ssh_option如果习惯于使用ssh_config(5)中的参数传递方式,
-P port注意是大写的P, port是指定数据传输用到的端口号
-S program指定加密传输时所使用的程序。此程序必须能够理解ssh(1)的选项。

usage

# 拷贝文件
## 本地拷贝到远程服务器
scp local_file remote_username@remote_ip:remote_folder

scp local_file remote_username@remote_ip:remote_file

scp local_file remote_ip:remote_folder

scp local_file remote_ip:remote_file

## 服务器拷贝到本地
scp remote_username@remote_ip:remote_folder/remote_file local_file 

----------------------------------

# 拷贝文件夹
## 本地拷贝到远程服务器
scp -r local_folder remote_username@remote_ip:remote_folder
或者
scp -r local_folder remote_ip:remote_folder

## 服务器拷贝到本地
scp -r remote_username@remote_ip:remote_folder local_folder

跨越跳板机传输

文件传输
上传
scp -P 22 -o "ProxyJump yqnj@10.78.3.199" -r /home/songweijie/faw/project_code/HAD_J5_3_APP_Framework-5/output.tar.gz root@172.20.0.55:/home/lidar/swj/output.tar.gz
简化
scp -o 'ProxyJump yqnj@10.78.3.199' -r /home/songweijie/faw/project_code/HAD_J5_3_APP_Framework-5/output.tar.gz root@172.20.0.55:/home/lidar/swj/output.tar.gz
scp -o 'ProxyJump yqnj@10.78.3.199' -r /home/qiancj/codes/faw-lidar/deploy/app_meta_lidar_perception.tar.gz root@172.20.0.53:/opt/data/driving_map_data/lidar/app_meta_lidar_perception.tar.gz

下载
scp -P 22 -o 'ProxyJump yqnj@10.78.3.199 -p 22' -r root@172.20.0.55:/home/lidar/FRidar.pcap /home/songweijie/faw/Lidar/data/FRidar.pcap
简化
scp -o 'ProxyJump yqnj@10.78.3.199' -r root@172.20.0.55:/home/lidar/FRidar.pcap /home/songweijie/faw/Lidar/data/FRidar.pcap

2 rsync(remote sync)

可以在本地计算机与远程计算机之间,或者两个本地目录之间同步文件(但不支持两台远程计算机之间的同步)。

也可以当作文件复制工具,替代cpmv命令。

特性

  1. 高效地复制同步数据到对端,或者对端到本地
  2. 支持复制链接、设备、属主、属组、权限
  3. 比scp(Secure Copy)更快。rsync使用远程更新协议( remote-update protocol ),会检查发送方和接收方已有的文件,这允许仅仅传输两组文件之间的差异(默认规则是文件大小或修改时间有变动)。对于首次传输,它将文件或目录的全部内容从源复制到目标,但是从下次起,它仅将变化部分复制到目标。
  4. Rsync消耗较少的带宽,因为它使用压缩和解压缩方法,同时发送和接收数据两端。HTTP压缩技术

install

$ sudo apt-get install rsync   [On Debian/Ubuntu & Mint] 
$ pacman -S rsync              [On Arch Linux]
$ emerge sys-apps/rsync        [On Gentoo]
$ sudo yum install rsync       [On Fedora/CentOS/RHEL and Rocky Linux/AlmaLinux]
$ sudo zypper install rsync    [On openSUSE]

usage

参数说明
-v详细模式输出
-r递归拷贝数据,但是传输数据时不保留时间戳和权限
-a归档模式, 归档模式总是递归拷贝,而且保留符号链接、权限、属主、属组时间戳
-z压缩传输
-hhuman-readable
–progress显示传输过程
–exclude=PATTERN指定排除传输的文件模式
–include=PATTERN指定需要传输的文件模式
–delete同步时,删除那些DST中有,而SRC没有的文件
–max-size限定传输文件大小的上限
–dry-run显示那些文件将被传输,并不会实际传输
–bwlimit限制传输带宽
-W拷贝文件,不进行增量检测
–numeric-ids不按用户/组名称映射 uid/gid 值
本地拷贝同步
rsync -zvh local_file local/directory
rsync -avzh local/directory1 local/directory2
将文件从远程机器复制到本地机器

假设要将文件从远程系统上用户的主目录复制到本地登录系统的当前目录。

rsync -avzh remote_user@remote_ip:remote_directory local/directory

rsync username@ip_address:/home/username/filename .
rsync -avzh root@192.168.0.100:/home/tarunika/rpmpkgs /tmp/myrpms
将文件从本地机器复制到远程机器

这是一个通用语法,它将文件复制到远程系统上用户名的主目录。

rsync -avzh local/directory remote_user@remote_ip:remote_directory 
rsync -r source_dir username@ip_address:/home/username/target_dir

rsync filename username@ip_address:/home/username
rsync -avz rpmpkgs/ root@192.168.0.101:/home/
通过ssh使用rsync

SSH(Secure Shell)使用 rsync,我们可以使用 SSH(安全外壳)进行数据传输,在传输数据时使用 SSH 协议,可以确保数据在加密的安全连接中传输,以便没有人可以在通过互联网上的线路传输数据时读取数据。

rsync -avzhe ssh local_file remote_user@remote_ip:remote/directory

rsync -avzhe ssh backup.tar root@192.168.0.100:/backups/

rsync -avzhe ssh remote_user@remote_ip:remote/directory local_file

# 传输数据时显示传输过程  使用--progress参数
rsync -avzhe ssh --progress local_file remote_user@remote_ip:remote/directory
  • 传输R开头的文件、目录,排除其他情况的文件、目录
  rsync -avze ssh --include 'R*' --exclude '*' remote_user@remote_ip:remote/directory local/directory
  
  rsync -avze ssh --include 'R*' --exclude '*' root@192.168.0.101:/var/lib/rpm/ /root/rpm

3 SSHFS

SSHFS(Secure SHell FileSystem)是一个客户端,可以让我们通过 SSH 文件传输协议(SFTP)挂载远程的文件系统并且在本地机器上和远程的目录和文件进行交互。

SFTP 是一种通过 SSH 协议提供文件访问、文件传输和文件管理功能的安全文件传输协议。因为 SSH 在网络中从一台电脑到另一台电脑传输文件的时候使用数据加密通道,并且 SSHFS 内置在 FUSE(用户空间的文件系统)内核模块,允许任何非特权用户在不修改内核代码的情况下创建他们自己的文件系统。

usage

用法: sshfs [user@]host:[dir] 挂载点 [选项]

常规选项:
     -o opt,[opt...] 挂载选项
     -h --help 打印帮助
     -V --version 打印版本

SSHFS 选项:
     -p PORT 相当于“-o port=PORT”
     -C 相当于“-o 压缩=yes”
     -F ssh_configfile 指定备用ssh配置文件
     -1 相当于 '-o ssh_protocol=1'
     -o reconnect 重新连接到服务器
     -o delay_connect 延迟与服务器的连接
     -o sshfs_sync 同步写入
     -o no_readahead 同步读取(无推测性预读)
     -osync_readdir 同步readdir
     -o sshfs_debug 打印一些调试信息
     -o cache=BOOL 启用缓存 {yes,no} (默认值:yes)
     -o cache_max_size=N 设置缓存的最大大小(默认值:10000)
     -o cache_timeout=N 设置缓存超时(以秒为单位)(默认值:20)
     -o cache_X_timeout=N 设置 {stat,dir,link} 缓存的超时
     -o 缓存清理间隔=N
                            设置自动清洁的时间间隔
                            缓存(默认:60)
     -ocache_min_clean_interval=N
                            设置强制清洁的时间间隔
                            缓存已满(默认值:5)
     -o workaround=LIST 冒号分隔的解决方法列表
              无 未启用解决方法
              [no]rename 修复重命名现有文件(默认值:关闭)
              [no]truncate 修复旧服务器的截断(默认:关闭)
              [no]buflimit 修复服务器中的缓冲区填充错误(默认:打开)
              [no]fstat 修复旧服务器的 fstat(默认值:关闭)
     -o idmap=TYPE 用户/组 ID 映射(默认值:无)
              none 不翻译 ID 空间
              用户仅翻译连接用户的 UID/GID
              文件转换 uidfile/gidfile 中包含的 UID/GID
     -o uidfile=FILE 包含用户名的文件:remote_uid 映射
     -o gidfile=FILE 包含组名的文件:remote_gid 映射
     -o nomap=TYPE 和 idmap=file,如何处理丢失的映射
              忽略不做任何重新映射
              错误返回错误(默认)
     -o ssh_command=CMD 执行 CMD 而不是 'ssh'
     -o ssh_protocol=N 要使用的 ssh 协议(默认值:2)
     -o sftp_server=sftp 服务器或子系统的 SERV 路径(默认值:sftp)
     -o directport=PORT 绕过ssh直接连接到PORT
     -o 从机通过 stdin 和 stdout 绕过网络进行通信
     -o disable_hardlink link(2) 将返回并将 errno 设置为 ENOSYS
     -o transform_symlinks 将绝对符号链接转换为相对符号链接
     -o follow_symlinks 跟随服务器上的符号链接
     -o no_check_root 不检查服务器上是否存在“dir”
     -o password_stdin 从 stdin 读取密码(仅适用于 pam_mount!)
     -o SSHOPT=VAL ssh 选项(参见 man ssh_config)

保险丝选项:
     -d -o debug 启用调试输出(隐含 -f)
     -f 前台操作
     -s 禁用多线程操作

     -o allow_other 允许其他用户访问
     -o allow_root 允许访问root
     -o auto_unmount 进程终止时自动卸载
     -o 非空允许安装在非空文件/目录上
     -o default_permissions 启用内核的权限检查
     -o fsname=NAME 设置文件系统名称
     -o subtype=NAME 设置文件系统类型
     -o large_read 发出大量读取请求(仅限 2.4-o max_read=N 设置读取请求的最大大小

     -o hard_remove 立即删除(不隐藏文件)
     -o use_ino 让文件系统设置 inode 编号
     -o readdir_ino 尝试在readdir中填写d_ino
     -o direct_io 使用直接 I/O
     -o kernel_cache 内核中的缓存文件
     -o [no]auto_cache 根据修改时间启用缓存(关闭)
     -o umask=M 设置文件权限(八进制)
     -o uid=N 设置文件所有者
     -o gid=N 设置文件组
     -o Entry_timeout=T 名称缓存超时(1.0 秒)
     -o negative_timeout=T 已删除名称的缓存超时(0.0s)
     -o attr_timeout=T 属性缓存超时(1.0s)
     -o ac_attr_timeout=T 属性自动缓存超时 (attr_timeout)
     -o noforget 永远不会忘记缓存的 inode
     -o Remember=T 记住缓存的 inode T 秒(0s)
     -o nopath 如果不需要,不提供路径
     -o intr 允许请求被中断
     -o intr_signal=中断时发送的 NUM 信号 (10)
     -o module=M1[:M2...] 要推送的模块名称

通过 SSHFS 从远程系统访问文件

在 Debian 和 Ubuntu 上,您可以使用以下命令:

sudo apt install sshfs

在系统上安装 sshfs 后,您可以使用它来挂载远程目录,最好为挂载点创建一个专用目录。

mkdir mount_dir

在远程机器上挂载所需的目录:

sshfs remote_username@remote_IP_address:remote_path_to_dir local_mount_dir

# 允许其他用户使用
sudo sshfs -o allow_other remote_username@remote_IP_address:remote_path_to_dir local_mount_dir

# 如果端口不是默认的22,则需要指定端口
-p 33000
sudo sshfs -o allow_other -p 33000 remote_username@remote_IP_address:path_to_dir local_mount_dir

sudo sshfs -o allow_other test@10.78.33.100:/home/test/qian /mnt/A100_qian
test@10.78.33.100's password: 
(base) qiancj@qiancj-HP-ZBook-G8:/mnt$ ll
total 20
drwxr-xr-x  5 root root 4096 625 10:41 ./
drwxr-xr-x 22 root root 4096 426 14:10 ../
drwxrwxr-x  1 1001 1001 4096 67 10:18 A100_qian/ # 拥有者有问题
(base) qiancj@qiancj-HP-ZBook-G8:/mnt$ cd A100_qian/
(base) qiancj@qiancj-HP-ZBook-G8:/mnt/A100_qian$ ls
20230607_010452.log  20230607_012200.log # 远程文件夹内文件

如果 Linux 服务器配置为基于 SSH 密钥授权,那么需要指定公共密钥的路径:

sshfs -o IdentityFile=~/.ssh/id_rsa remote_username@remote_IP_address:remote_path_to_dir local_mount_dir
sudo sshfs -o allow_other,IdentityFile=~/.ssh/id_rsa remote_username@remote_IP_address:path_to_dir local_mount_dir

挂载后,使用df -hT检查挂载点

df -hT

永久挂载远程文件系统

修改 /etc/fstab 的文件:

sudo vi /etc/fstab 

在文件最下面添加如下命令:

sshfs#remote_username@remote_IP_address:remote_path_to_dir local_mount_dir fuse.sshfs defaults 0 0

可以将文件复制到该目录或从该目录复制,就好像它在本地计算机上一样

cp local_file mount_dir

请记住,您已安装此文件,完成工作后,您还应该卸载它:

umount mount_dir

4 使用基于 GUI 的 SFTP 客户端在远程系统之间传输文件

作为最后的手段,您可以使用 FTP 客户端在远程和本地系统之间传输文件。

FileZilla是最流行的跨平台 FTP 客户端之一。可以轻松地安装在本地系统上。

安装后,转到文件->站点管理器并添加远程系统详细信息,例如 IP 地址、SSH 端口号、用户名和密码。

连接后,可以看到一个拆分窗口视图,左侧显示本地文件系统,右侧显示远程文件系统。

5 msrsync(multi-stream rsync)

从名字也能看出来,基于rsync做了个多路传输

在扫描源时,它将把传输分成多个存储桶,并有望通过并行运行可配置数量的 rsync 进程来帮助最大限度地利用可用带宽。 主要限制是它不处理远程源或目标目录,它们必须是本地可访问的(本地磁盘、nfs/cifs/其他挂载点)。

安装

msrsync 是一个单独的 python 文件,只需下载它即可。 或者,如果愿意,可以克隆存储库并使用提供的 Makefile:

$ wget https://raw.githubusercontent.com/jbd/msrsync/master/msrsync && chmod +x msrsync
# or
$ git clone https://github.com/jbd/msrsync && cd msrsync && sudo make install

用法:

$ msrsync --help
usage: msrsync [options] [--rsync "rsync-options-string"] SRCDIR [SRCDIR2...] DESTDIR
   or: msrsync --selftest

msrsync选项:
     -p, --processes ...要使用的 rsync 进程数 [1]
     -f, --files ... 将存储桶限制为 <files> 文件数 [1000]
     -s, --size ... 将分区限制为 BYTES 大小(1024 个后缀:K、M、G、T、P、E、Z、Y)[1G]
     -b, --buckets ... 存放存储桶文件的位置(默认值:自动临时目录)
     -k, --keep 最后不删除存储桶目录
     -j, --show 显示存储桶目录
     -P, --progress 显示进度
     --stats 显示额外的统计数据
     -d, --dry-run 不运行 rsync 进程
     -v, --version 打印版本

rsync选项:
     -r, --rsync ... 必须是最后一个选项。 rsync 选项作为带引号的字符串 ["-aS --numeric-ids"]。 “--from0 --files-from=... --quiet --verbose --stats --log-file=...”选项
                             无论如何都会被添加。 请注意,如果您想使用它们,这将影响所有 rsync *from/filter 文件。 请参阅 rsync(1) 联机帮助页
                             细节。

自检选项:
     -t, --selftest 运行集成单元和功能测试
     -e, --bench 运行基准测试
     -g, --benchshm 在 /dev/shm 或 $SHM 环境变量中的目录中运行基准测试

Example

$ msrsync -p4 --rsync "-a --numeric-ids --inplace" source destination

$ msrsync -p 8 /usr/share/doc/ /tmp/doc/
$ msrsync -P -p 8 /usr/share/doc/ /tmp/doc/
[33491/33491 entries] [602.1 M/602.1 M transferred] [3378 entries/s] [60.7 M/s bw] [monq 1] [jq 1]

$ msrsync -P -p 8 --stats /usr/share/doc/ /tmp/doc/
[33491/33491 entries] [602.1 M/602.1 M transferred] [3533 entries/s] [63.5 M/s bw] [monq 1] [jq 1]
Status: SUCCESS
Working directory: /home/jbdenis/Code/msrsync
Command line: ./msrsync -P -p 8 --stats /usr/share/doc/ /tmp/doc/
Total size: 602.1 M
Total entries: 33491
Buckets number: 34
Mean entries per bucket: 985
Mean size per bucket: 17.7 M
Entries per second: 3533
Speed: 63.5 M/s
Rsync workers: 8
Total rsync's processes (34) cumulative runtime: 73.0s
Crawl time: 0.4s (4.3% of total runtime)
Total time: 9.5s

测试

测试数据

test_src 文件夹下包含1个1.5G大文件,和4260个小文件

(base) qiancj@qiancj-HP-ZBook-G8:~$ du -s --apparent-size --bytes test_src
3007084438	test_src
(base) qiancj@qiancj-HP-ZBook-G8:~$ du -s --apparent-size --human test_src
2.9G	test_src
(base) qiancj@qiancj-HP-ZBook-G8:~$ find test_src -type f | wc -l
4261
(base) qiancj@qiancj-HP-ZBook-G8:~$ find test_src -type d | wc -l
1

scp 和 rsync 远程传输与本地传输

$ time scp -r test_src test@10.78.4.100:/home/test/qian/
real	0m38.079s
user	0m6.412s
sys	0m2.203s


$ time rsync -avzh test_src test@10.78.4.100:/home/test/qian/

sent 1.28G bytes  received 80.98K bytes  11.01M bytes/sec
total size is 3.01G  speedup is 2.34

real	1m56.117s
user	1m53.235s
sys	0m1.581s

$ time rsync -a --numeric-ids  test_src test@10.78.4.100:/home/test/qian/
test@10.78.4.100's password: 

real	0m35.946s
user	0m9.702s
sys	0m2.027s


# 本地传输
$ time scp -r test_src test2/

real	0m1.201s
user	0m0.003s
sys	0m1.192s

$ time rsync -avzh test_src test2/
sent 1.28G bytes  received 80.98K bytes  9.83M bytes/sec
total size is 3.01G  speedup is 2.34

real	2m10.117s
user	2m23.374s
sys	0m2.479s

$ time rsync -a --numeric-ids test_src test2/

real	0m5.073s
user	0m7.438s
sys	0m1.919s

rsync 和 msrsync 本地传输

$ time rsync -a --numeric-ids test_src test2/

real	0m5.073s
user	0m7.438s
sys	0m1.919s

(base) qiancj@qiancj-HP-ZBook-G8:~$ time msrsync -p 1 test_src test2/

real	0m7.306s
user	0m7.613s
sys	0m4.116s
(base) qiancj@qiancj-HP-ZBook-G8:~$ rm -rf test2/test_src/
(base) qiancj@qiancj-HP-ZBook-G8:~$ time msrsync -p 2 test_src test2/

real	0m5.398s
user	0m7.520s
sys	0m4.544s
(base) qiancj@qiancj-HP-ZBook-G8:~$ rm -rf test2/test_src/
(base) qiancj@qiancj-HP-ZBook-G8:~$ time msrsync -p 3 test_src test2/

real	0m5.315s
user	0m7.798s
sys	0m4.424s
(base) qiancj@qiancj-HP-ZBook-G8:~$ rm -rf test2/test_src/
(base) qiancj@qiancj-HP-ZBook-G8:~$ time msrsync -p 4 test_src test2/

real	0m4.480s
user	0m7.716s
sys	0m4.943s
(base) qiancj@qiancj-HP-ZBook-G8:~$ rm -rf test2/test_src/
(base) qiancj@qiancj-HP-ZBook-G8:~$ time msrsync -p 5 test_src test2/

real	0m4.550s
user	0m7.941s
sys	0m4.954s

结果对比

  1. 远程传输
CommandTimeEntries per secondBandwidth (MBytes/s)Speedup
scp0m38.079s11275.31x1
rsync -avzh1m56.117s3724.69x0.32
rsync -a --numeric-ids0m35.946s11979.78x1.05
  1. 本地传输
CommandTimeEntries per secondBandwidth (MBytes/s)Speedup
scp0m1.201s3547.882388.01x1
rsync -avzh2m10.117s32.7522.04x0.01
rsync -a --numeric-ids0m5.073s839.94565.35x0.24
msrsync -p 10m7.306s583.22392.55x0.16
msrsync -p 20m5.398s789.37531.31x0.22
msrsync -p 40m5.315s801.69539.60x0.23
msrsync -p 80m4.480s951.12640.18x0.27
msrsync -p 160m4.550s936.48630.33x0.26

这里测试数据不平衡,msrsync 多个进程开始,最后瓶颈在于传输1.5G的单文件了,而测试文件小文件也不足够多。

Reference


>>>>> 欢迎关注公众号【三戒纪元】 <<<<<

要在Windows主机和Ubuntu之间互传文件,可以使用多种方法。其中一种方法是使用FileZilla软件进行文件传输。首先,打开FileZilla软件,并选择"文件" -> "站点管理器" -> "新增站点"。然后,配置连接参数,如主机名、用户名、密码等。点击连接后,你就可以看到Ubuntu中的文件了。接下来,你可以直接将Windows下的目录文件拖到右侧Ubuntu中指定的目录中,实现文件传输。\[1\] 另一种方法是使用共享文件夹功能。在虚拟机中,需要先开启共享文件夹的功能。首先,确保Ubuntu虚拟机已经开机。然后,在虚拟机中选择"虚拟机" -> "设置" -> "选项" -> "共享文件夹"。在共享文件夹设置中,选择"总是启动",然后添加一个与Windows主机共享的路径。这样,你就可以在Ubuntu中访问Windows主机的共享文件夹,或者在Windows主机中访问Ubuntu虚拟机的共享文件夹,实现文件的互传。\[3\] 还有其他方法可以实现Windows主机和Ubuntu之间的文件互传,比如使用Mobaxterm通过SSH远程登录Ubuntu,或者使用Samba方式进行文件共享。具体的步骤可以参考相关的教程和文档。\[2\] #### 引用[.reference_title] - *1* *2* *3* [windows主机和ubuntu互传文件的4种方法](https://blog.csdn.net/qq_28077617/article/details/127001307)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值