Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux sftp command

Linux sftp command

Linux Command Reference Linux Command Reference


What is sftp?

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

The difference between SFTP and FTP

Feature SFTP FTP
Safety Encrypted transfer Plaintext transfer
port 22 (SSH port) 21 (control port) + 20 (data port)
Protocol Based on SSH Standalone protocol
Firewall friendliness Only one port needs to be opened Multiple ports need to be opened

Basic syntax of the sftp command



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

Commonly used options

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

Connect to an SFTP server

Basic connection method

Example

sftp username

@

hostname

Example:

Example

sftp user

@

example.com

Connect specifying a 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 the local working directory
lmkdir Create a directory locally
lrm Delete local files

2. Remote file operations

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

3. File transfer

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

Practical Examples

Example 1: Upload a file 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

creation sftp_commands.txt:

put file1.txt

put file2.txt

get report.pdf

exit

Execute the batch:

Example

sftp

-b

sftp_commands.txt user

@

example.com


Advanced techniques

1. Transfer multiple files using wildcards

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

2. Authentication failure

3. File transfer interrupted

4. Insufficient permissions


Security best practices

  1. Use key authenticationinstead of password authentication
  2. Restrict user permissions, use a chroot environment
  3. Update regularly SSH 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:

Now you can securely and efficiently transfer files between local and remote servers!

Linux Command Reference Linux Command Reference