
?博__主?:米码收割机
?技__能?:C++/Python语言
?公众号?:测试开发自动化【获取源码+商业合作】
?荣__誉?:阿里云博客专家博主、51CTO技术博主
?专__注?:专注主流机器人、人工智能等相关领域的开发、测试技术。
Linux之间如何互传文件(详细讲解)
目录
Linux之间如何互传文件(详细讲解)1. `scp` (Secure Copy)2. `rsync`3. `sftp` (Secure File Transfer Protocol)4. `sshfs` (SSH Filesystem)5. `nc` (Netcat)注意事项
在两台Linux机器之间传输文件,通常有以下几种方法:
1. scp (Secure Copy)
scp 是基于SSH的,因此它在传输文件时是加密的。这是一种简单且常用的方法。
从本地复制到远程:
scp /path/to/local/file username@remote_host:/path/to/remote/directory/ 从远程复制到本地:
scp username@remote_host:/path/to/remote/file /path/to/local/directory/ 2. rsync
rsync 是一种更强大的文件复制工具,它可以在本地和远程之间复制文件,还可以用于备份。它也支持SSH,因此传输是安全的。
从本地复制到远程:
rsync -av /path/to/local/directory/ username@remote_host:/path/to/remote/directory/ 从远程复制到本地:
rsync -av username@remote_host:/path/to/remote/directory/ /path/to/local/directory/ 3. sftp (Secure File Transfer Protocol)
sftp 也是基于SSH的,它为文件传输提供了一个交互式界面。
开始一个 sftp 会话:
sftp username@remote_host 然后你可以使用像 cd, ls, get, put 这样的命令进行文件操作。
4. sshfs (SSH Filesystem)
sshfs 允许你挂载远程目录到本地文件系统中,就像它们是本地目录一样。
首先,你可能需要安装 sshfs:
sudo apt install sshfs # 对于Debian/Ubuntu 挂载远程目录:
sshfs username@remote_host:/path/to/remote/directory /path/to/local/mountpoint 取消挂载:
fusermount -u /path/to/local/mountpoint 5. nc (Netcat)
虽然 netcat 不是专门设计用来传输文件的,但在没有上述工具的情况下,它仍然可以作为一个快速和简单的文件传输方法。
在接收端:
nc -l 12345 > received_file 在发送端:
nc target_host 12345 < source_file 请注意,上述 netcat 方法不是加密的,因此不建议在不安全的网络上使用它。