Linux sftp command
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
- Check whether the server is running the SSH service
- Confirm whether the port is correct
- Check the firewall settings
2. Authentication failure
- Confirm the username and password are correct
- Check the key file permissions (should be 600)
- Ensure your public key is on the server
3. File transfer interrupted
- Check the network connection
- Try using
regetandreputcommand to resume the transfer
4. Insufficient permissions
- Check the write permissions of the remote directory
- May need to use
sudo(but sftp usually does not support direct sudo)
Security best practices
- Use key authenticationinstead of password authentication
- Restrict user permissions, use a chroot environment
- Update regularly SSH server software
- Disable root login, use a regular user + sudo
- 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:
- The basic concepts and connection methods of SFTP
- Commonly used file operations and transfer commands
- Practical batch processing and advanced techniques
- Solutions to common problems
- Best practices for secure usage
Now you can securely and efficiently transfer files between local and remote servers!
Linux Command Reference