Linux sftp Command

Linux 命令大全Complete List of Linux Commands


What is sftp?

SFTP (SSH File Transfer Protocol) is a secure file transfer protocol that transmits files through an SSH (Secure Shell) encrypted channel. Unlike traditional FTP, SFTP provides higher security, and all transmitted data is encrypted.

Differences between SFTP and FTP

Feature SFTP FTP
Security Encrypted transmission Plaintext transmission
Port 22 (SSH port) 21 (control port) + 20 (data port)
Protocol Based on SSH Independent protocol
Firewall friendliness Only need to open one port Need to open multiple ports

Basic syntax of the sftp command

sftp [选项] [用户名@]主机名[:路径]

Common option parameters

Option Description
-P 端口号 Specify the connection port (default 22)
-b 批处理文件 Execute commands from a batch file
-r Recursively copy an entire directory
-v Display detailed debugging information
-i 私钥文件 Authenticate using the specified private key file

Connect to SFTP server

Basic connection method

Example

sftp username@hostname

Example:

Example

sftp user@example.com

Connect with a specified port

If the server uses a non-standard port (not 22):

Example

sftp -P 2222 user@example.com

Use key authentication

Example

sftp -i ~/.ssh/id_rsa user@example.com

Common SFTP operation commands

1. Local file operations

Command Description
lls List local directory contents
lcd Change local working directory
lmkdir Create directory locally
lrm Delete local files

2. Remote file operations

Command Description
ls List remote directory contents
cd Change remote working directory
mkdir Create directory remotely
rm Delete remote files
rename Rename remote files

3. File transfer

Command Description
put 本地文件 [远程路径] Upload files to remote server
get 远程文件 [本地路径] Download files from remote server
mput 本地文件* Upload multiple files
mget 远程文件* Download multiple files

Practical examples

Example 1: Upload files to the server

Example

sftp user@example.com
sftp> cd /var/www/html
sftp> put index.html
sftp> exit

Example 2: Download an entire directory (recursive)

Example

sftp -r user@example.com:/backups /local/backups

Example 3: Use a batch file

Createsftp_commands.txt:

put file1.txt
put file2.txt
get report.pdf
exit

Execute batch:

Example

sftp -b sftp_commands.txt user@example.com

Advanced tips

1. Use wildcards to transfer multiple files

Example

sftp> mput *.jpg      # Upload all jpg files
sftp> mget log_*.txt  # Download all txt files starting with log_

2. Change file permissions

Example

sftp> chmod 644 index.html

3. View file information

Example

sftp> ls -l

4. Use symbolic links

Example

sftp> symlink /path/to/original /path/to/link

Common problem solutions

1. Connection refused

  • Check whether the server is running the SSH service
  • Confirm whether the port is correct
  • Check firewall settings

2. Authentication failure

  • Confirm that the username and password are correct
  • Check the key file permissions (should be 600)
  • Ensure that your public key is on the server

3. File transfer interrupted

  • Check the network connection
  • Try usingregetandreputcommand to resume the transfer

4. Insufficient permissions

  • Check the write permission of the remote directory
  • You may need to usesudo(But sftp usually does not support direct sudo)

Security best practices

  1. Use key authenticationinstead of password authentication
  2. Limit user permissions, use a chroot environment
  3. Regularly updateSSH server software
  4. Disable root login, use a regular user + sudo
  5. Monitor log files(/var/log/auth.log)

Summary

SFTP is an important tool for securely transferring files in Linux systems. Through this guide, you should have mastered:

  • Basic concepts and connection methods of SFTP
  • Common file operation and transfer commands
  • Practical batch processing and advanced tips
  • Solutions to common problems
  • Best practices for secure use

Now you can securely and efficiently transfer files between your local machine and a remote server!

Linux 命令大全Complete List of Linux Commands

Other extensions