How to Use the SFTP Command in Linux
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_server
andremote_server
workstations respectively. Enable thelimited user login
feature 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
.txt
sample files on your local server.console$ touch file1.txt file2.txt file3.txt
Create a new
dir
sample directory on your local server.console$ mkdir dir
Create three
.txt
sample files on the remote server.console$ touch file4.txt file5.txt file6.txt
Create new
dir1
anddir2
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.
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:
List files under the user's home directory on the remote server.
consolesftp> ls
Output:
dir1 dir2 file4.txt file5.txt file6.txt
Upload
file1.txt
from your local server to thedir2
directory on the remote server.consolesftp> put file1.txt dir2
Output:
Download
file4.txt
from the remote server's working directory to your local server.consolesftp> 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:
Upload the
dir
directory from the local server to the remote server using the recursive-r
option.consolesftp> put -r dir /home/linuxuser2
The above command uses the recursive
-r
option to copy thedir
directory and its contents from your local server to the remote server user's home directory/home/linuxuser2
.Output:
Download the
dir1
directory from the remote server to your local server.consolesftp> get -r /home/linuxuser2/dir1 /home/linuxuser1
This command uses the
-r
option to recursively download thedir1
directory 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.txt
from your local server to thedir2
directory on the remote server.consolesftp> put file2.txt
Output:
Use the sftp
Command Advanced Options
Follow the steps below to use advanced options with the sftp
command in Linux.
Copy
file3.txt
to the remote server and preserve the original file's attributes.consolesftp> 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:
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 thedir
directory on your local server.consolesftp> get file5.txt dir/
Exit the SFTP shell.
consolesftp> exit
Output:
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.consolesftp> get /home/linuxuser2/file6.txt /home/linuxuser1
Exit the SFTP shell.
consolesftp> exit
Output:
Execute multiple
sftp
commands at once using a batch file. For example, create a newsftp_batch.txt
file 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 ls
Save and close the file.
Enable execute permissions on the file.
console$ chmod +x sftp_batch.txt
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 thesftp_batch.txt
batch file using the-b
to transfer files to the remote server.Output:
Use the
-P
option 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.txt
from your local server to thedir2
directory on the remote server.consolesftp> put file1.txt /home/linuxuser2
Output:
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> pwd
Output:
Remote working directory: /home/linuxuser2
List files in the remote working directory.
consolesftp> ls
Output:
dir1 dir2 file4.txt file5.txt file6.txt
List files in the local server's working directory using the
lls
command.consolesftp> 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
Create a new directory on the remote server.
consolesftp> mkdir test_directory
List files in the remote working directory and verify that
test_directory
is available.consolesftp> ls
Output:
dir1 dir2 file2.txt file4.txt file5.txt file6.txt test_directory
Remove the
test_directory
from the remote server..consolesftp> rmdir test_directory
Rename
file2.txt
on the remote server toexample.txt
.consolesftp> rename file2.txt example.txt
List files in the remote working directory and verify that
example.txt
is available.consolesftp> ls
Output:
dir1 dir2 example.txt file4.txt file5.txt file6.txt test_directory
Delete
example.txt
from the remote server.consolesftp> rm example.txt
Output:
Removing /home/linuxuser2/example.txt
Switch to the
dir2
on the remote server.consolesftp> cd directory
Print the working directory on your local server.
consolesftp> lpwd
Output:
Local working directory: /home/linuxuser1
Switch to the
dir
directory on your local server.consolesftp> lcd dir
Print the working directory on your local server and verify the
dir
directory path.consolesftp> lpwd
Output:
Local working directory: /home/linuxuser1
Access a new shell on your local server.
consolesftp> !
The above
!
command creates a new sub-shell on your local server within thesftp
session.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
Exit 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.