
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 on Vultr to use as your
local_serverandremote_serverworkstations respectively. Enable thelimited user loginfeature on each instance. - Access both instances using SSH.
- Update the instances.
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.
Create three
.txtsample files on your local server.console$ touch file1.txt file2.txt file3.txt
Create a new
dirsample directory on your local server.console$ mkdir dir
Create three
.txtsample files on the remote server.console$ touch file4.txt file5.txt file6.txt
Create new
dir1anddir2sample 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.
Connect to the remote server using the
sftpcommand to open the interactive file transfer shell.console$ sftp linuxuser2@remote_server
Enter
yesto accept the remote server's fingerprint key and enter the user password when prompted.Output:

List files under the user's home directory on the remote server.
consolesftp> lsOutput:
dir1 dir2 file4.txt file5.txt file6.txtUpload
file1.txtfrom your local server to thedir2directory on the remote server.consolesftp> put file1.txt dir2Output:

Download
file4.txtfrom the remote server's working directory to your local server.consolesftp> get file4.txtThe above command downloads the
file4.txtfile from the working directory on your remote server and saves it to the working directory on your local server directory.Output:

Upload the
dirdirectory from the local server to the remote server using the recursive-roption.consolesftp> put -r dir /home/linuxuser2The above command uses the recursive
-roption to copy thedirdirectory and its contents from your local server to the remote server user's home directory/home/linuxuser2.Output:

Download the
dir1directory from the remote server to your local server.consolesftp> get -r /home/linuxuser2/dir1 /home/linuxuser1This command uses the
-roption to recursively download thedir1directory from the remote server to your local server user's home directory.Output:

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

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:

Access the remote server using SSH and verify that you are not prompted for the user's password.
console$ ssh linuxuser2@remote_server
Output:

Exit the SSH console.
console$ exit
Connect to the remote server using SFTP.
console$ sftp linuxuser2@remote_server
Upload
file2.txtfrom your local server to thedir2directory on the remote server.consolesftp> put file2.txtOutput:

Use the sftp Command Advanced Options
Follow the steps below to use advanced options with the sftp command in Linux.
Copy
file3.txtto the remote server and preserve the original file's attributes.consolesftp> put -p file3.txt /home/linuxuser2The above command copies the
file3.txtfile to the/home/linuxuser2user home directory on the remote server. The-poption keeps the file's original timestamps and permissions during transfer.Output:

Use the
-Coption to enable compression and speed up the SFTP transfer.console$ sftp -C linuxuser2@remote_server
Download
file5.txtfrom the remote server to thedirdirectory on your local server.consolesftp> get file5.txt dir/Exit the SFTP shell.
consolesftp> exitOutput:

Use the
-ioption to set a specific private key for authentication. Replace/path/to/private_keywith 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_rsafile in your files to use as the private key that corresponds to your public key file with a.pubextension.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.oldDownload
file6.txtfile from the remote server to your local server.consolesftp> get /home/linuxuser2/file6.txt /home/linuxuser1Exit the SFTP shell.
consolesftp> exitOutput:

Execute multiple
sftpcommands at once using a batch file. For example, create a newsftp_batch.txtfile on your local server.console$ nano sftp_batch.txt
Add the following contents to the file.
cd /home/linuxuser2/dir1 put file1.txt get remote_file.txt mkdir new_remote_directory lsSave and close the file.
Enable execute permissions on the file.
console$ chmod +x sftp_batch.txt
Connect to the remote server using the
-boption and specify the batch file to execute.console$ sftp -b sftp_batch.txt linuxuser2@remote_server
The above
sftpcommand executes all commands in thesftp_batch.txtbatch file using the-bto transfer files to the remote server.Output:

Use the
-Poption to connect to the remote server using a custom SSH port to transfer files. For example, the default SSH port22.console$ sftp -P 22 linuxuser2@remote_server
Upload
file1.txtfrom your local server to thedir2directory on the remote server.consolesftp> put file1.txt /home/linuxuser2Output:

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.
Print the working remote directory in your SFTP session.
consolesftp> pwdOutput:
Remote working directory: /home/linuxuser2List files in the remote working directory.
consolesftp> lsOutput:
dir1 dir2 file4.txt file5.txt file6.txtList files in the local server's working directory using the
llscommand.consolesftp> llsThe
llscommand 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.txtCreate a new directory on the remote server.
consolesftp> mkdir test_directoryList files in the remote working directory and verify that
test_directoryis available.consolesftp> lsOutput:
dir1 dir2 file2.txt file4.txt file5.txt file6.txt test_directoryRemove the
test_directoryfrom the remote server..consolesftp> rmdir test_directoryRename
file2.txton the remote server toexample.txt.consolesftp> rename file2.txt example.txtList files in the remote working directory and verify that
example.txtis available.consolesftp> lsOutput:
dir1 dir2 example.txt file4.txt file5.txt file6.txt test_directoryDelete
example.txtfrom the remote server.consolesftp> rm example.txtOutput:
Removing /home/linuxuser2/example.txtSwitch to the
dir2on the remote server.consolesftp> cd directoryPrint the working directory on your local server.
consolesftp> lpwdOutput:
Local working directory: /home/linuxuser1Switch to the
dirdirectory on your local server.consolesftp> lcd dirPrint the working directory on your local server and verify the
dirdirectory path.consolesftp> lpwdOutput:
Local working directory: /home/linuxuser1Access a new shell on your local server.
consolesftp> !The above
!command creates a new sub-shell on your local server within thesftpsession.View your server's storage usage in the new shell using the
df -hcommand.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/shmExit the new shell.
console$ exit
Output:
exit sftp>Exit the SFTP session.
consolesftp> 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.