Secure File Transfer Protocol (SFTP) is a network protocol. It provides secure file access, transfer, and management. This occurs over a reliable data stream. SFTP is commonly used with SSH to provide a secure channel. Let's explore the SFTP command in Linux, its usage, options, and real-world examples. You will understand how to transfer files securely.
What is SFTP?
SFTP stands for Secure File Transfer Protocol. It is a secure way to transfer files between computers. Unlike FTP, SFTP encrypts both commands and data. This protects sensitive information during transmission. It is often preferred over other file transfer methods. You can learn more about Linux here.
Why Use SFTP?
SFTP provides several advantages. These benefits make it a popular choice for secure file transfer. These benefits include:
- Security: Encrypts data to protect against eavesdropping.
- Authentication: Supports various authentication methods, including passwords and SSH keys.
- Integrity: Ensures data integrity during transfer.
- Firewall Friendly: Typically uses a single port (port 22 by default), simplifying firewall configuration.
Basic SFTP Usage
To start an SFTP session, use the following command:
sftp user@host
Replace
user
with your username. Replace
host
with the server's address. SFTP will then prompt you for your password. It will establish a secure connection.
Common SFTP Commands
Once connected, you can use various SFTP commands to manage files. Here are some of the most common:
-
ls
: Lists files in the remote directory. -
pwd
: Prints the remote working directory. -
cd
: Changes the remote directory. -
get
: Downloads a file from the remote server to your local machine. -
put
: Uploads a file from your local machine to the remote server. -
rm
: Deletes a file on the remote server. -
mkdir
: Creates a directory on the remote server. -
rmdir
: Removes a directory on the remote server. -
exit
: Closes the SFTP connection.
Examples of SFTP Commands
Let's look at practical examples. These will illustrate the usage of SFTP commands.
Downloading a File
To download a file named
document.txt
, use the
get
command:
get document.txt
This command downloads
document.txt
. It saves it to your current local directory.
Uploading a File
To upload a file named
image.jpg
, use the
put
command:
put image.jpg
This command uploads
image.jpg
. It places it into the current remote directory.
Navigating Directories
To change to the
/var/www/html
directory on the remote server, use the
cd
command:
cd /var/www/html
This command changes the current remote directory. It moves to
/var/www/html
.
Advanced SFTP Options
SFTP also supports advanced options. These options can customize the file transfer process.
Using SSH Keys for Authentication
SSH keys provide a more secure way to authenticate. This avoids entering passwords repeatedly. To use SSH keys, ensure your public key is added to the
~/.ssh/authorized_keys
file on the remote server.
Specifying a Port
If the SFTP server uses a non-standard port, specify it using the
-P
option:
sftp -P 2222 user@host
In this example, SFTP connects to port 2222 on the remote server.
Batch Mode
Batch mode allows you to execute multiple SFTP commands. This executes them from a file. Use the
-b
option:
sftp -b commands.txt user@host
Here,
commands.txt
contains a list of SFTP commands. Each command is on a new line.
SFTP Scripting
SFTP can be incorporated into scripts. This automates file transfer tasks. Here's an example script:
#!/bin/bash
sftp user@host << END_SFTP
cd /var/www/html
get important_file.txt
exit
END_SFTP
This script connects to the SFTP server. It changes to the
/var/www/html
directory. It downloads
important_file.txt
. Then, it closes the connection.
SFTP vs. Other File Transfer Protocols
SFTP is often compared to other file transfer protocols like FTP and SCP.
- FTP: FTP (File Transfer Protocol) lacks built-in encryption. This makes it less secure than SFTP.
- SCP: SCP (Secure Copy Protocol) is also secure. However, SFTP offers more features. This includes directory management.
What is the default port for SFTP?
The default port for SFTP is port 22, the same as SSH.
How do I use SFTP with SSH keys?
Ensure your public key is added to the ~/.ssh/authorized_keys file on the remote server. Then, SFTP will automatically use the key for authentication.
How do I download multiple files with SFTP?
You can use a wildcard with the get command (e.g.,
get *.txt
) or use batch mode to execute multiple get commands from a file.
Is SFTP more secure than FTP?
Yes, SFTP is significantly more secure than FTP. SFTP encrypts both commands and data, protecting against eavesdropping, while FTP transmits data in plaintext.
How to resume interrupted SFTP transfers?
SFTP does not natively support resuming interrupted transfers. Consider using tools like `rsync` over SSH for more robust and resumable transfers.
Conclusion
SFTP is a powerful and secure method for file transfer. It provides encryption, authentication, and integrity. These features make it ideal for various applications. Understanding SFTP commands and options allows you to transfer files efficiently. You can do so securely. Master SFTP to enhance your file management capabilities.
Understanding SFTP command is very crucial for daily work.