---
title: How to Use the SFTP Command in Linux
url: https://docs.vultr.com/how-to-use-the-sftp-command-in-linux
description: Learn how to securely transfer files using SFTP in Linux with step-by-step instructions for connecting, uploading, downloading, and managing files via command line.
publish_date: 2024-11-01T10:53:21.852869Z
last_updated: 2024-11-01T10:53:21.852869Z
author: Vishwas Agrawal
header_image: https://docs.vultr.com/public/doc-assets/1995/b02e5ea4-edf1-419e-8135-7bdb998dbcbf.png
---

> Index of every Vultr Docs page in markdown: https://docs.vultr.com/llms.txt

## 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:

* Deploy two [Ubuntu instances](https://www.vultr.com/servers/ubuntu/) on Vultr to use as your `local_server` and `remote_server` workstations respectively. Enable the `limited user login` feature on each instance.
* [Access both instances using SSH](https://www.vultr.com/docs/how-to-use-ssh-with-vultr-servers/).
* [Update the instances](https://www.vultr.com/docs/how-to-update-a-vultr-cloud-server/).

## 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](https://docs.vultr.com/how-to-compress-files-in-linux-using-the-tar-command) 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
    ```

1. Create a new `dir` sample directory on your local server.

    ```console
    $ mkdir dir
    ```

1. Create three `.txt` sample files on the remote server.

    ```console
    $ touch file4.txt file5.txt file6.txt
    ```

1. 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](https://docs.vultr.com/public/doc-assets/1995/2a569c4c-c53d-4ef7-b360-7768547c12b2.png)

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

    ```console
    sftp> ls
    ```

    Output:

    ```
    dir1  dir2  file4.txt  file5.txt  file6.txt
    ```

1. 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](https://docs.vultr.com/public/doc-assets/1995/75ad2948-b2d8-4411-850c-82d60ae73a38.png)

1. 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](https://docs.vultr.com/public/doc-assets/1995/649b6c61-3932-4a51-b7b6-af67ecc7c8b5.png)

1. 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](https://docs.vultr.com/public/doc-assets/1995/63d11a6c-dc70-484b-9db4-fb07ca7ac6e1.png)

1. 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](https://docs.vultr.com/public/doc-assets/1995/ba5fe341-dd35-4438-a8ad-8913fc8895ef.png)

## 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 :key_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](https://docs.vultr.com/public/doc-assets/1995/6c5c29a9-a137-40d2-9832-2173d3e844c0.png)

1. 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](https://docs.vultr.com/public/doc-assets/1995/440e9236-6dba-42f6-927c-b6105bd4d65b.png)

1. 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](https://docs.vultr.com/public/doc-assets/1995/189ac851-2af0-4af1-8397-bc9f9a88204b.png)

1. Exit the SSH console.

    ```console
    $ exit
    ```

1. Connect to the remote server using SFTP.

    ```console
    $ sftp linuxuser2@remote_server
    ```

1. 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](https://docs.vultr.com/public/doc-assets/1995/594c4be9-d379-47ac-a152-6f2c1fba32eb.png)

## 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](https://docs.vultr.com/public/doc-assets/1995/38c33d3d-a9fd-4083-8dfb-06073ff2f227.png)

1. 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](https://docs.vultr.com/public/doc-assets/1995/dec42cd5-811f-4fcb-a04c-11519b30e9d0.png)

1. 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](https://i.postimg.cc/8cGwM7qM/12.png)

1. 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
    ```

1. 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.

1. Enable execute permissions on the file.

    ```console
    $ chmod +x sftp_batch.txt
    ```

1. 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](https://docs.vultr.com/public/doc-assets/1995/ac5b54aa-7957-4e25-9899-aa4084d0dff1.png)

1. 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
    ```

1. 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](https://docs.vultr.com/public/doc-assets/1995/32b77b59-de57-40ae-8a0f-4c82033e3d30.png)

## 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
    ```

1. List files in the remote working directory.

    ```console
    sftp> ls
    ```

    Output:

    ```
    dir1  dir2  file4.txt  file5.txt  file6.txt
    ```

1. 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
    ```

1. Create a new directory on the remote server.

    ```console
    sftp> mkdir test_directory
    ```

1. 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  
    ```

1. Remove the `test_directory` from the remote server..

    ```console
    sftp> rmdir test_directory
    ```

1. Rename `file2.txt` on the remote server to `example.txt`.

    ```console
    sftp> rename file2.txt example.txt
    ```

1. 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  
    ```

1. Delete `example.txt` from the remote server.

    ```console
    sftp> rm example.txt
    ```

    Output:

    ```
    Removing /home/linuxuser2/example.txt
    ```

1. Switch to the `dir2` on the remote server.

    ```console
    sftp> cd directory
    ```

1. Print the working directory on your local server.

    ```console
    sftp> lpwd
    ```

    Output:

    ```
    Local working directory: /home/linuxuser1
    ```

1. Switch to the `dir` directory on your local server.

    ```console
    sftp> lcd dir
    ```

1. Print the working directory on your local server and verify the `dir` directory path.

    ```console
    sftp> lpwd
    ```

    Output:

    ```
    Local working directory: /home/linuxuser1
    ```

1. 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.

1. 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
    ```

1. Exit the new shell.

    ```console
    $ exit
    ```

    Output:

    ```
    exit

    sftp> 
    ```

1. 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.
