使用scp在Linux之间进行文件传输
封面图
•
2025-11-04
•
Linux
199
0
0
0
在 Linux 系统之间使用 `scp`(secure copy)进行文件传输是一种安全、方便的方式,它基于 SSH 协议,可以实现本地与远程主机之间、或者两个远程主机之间的文件复制。 --- ## ✅ 基本语法 ```bash scp [选项] 源文件 目标路径 ``` --- ## 📦 1. 从本地复制文件到远程主机 ```bash scp /path/to/local/file username@remote_host:/path/to/destination ``` **示例:** ```bash scp myfile.txt user@192.168.1.100:/home/user/ ``` 这会将本地的 `myfile.txt` 文件复制到远程主机 `192.168.1.100` 的 `/home/user/` 目录下。 --- ## 📦 2. 从远程主机复制文件到本地 ```bash scp username@remote_host:/path/to/remote/file /path/to/local/destination ``` **示例:** ```bash scp user@192.168.1.100:/home/user/remotefile.txt /home/localuser/ ``` 这会将远程主机上的 `remotefile.txt` 复制到本地的 `/home/localuser/` 目录中。 --- ## 📦 3. 在两个远程主机之间复制文件(通过本地中转) ```bash scp username1@host1:/path/to/file username2@host2:/path/to/destination ``` **示例:** ```bash scp user1@192.168.1.100:/home/user1/file.txt user2@192.168.1.101:/home/user2/ ``` 这会把 `192.168.1.100` 上的文件传到 `192.168.1.101` 上。 > ⚠️ 注意:这种情况下,本地机器需要有访问两个远程主机的权限。 --- ## 📦 4. 递归复制整个目录 ```bash scp -r /path/to/local/directory username@remote_host:/path/to/destination ``` **示例:** ```bash scp -r myfolder user@192.168.1.100:/home/user/ ``` --- ## 🔐 5. 使用指定的 SSH 密钥(可选) 如果你使用密钥认证,可以通过 `-i` 参数指定私钥文件: ```bash scp -i /path/to/private_key.pem /path/to/file username@remote_host:/path/to/destination ``` --- ## 🧩 6. 常用选项 | 选项 | 说明 | |------|------| | `-r` | 递归复制目录 | | `-P` | 指定 SSH 端口(默认是 22) | | `-i` | 指定私钥文件 | | `-C` | 启用压缩传输(加快速度) | | `-p` | 保留文件属性(如时间戳) | --- ## 💡 示例:使用非默认端口 ```bash scp -P 2222 /localfile user@remote:/remote/path ``` --- ## 🧪 验证传输是否成功 传输完成后,可以在目标主机上使用 `ls` 或 `cat` 命令查看文件是否已正确传输。 --- ## ❗ 注意事项 - 确保远程主机的 SSH 服务正在运行。 - 确保你有权限访问远程主机和目标目录。 - 如果第一次连接远程主机,系统会提示你确认对方的指纹,输入 `yes` 即可继续。 --- 如果你有具体的场景(比如跨服务器传输、使用密钥等),我可以帮你写出更详细的命令。需要的话请告诉我!
上一篇:
unzip命令,linux下解压zip
下一篇:
Linux操作系统下文件路径命令
标题录入,一次不能超过6条
留言
评论