How to Use the SFTP Command in Linux

By Raman Kumar

Updated on Dec 11, 2024

In this tutorial, we'll explain how to use the SFTP command in Linux.

The sftp (Secure File Transfer Protocol) command in Linux provides a secure and straightforward method for transferring files between a local machine and a remote server over an SSH (Secure Shell) connection. Unlike FTP, SFTP encrypts the data in transit, making it a more secure choice for file transfers. This tutorial will guide you through basic and advanced usage of the sftp command, ensuring you can leverage its full capabilities for secure file management.

Understanding SFTP

SFTP operates as an extension of SSH. It uses the same secure channel to transfer files, ensuring the data, authentication credentials, and commands are encrypted. This makes it an ideal choice for transferring sensitive data.

Prerequisites

Before you begin using the sftp command, ensure the following:

  • A KVM VPS or dedicated server with any Linux distro installed.
  • SSH Access: You must have SSH access to the remote system.
  • SFTP Client: The sftp utility is typically pre-installed on most Linux distributions. You can verify its presence by running:
sftp -v

If not installed, you can install the OpenSSH client package:

sudo apt update && sudo apt install openssh-client  # For Ubuntu/Debian
sudo yum install openssh-clients                   # For CentOS/RHEL

Credentials: You need the username and password or an SSH key for authentication.

How to Use the SFTP Command in Linux

Establishing an SFTP Connection

To initiate an SFTP session, use the following syntax:

sftp username@remote_host

Replace username with your remote system’s username and remote_host with its IP address or domain name. For example:

sftp user@example.com

Once connected, you will be prompted to enter the password. If authentication succeeds, you will enter the sftp> interactive prompt.

Basic SFTP Commands

Here are some essential SFTP commands for everyday use:

1. Navigating Directories

Change Directory on Remote System:

cd /path/to/remote/directory

Change Directory on Local System:

lcd /path/to/local/directory

List Directory Contents:

ls  # Remote directory
lls # Local directory

2. Transferring Files

Download a File:

get remote_file_path

Example:

get /remote/path/file.txt

Download Multiple Files:

mget file1 file2 file3

Upload a File:

put local_file_path

Example:

put /local/path/file.txt

Upload Multiple Files:

mput file1 file2 file3

3. Working with Files

Rename a File:

rename old_name new_name

Remove a File:

rm file_name

Create a Directory:

mkdir directory_name

Remove a Directory:

rmdir directory_name

4. Checking File Transfers

Check File Details:

stat file_name

Verify the Current Working Directory:

pwd  # Remote working directory
lpwd # Local working directory

Advanced SFTP Usage

1. Using SSH Key Authentication

For enhanced security, use SSH keys instead of passwords:

Generate an SSH Key Pair:

ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa

Copy the Public Key to the Remote Server:

ssh-copy-id username@remote_host

Connect Using SFTP:

sftp username@remote_host

2. Batch File Transfers

Automate file transfers using a batch file:

Create a Batch File:

echo -e "put file1.txt\nget file2.txt" > batch.txt

Run SFTP with the Batch File:

sftp -b batch.txt username@remote_host

3. Resume Interrupted Transfers

Use the reget and reput commands to resume incomplete downloads or uploads:

Resume Download:

reget file_name

Resume Upload:

reput file_name

4. File Permissions

Modify file permissions directly within the SFTP session:

chmod 644 file_name

Exiting an SFTP Session

To exit the SFTP session, type:

exit

or

bye

Troubleshooting Common Issues

1. Connection Refused

Ensure the SSH service is running on the remote server:

sudo systemctl start sshd

2. Permission Denied

Verify your username, password, or SSH key permissions.

3. Timeout Issues

Increase the SSH timeout settings by editing the SSH configuration file on the server (/etc/ssh/sshd_config) and setting:

ClientAliveInterval 120
ClientAliveCountMax 720

Conclusion

The sftp command is a powerful and secure method for transferring files in Linux environments. By mastering both its basic and advanced features, you can efficiently manage file transfers between systems while maintaining robust security. Incorporate these practices into your workflow to optimize data handling in various scenarios.