您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
# Linux中如何使用scp命令远程上传下载文件或文件夹
## 一、scp命令简介
`scp`(Secure Copy Protocol)是Linux系统中基于SSH协议的安全文件传输命令,能够在本地与远程服务器之间加密传输文件或目录。相比传统的FTP,scp提供了更高的安全性,是系统管理员常用的文件传输工具之一。
### 主要特点:
- 加密传输(使用SSH通道)
- 保留文件权限和时间戳
- 支持递归复制目录
- 支持断点续传(需结合rsync)
## 二、基本语法格式
```bash
scp [参数] 源文件 目标路径
参数 | 作用 |
---|---|
-r | 递归复制整个目录 |
-P | 指定SSH端口(默认22时可省略) |
-p | 保留文件原始属性 |
-C | 启用压缩传输 |
-q | 静默模式(不显示进度) |
-v | 显示详细传输信息 |
# 上传单个文件
scp /local/path/file.txt user@remote:/remote/path/
# 上传时重命名文件
scp file.txt user@remote:/remote/path/new_name.txt
# 上传整个目录(使用-r参数)
scp -r /local/folder user@remote:/remote/path/
# 下载单个文件
scp user@remote:/remote/path/file.txt /local/path/
# 下载整个目录
scp -r user@remote:/remote/folder /local/path/
当远程服务器使用非默认22端口时:
scp -P 2222 file.txt user@remote:/path/
scp user1@server1:/path/file user2@server2:/path/
scp -l 1000 file.txt user@remote:/path/ # 限制为1000Kbit/s
生成SSH密钥对:
ssh-keygen -t rsa
上传公钥到远程服务器:
ssh-copy-id user@remote
之后使用scp无需输入密码
虽然scp本身不支持断点续传,但可以通过rsync实现类似效果:
rsync -Pav -e "ssh -p 22" /local/path/ user@remote:/remote/path/
# 增加连接超时时间
scp -o ConnectTimeout=30 file.txt user@remote:/path/
# 确保目标目录有写入权限
scp -p file.txt user@remote:/tmp/ # 临时目录通常可写
# 设置字符集为UTF-8
scp -o ServerAliveInterval=60 -o ServerAliveCountMax=2 file.txt user@remote:/path/
敏感文件处理:
# 传输后删除源文件(谨慎使用)
scp file.txt user@remote:/path/ && rm -f file.txt
审计传输记录:
scp -v file.txt user@remote:/path/ > transfer.log 2>&1
防火墙配置:
工具 | 加密 | 目录传输 | 速度 | 断点续传 |
---|---|---|---|---|
scp | ✓ | ✓ | 中 | ✗ |
rsync | ✓ | ✓ | 快 | ✓ |
sftp | ✓ | ✗ | 慢 | ✗ |
ftp | ✗ | ✓ | 快 | ✗ |
大文件传输建议先压缩:
tar czf archive.tar.gz folder/ && scp archive.tar.gz user@remote:/path/
批量传输使用脚本:
#!/bin/bash
for file in /path/to/files/*; do
scp "$file" user@remote:/target/path/
done
传输完成后校验文件完整性:
md5sum file.txt # 本地计算
ssh user@remote "md5sum /path/file.txt" # 远程计算
通过掌握这些scp命令的使用方法和技巧,你可以高效安全地在Linux系统间传输文件,大大提高工作效率。 “`
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。