Lesson 94: sftp Command
In this lesson, you'll learn how to use the sftp command to securely transfer files between local and remote Linux hosts.
File Transfer Protocol (FTP) was a widely used protocol to transfer files or data remotely in an unencrypted format, which is not a secure way to communicate.
As we all know, the File Transfer Protocol is not at all secure because all transmissions happen in clear text, and the data can be readable by anyone during sniffing the packets on the network.
So, basically, FTP can be used in limited cases or on the networks that you trust.
Over a period of time, SCP (Secure Copy) and SSH (Secure Shell) addressed this security ambiguity and added an encrypted secure layer while transferring data between remote computers.
SFTP (Secure File Transfer Protocol) runs over SSH protocol on standard port 22 by default to establish a secure connection.
SFTP has been integrated into many GUI tools (FileZilla, WinSCP, FireFTP, etc.).
Security Warning: Please don't open the SSH port (Secure SHell) globally as this would be a security breach.
You can only open it for specific IPs from where you are going to transfer or manage files on the remote system or vice versa.
Let's explore some sftp command examples to use through the interactive command-line interface in the Linux terminal.
sftp Command Syntax
$ sftp username@remote_host
sftp Interactive Commands
| Command | Description |
|---|---|
pwd |
Display the remote working directory |
lpwd |
Display the local working directory |
ls |
List files in the remote directory |
lls |
List files in the local directory |
cd PATH |
Change the remote directory |
lcd PATH |
Change the local directory |
mkdir PATH |
Create a remote directory |
lmkdir PATH |
Create a local directory |
put FILE |
Upload a single file to the remote server |
mput *.ext |
Upload multiple files to the remote server |
get FILE |
Download a single file from the remote server |
mget *.ext |
Download multiple files from the remote server |
rm FILE |
Delete a file on the remote server |
rmdir DIR |
Remove a directory on the remote server |
! |
Drop into a local shell |
exit or bye or quit |
Exit the sftp session |
? or help |
Display help for all available commands |
1. Connect to SFTP
By default, the same SSH protocol is used to authenticate and establish an SFTP connection.
To start an SFTP session, enter the username and remote hostname or IP address at the command prompt.
Once authentication is successful, you will see a shell with an sftp> prompt.
root@TecMint:~$ sftp [email protected]
The authenticity of host '192.168.168.136 (192.168.168.136)' can't be established.
ED25519 key fingerprint is SHA256:ev0t2i5/GVPb/ch6zpR/Brta+pAQ3m+jYL6f1AOPPGA.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.168.136' (ED25519) to the list of known hosts.
[email protected]'s password:
Connected to 192.168.168.136.
sftp>
2. Get Help
Once you are in the sftp> prompt, check the available commands by typing ? or help at the command prompt.