温馨提示×

怎样在linux sftp传文件

小樊
55
2026-10-10 21:57:31
栏目: 智能运维

在 Linux 下使用 SFTP(SSH File Transfer Protocol) 传文件,常见方式有以下几种:


一、使用命令行 SFTP(最常用)

1. 连接 SFTP 服务器

sftp username@hostname
# 或指定端口
sftp -P 22 username@hostname

连接成功后,提示符会变成:

sftp>

2. 上传文件(本地 → 服务器)

put localfile.txt

上传到指定目录:

put localfile.txt /remote/path/

上传整个目录:

put -r localdir

3. 下载文件(服务器 → 本地)

get remotefile.txt

下载到指定目录:

get remotefile.txt /local/path/

下载整个目录:

get -r remotedir

4. 常用命令

ls          # 查看远程目录
lls         # 查看本地目录
cd          # 切换远程目录
lcd         # 切换本地目录
pwd         # 远程当前路径
lpwd        # 本地当前路径
exit        # 退出

二、一步到位传文件(非交互模式)

上传

sftp username@hostname:/remote/path/ <<EOF
put localfile.txt
EOF

下载

sftp username@hostname:/remote/path/ <<EOF
get remotefile.txt
EOF

三、使用 scp(更简单,底层也是 SSH)

如果你只是传文件,不一定要进 sftp

# 上传
scp file.txt user@host:/path/

# 下载
scp user@host:/path/file.txt ./

# 传目录
scp -r dir user@host:/path/

四、使用密钥登录(推荐)

sftp -i ~/.ssh/id_rsa user@host

五、图形化工具(可选)

  • FileZilla
  • WinSCP(Windows)
  • Nautilus / Dolphin(Linux 文件管理器)
    • 地址栏输入:
      sftp://user@host
      

如果你有具体场景(如脚本自动传文件、只能 SFTP 不能 SSH、限速等),可以告诉我,我给你更合适的方案。

0 踩