Overview
SFTP (SSH File Transfer Protocol) is a protocol developed to replace the less secure FTP. The `sftp` command uses an SSH connection to encrypt all data and commands, eliminating the risk of exposing usernames, passwords, and file contents over the network. Like FTP, it supports an interactive mode, and its usage is similar, making it easy for users familiar with FTP to use.
Key Features
The main features of the `sftp` command are:
- Based on SSH, it encrypts all communications.
- Provides an interactive command interface similar to FTP.
- Uses a single port (default 22), which is advantageous in firewall environments.
- Supports various functions such as file upload (`put`), download (`get`), and directory navigation (`cd`, `lcd`).
SFTP vs FTP
SFTP and FTP differ significantly in file transfer methods and security aspects.
- SFTP: Transfers data securely by encrypting it via SSH. Uses port 22 and handles all communication over a single port.
- FTP: Transfers data in plain text, making it vulnerable to security breaches. Uses port 21 and requires separate ports for data transfer.
Key Options
Similar to FTP, `sftp` can be used by entering interactive mode or non-interactively with specific commands.
1) Execution Options
2) Interactive Mode Internal Commands
Generated command:
Try combining the commands.
Description:
`sftp` Executes the command.
Combine the above options to virtually execute commands with AI.
Usage Examples
Explore the various usage examples of the `sftp` command to learn its functionalities.
Connect to Server in Interactive Mode
sftp user@server.com
Connect to `server.com` with the `user` account to enter interactive mode.
Download File
sftp user@server.com
... (접속 후)
cd /var/log
get syslog ~
Download the `syslog` file from the remote server's `/var/log` directory to the local `~` directory.
Upload File
sftp user@server.com
... (접속 후)
cd /var/www
put /home/user/document.txt
Upload the `/home/user/document.txt` file from the local system to the remote server's `/var/www` directory.
Transfer File in Non-Interactive Mode
sftp user@server.com:/remote/path <<< 'put localfile.txt'
Upload a file in a single command using the `put` command in non-interactive mode.
Installation
sftp is typically included by default as part of the `openssh-client` package in most Linux distributions. If an SSH client is already installed, no separate installation is required.
Debian/Ubuntu
sudo apt update
sudo apt install -y openssh-client
RHEL/CentOS/Fedora
sudo dnf install -y openssh-clients
Tips & Cautions
Here are some points to note when using the `sftp` command.
Tips
- Like `scp`, sftp uses SSH authentication information. Therefore, connection settings can be easily managed using the `~/.ssh/config` file.
- In interactive mode, typing `help` will show a list of all internal commands. In addition to `put` and `get`, you can transfer multiple files using `mput` and `mget`.
- If network issues or authentication errors occur, using the `-v` option to output detailed information can help with troubleshooting.