Linux每日一篇 - 56 - scp
Linux安全文件传输神器!掌握scp命令,使用SSH加密传输文件,安全高效!
scp命令是什么?
scp是”Secure Copy”的缩写,基于SSH协议的安全文件传输命令,用于在本地主机和远程主机之间,或两个远程主机之间安全地复制文件和目录。
基本用法
# 从本地复制到远程
scp file.txt user@remote:/path/to/destination
# 从远程复制到本地
scp user@remote:/path/to/file.txt .
# 复制目录(递归)
scp -r directory/ user@remote:/path/to/destination
# 指定端口
scp -P 2222 file.txt user@remote:/path/to/destination
# 使用压缩传输
scp -C file.txt user@remote:/path/to/destination
# 保留文件属性
scp -p file.txt user@remote:/path/to/destination
实用技巧
# 复制多个文件到远程
scp file1.txt file2.txt user@remote:/path/to/destination
# 限制传输速度
scp -l 1000 file.txt user@remote:/path/to/destination
# 使用特定密钥文件
scp -i ~/.ssh/specific_key file.txt user@remote:/path/to/destination
# 详细模式
scp -v file.txt user@remote:/path/to/destination
# 两个远程主机间传输
scp user1@remote1:/path/to/file user2@remote2:/path/to/destination
# 保持文件权限和时间戳
scp -p file.txt user@remote:/path/to/destination
常用场景
# 备份配置文件到远程服务器
scp ~/.bashrc user@backup-server:/backup/
# 上传脚本到服务器
scp deploy.sh user@server:/home/user/
# 下载远程日志文件
scp user@server:/var/log/app.log ./
# 复制整个项目目录
scp -r ~/project/ user@server:/var/www/
# 传输大文件使用压缩
scp -C largefile.tar.gz user@server:/tmp/
# 使用别名(需要配置SSH config)
scp file.txt myserver:/tmp/
# 批量传输文件
cat filelist.txt | xargs -I {} scp {} user@server:/destination/