How to Use the SFTP Command in Linux

Updated on November 1, 2024
How to Use the SFTP Command in Linux header image

Introduction

The sftp (Secure File Transfer Protocol) command in Linux allows you to securely transfer files between local and remote systems over SSH. The SFTP command ensures data integrity and confidentiality by encrypting commands and data during transit, unlike traditional FTP applications. SFTP includes an interactive shell environment for downloading, uploading, and managing files.

This article explains how to use the sftp command to securely transfer files and directories in Linux.

Prerequisites

Before you begin:

The sftp Command Syntax

The following is a basic sftp command syntax.

sftp [options] [user@host]

In the above sftp command:

  • [options]: Includes optional flags that modify the file transfer process.
  • [user@host]: specifies the user and remote host when transferring files.

Explore how the tar command in Linux enables efficient file compression and archiving for streamlined data storage and transfer.

Use Most Common sftp Command Options

Below are the most common sftp command options in Linux:

Option Description
-P Specifies the port to connect to on the remote host
-p Preserves the file modification and access attributes
-r Recursively copies the entire directory contents
-q Quiet mode: suppresses non-error messages
-C Enables compression during the file transfer process
-v Verbose mode: displays detailed debugging messages
-i Selects the identity (private key) file to use for public key authentication

Create Sample Directories and Files

This article uses the following usernames and example hostnames. Replace the values with your actual usernames and server public IP addresses.

Hostname Username
local_server linuxuser1
remote_server linuxuser2

Follow the steps below to create new sample files and directories to test the sftp command on your local_server and remote_server.

  1. Create three .txt sample files on your local server.

    console
    $ touch file1.txt file2.txt file3.txt
    
  2. Create a new dir sample directory on your local server.

    console
    $ mkdir dir
    
  3. Create three .txt sample files on the remote server.

    console
    $ touch file4.txt file5.txt file6.txt
    
  4. Create new dir1 and dir2 sample directories on the remote server.

    console
    $ mkdir dir1 dir2
    

Transfer Files Using the sftp Command in Linux

Follow the steps below to securely transfer files using the sftp command to your remote server.

  1. Connect to the remote server using the sftp command to open the interactive file transfer shell.

    console
    $ sftp linuxuser2@remote_server
    

    Enter yes to accept the remote server's fingerprint key and enter the user password when prompted.

    Output:

    connect to remote server using SFTP

  2. List files under the user's home directory on the remote server.

    console
    sftp> ls
    

    Output:

    dir1  dir2  file4.txt  file5.txt  file6.txt
  3. Upload file1.txt from your local server to the dir2 directory on the remote server.

    console
    sftp> put file1.txt dir2
    

    Output:

    upload file to remote server

  4. Download file4.txt from the remote server's working directory to your local server.

    console
    sftp> get file4.txt
    

    The above command downloads the file4.txt file from the working directory on your remote server and saves it to the working directory on your local server directory.

    Output:

    download file from remote server

  5. Upload the dir directory from the local server to the remote server using the recursive -r option.

    console
    sftp> put -r dir /home/linuxuser2
    

    The above command uses the recursive -r option to copy the dir directory and its contents from your local server to the remote server user's home directory /home/linuxuser2.

    Output:

    upload directory to remote server

  6. Download the dir1 directory from the remote server to your local server.

    console
    sftp> get -r /home/linuxuser2/dir1 /home/linuxuser1
    

    This command uses the -r option to recursively download the dir1 directory from the remote server to your local server user's home directory.

    Output:

    download directory from remote server

Set up an SSH Key for Passwordless Authentication

SFTP uses password authentication by default unless you specify a private key using the -i option. SSH Keys allow SFTP clients to connect to remote hosts without a password. Follow the steps below to set up a new SSH key and enable passwordless authentication.

  1. Generate a new SSH key pair on your local server.

    console
    $ ssh-keygen -t rsa -b 4096
    

    Press Enter to accept the default SSH file location, such as /home/linuxuser1/.ssh/id_rsa, and set an optional passphrase to secure the SSH key.

    Output:

    Generate SSH key pair

  2. Copy your SSH public key to the remote server.

    console
    $ ssh-copy-id linuxuser2@remote_server
    

    Enter the remote user's password when prompted to copy the SSH public key.

    Output:

    Copy SSH public key

  3. Access the remote server using SSH and verify that you are not prompted for the user's password.

    console
    $ ssh linuxuser2@remote_server
    

    Output:

    Verify passwordless login

  4. Exit the SSH console.

    console
    $ exit
    
  5. Connect to the remote server using SFTP.

    console
    $ sftp linuxuser2@remote_server
    
  6. Upload file2.txt from your local server to the dir2 directory on the remote server.

    console
    sftp> put file2.txt
    

    Output:

    Copy a file using SFTP without password

Use the sftp Command Advanced Options

Follow the steps below to use advanced options with the sftp command in Linux.

  1. Copy file3.txt to the remote server and preserve the original file's attributes.

    console
    sftp> put -p file3.txt /home/linuxuser2
    

    The above command copies the file3.txt file to the /home/linuxuser2 user home directory on the remote server. The -p option keeps the file's original timestamps and permissions during transfer.

    Output:

    preserve file attributes

  2. Use the -C option to enable compression and speed up the SFTP transfer.

    console
    $ sftp -C linuxuser2@remote_server
    
    • Download file5.txt from the remote server to the dir directory on your local server.

      console
      sftp> get file5.txt dir/
      
    • Exit the SFTP shell.

      console
      sftp> exit
      

      Output:

      enable compression in sftp

  3. Use the -i option to set a specific private key for authentication. Replace /path/to/private_key with your private key path.

    console
    $ sftp -i /path/to/private_key linuxuser2@remote_server
    
    • Run the following command in your user home directory to view the default SSH key files.

      console
      $ ls -l /home/linuxuser1/.ssh/
      

      Find the id_rsa file in your files to use as the private key that corresponds to your public key file with a .pub extension.

      Output:

      -rw------- 1 linuxuser1 linuxuser1 3381 Oct  1 19:11 id_rsa
      -rw-r--r-- 1 linuxuser1 linuxuser1  744 Oct  1 19:11 id_rsa.pub
      -rw------- 1 linuxuser1 linuxuser1  978 Oct  1 15:50 known_hosts
      -rw-r--r-- 1 linuxuser1 linuxuser1  142 Oct  1 15:50 known_hosts.old
    • Download file6.txt file from the remote server to your local server.

      console
      sftp> get /home/linuxuser2/file6.txt /home/linuxuser1
      
    • Exit the SFTP shell.

      console
      sftp> exit
      

      Output:

      use identity file in sftp

  4. Execute multiple sftp commands at once using a batch file. For example, create a new sftp_batch.txt file on your local server.

    console
    $ nano sftp_batch.txt
    
  5. Add the following contents to the file.

    cd /home/linuxuser2/dir1
    put file1.txt
    get remote_file.txt
    mkdir new_remote_directory
    ls

    Save and close the file.

  6. Enable execute permissions on the file.

    console
    $ chmod +x sftp_batch.txt
    
  7. Connect to the remote server using the -b option and specify the batch file to execute.

    console
    $ sftp -b sftp_batch.txt linuxuser2@remote_server
    

    The above sftp command executes all commands in the sftp_batch.txt batch file using the -b to transfer files to the remote server.

    Output:

    batch mode in sftp

  8. Use the -P option to connect to the remote server using a custom SSH port to transfer files. For example, the default SSH port 22.

    console
    $ sftp -P 22 linuxuser2@remote_server
    
  9. Upload file1.txt from your local server to the dir2 directory on the remote server.

    console
    sftp> put file1.txt /home/linuxuser2
    

    Output:

    use sftp with a custom SSH port

Use Interactive SFTP Command Options

SFTP supports interactive shell commands for navigating and interacting with files on the remote server.

Command Description
ls Lists the remote directory contents
lls Lists the local directory contents
cd Changes the working remote directory
lcd Changes the local working directory
pwd Prints the working remote directory
lpwd Prints the working local directory
mkdir Creates a new directory on the remote server
rmdir Removes a directory on the remote server
rm Deletes a file on the remote server
rename Renames a file on the remote server

Follow the steps below to use interactive commands in your SFTP session.

  1. Print the working remote directory in your SFTP session.

    console
    sftp> pwd
    

    Output:

    Remote working directory: /home/linuxuser2
  2. List files in the remote working directory.

    console
    sftp> ls
    

    Output:

    dir1  dir2  file4.txt  file5.txt  file6.txt
  3. List files in the local server's working directory using the lls command.

    console
    sftp> lls
    

    The lls command lists all files in the local working directory enabling you to view and verify files downloaded from the remote server.

    Output:

    dir  file1.txt  file2.txt  file3.txt
  4. Create a new directory on the remote server.

    console
    sftp> mkdir test_directory
    
  5. List files in the remote working directory and verify that test_directory is available.

    console
    sftp> ls
    

    Output:

    dir1               dir2               file2.txt          file4.txt          file5.txt          
    file6.txt          test_directory  
  6. Remove the test_directory from the remote server..

    console
    sftp> rmdir test_directory
    
  7. Rename file2.txt on the remote server to example.txt.

    console
    sftp> rename file2.txt example.txt
    
  8. List files in the remote working directory and verify that example.txt is available.

    console
    sftp> ls
    

    Output:

    dir1               dir2               example.txt          file4.txt          file5.txt          
    file6.txt          test_directory  
  9. Delete example.txt from the remote server.

    console
    sftp> rm example.txt
    

    Output:

    Removing /home/linuxuser2/example.txt
  10. Switch to the dir2 on the remote server.

    console
    sftp> cd directory
    
  11. Print the working directory on your local server.

    console
    sftp> lpwd
    

    Output:

    Local working directory: /home/linuxuser1
  12. Switch to the dir directory on your local server.

    console
    sftp> lcd dir
    
  13. Print the working directory on your local server and verify the dir directory path.

    console
    sftp> lpwd
    

    Output:

    Local working directory: /home/linuxuser1
  14. Access a new shell on your local server.

    console
    sftp> !
    

    The above ! command creates a new sub-shell on your local server within the sftp session.

  15. View your server's storage usage in the new shell using the df -h command.

    console
    $ df -h
    

    Output:

    Filesystem      Size  Used Avail Use% Mounted on
    tmpfs           392M  1.2M  390M   1% /run
    efivarfs        256K   23K  229K  10% /sys/firmware/efi/efivars
    /dev/vda2        28G  6.8G   20G  26% /
    tmpfs           2.0G     0  2.0G   0% /dev/shm
  16. Exit the new shell.

    console
    $ exit
    

    Output:

    exit
    
    sftp> 
  17. Exit the SFTP session.

    console
    sftp> exit
    

Conclusion

You have used the sftp command in Linux to transfer files from one host to another. You can use the sftp command with other Linux commands and options to securely transfer files and directories between hosts. For more information and command options, run man sftp to view the SFTP command manual.