Linux Comm Sftp
[ Linux Command Encyclopedia](#)
* * *
## What is sftp?
SFTP (SSH File Transfer Protocol) is a secure file transfer protocol that transfers 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 | Plain text transmission |
| Port | 22 (SSH port) | 21 (control port) + 20 (data port) |
| Protocol | Based on SSH | Independent protocol |
| Firewall friendly | Only one port needs to be open | Multiple ports need to be open |
* * *
## sftp Command Basic Syntax
sftp [username@]hostname[:path]
### Common Option Parameters
| Option | Description |
| --- | --- |
| `-P port` | Specify connection port (default 22) |
| `-b batchfile` | Execute commands in batch file |
| `-r` | Recursively copy entire directory |
| `-v` | Show detailed debug information |
| `-i private_key_file` | Authenticate using specified private key file |
* * *
## Connecting to SFTP Server
### Basic Connection Method
## Example
sftp username@hostname
Example:
## Example
sftp user@example.com
### Connecting with Specified Port
If the server uses a non-standard port (not 22):
## Example
sftp -P 2222 user@example.com
### Using Key Authentication
## Example
sftp -i ~/.ssh/id_rsa user@example.com
* * *
## Common SFTP Operations
### 1. Local File Operations
| Command | Description |
| --- | --- |
| `lls` | List local directory contents |
| `lcd` | Change local working directory |
| `lmkdir` | Create directory locally |
| `lrm` | Delete local file |
### 2. Remote File Operations
| Command | Description |
| --- | --- |
| `ls` | List remote directory contents |
| `cd` | Change remote working directory |
| `mkdir` | Create directory on remote |
| `rm` | Delete remote file |
| `rename` | Rename remote file |
### 3. File Transfer
| Command | Description |
| --- | --- |
| `put local_file ` | Upload file to remote server |
| `get remote_file ` | Download file from remote server |
| `mput local_files*` | Upload multiple files |
| `mget remote_files*` | Download multiple files |
* * *
## Practical Examples
### Example 1: Upload File to Server
## Example
sftp user@example.com
sftp>cd/var/www/html
sftp> put index.html
sftp>exit
### Example 2: Download Entire Directory (Recursive)
## Example
sftp -r user@example.com:/backups /local/backups
### Example 3: Using Batch File
Create `sftp_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. Using 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. Changing File Permissions
## Example
sftp>chmod 644 index.html
### 3. Viewing File Information
## Example
sftp>ls-l
### 4. Using Symbolic Links
## Example
sftp> symlink /path/to/original /path/to/link
* * *
## Troubleshooting Common Issues
### 1. Connection Refused
* Check if the server is running SSH service
* Confirm if the port is correct
* Check firewall settings
### 2. Authentication Failed
* Confirm username and password are correct
* Check key file permissions (should be 600)
* Ensure your public key is on the server
### 3. File Transfer Interrupted
* Check network connection
* Try using `reget` and `reput` commands to resume transfer
### 4. Insufficient Permissions
* Check write permissions on remote directory
* May need to use `sudo` (but sftp usually doesn't support direct sudo)
* * *
## Security Best Practices
1. **Use key authentication** instead of password authentication
2. **Limit user permissions**, use chroot environment
3. **Regularly update** SSH server software
4. **Disable root login**, use 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 operations and transfer commands
* Practical batch processing and advanced tips
* Solutions to common problems
* Best practices for secure usage
Now you can safely and efficiently transfer files between local and remote servers!
[ Linux Command Encyclopedia](#)
YouTip