sftp
基于 SSH 的安全文件传输协议,提供交互式文件管理界面
语法
sftp [options] [user@]host[:path]
参数
| 参数 | 说明 | 示例 | 级别 |
|---|---|---|---|
-P |
指定远程端口 | sftp -P 2222 user@host |
常用 |
-i |
指定私钥文件 | sftp -i ~/.ssh/key user@host |
常用 |
-b |
批处理模式,从文件读取命令 | sftp -b commands.txt user@host |
进阶 |
-r |
递归操作目录 | sftp> put -r ./local_dir |
常用 |
-l |
限制带宽(Kbit/s) | sftp -l 1024 user@host |
进阶 |
示例
交互式连接
sftp user@server.example.com
进入交互模式后可用 ls/cd/get/put 等命令
上传文件
sftp> put ./local-file.tar.gz /remote/path/
在 sftp 交互模式中上传文件
下载整个目录
sftp> get -r /remote/project ./local/
递归下载远程目录到本地
批量传输
echo 'put *.log /var/backup/' > batch.txt sftp -b batch.txt user@host
非交互式批量执行命令,适合脚本自动化
常见错误
Couldn't stat remote file: No such file or directory
远程路径不存在,用 ls 确认路径后重试
Uploading file.txt to /path/file.txt: Permission denied
远程目录没有写权限,联系管理员或换目录
技巧
- sftp 交互模式中 lls/lcd 操作本地目录,ls/cd 操作远程目录
- 用 ! 前缀可以在 sftp 中执行本地 shell 命令,如 !ls
- 批处理模式(-b)适合 cron 定时任务自动传输文件