scp 命令 用于安全地在本地与远程主机之间复制文件。如果文件就在当前工作目录,直接给文件名即可;否则就需要写清楚完整路径,包括远程主机名。
把本地文件复制到远程目录:
scp localfile.txt /home/friend/share/
从远程主机拉取文件到当前目录:
scp user@remotehost.com:/var/www/html/remote_file.txt ./
在远程主机之间直接对拷文件:
scp user1@remotehost1.com:/var/www/html/file.txt user2@remotehost2.com:/home/user2/
如果需要复制整个目录(含子目录),记得加上 -r 递归选项:
scp -r user@remotehost.com:/var/www/html/ ./html/
基本用法
日常使用中,可以记忆下面几个最常见的操作模式。
# 将远程文件复制到本地目录
scp user@remotehost.com:/remote/path/to/foobar.md /local/dest
# 将本地文件复制到远程目录
scp /local/path/to/foobar.md user@remotehost.com:/remote/path
# 使用密钥文件认证(和 ssh 用法一致)
scp -i /path/to/key/file /local/path/to/foobar.md user@remotehost.com:/remote/path
以上就是 SCP 命令最常用的几种场景,掌握它们基本就能覆盖日常文件拷贝需求。更多 Linux 实战技巧,欢迎常来云栈社区逛逛。
|