This guide will explore how to use sftp (SSH File Transfer Protocol), a command-line program for securely transferring files between two Linux computers over a network.
What Is SFTP?
Before defining SFTP, it is important to take a step back and understand FTP (File Transfer Protocol), the predecessor of SFTP.
FTP is a network communication protocol used for transferring files between two computers on a network, typically between a server and a client.
SFTP is a more secure version of FTP. It maintains security by encrypting the data exchanged between two computers by utilizing the SSH (Secure Shell) protocol. FTP, on the other hand, transmits the data as plain text over a network.
SFTP can also help you and your organization achieve certain compliance regulations such as GDPR or CCPA, which require confidential data to be transferred in the most secure way possible.
Accessing a Remote PC
Linux mainly allows you to transfer files with SFTP through the sftp program. Sftp is a robust command-line software that allows you to transfer files over SSH securely and interactively.
You can connect to a remote server through sftp using the following command. Remember to replace the placeholders “username” and “ip-address-of-remote-pc” with the correct username and IP address of the server accordingly.
sftp username@ip-address-of-remote-pc
Enter the server password if prompted. If the terminal greets you with the Connected status, it means that you’ve succeeded in establishing a connection. The blinking cursor simply shows that sftp is in interactive mode.
Note: If you do not have a remote server to interact with, you can try out sftp locally by running the following command:
sftp 127.0.0.1
Interactive sftp Commands
Once you are in the interactive mode, there are several commands that you can use to interact with the remote PC. Some of the most important commands are outlined below. Note that, unlike most Linux commands, sftp commands are not case-sensitive.
Listing Files on a Remote Server
One of the most important tasks is to list files in a directory so that you have an idea of what you want to transfer. You can also get file and folder information of the remote PC, including the size and permissions of the stored files.
To list files on your remote PC, you can use the following command:
ls
You can use most of the ls command flags such as -a, -l, etc. and they will work just fine.
To list files on your local working PC, you can use the following command:
lls
Listing the Current Working Directory
You can also output the path of the current working directory on the remote PC with pwd. The pwd command stands for Present Working Directory.
pwd
To list the present working directory on your local PC:
lpwd
Use the Ctrl + L keyboard shortcut to clear the interactive terminal output.
Uploading Files to a Server
Use the put command to transfer files from your local PC to a remote server. For example, to transfer a Linux Manjaro ISO image from your home folder, use the command below. You can use the -R flag to copy a directory recursively.
put manjaro-xfce-21.0.7-minimal-210614-linux54.iso
The output will be similar to the one shown below. Sftp will display the progress of the file transfer in real-time.
Also, since the file path to the remote server has not been specified, sftp will put the file in a similar path as that of the local PC.
If you wish to copy multiple files, use the mput command instead. For example, to copy all Excel spreadsheets (XLS) files in a directory:
mput *.xls
To interrupt or stop the transfer of a file to the server, you can use the keyboard shortcut Ctrl + C.
Downloading Files From a Server
Use the get command to download files from the server onto your local PC. For example, to copy an HTML file named index.html from the server, simply run the following command:
get index.html
To get multiple files from the server, use the mget command. For example, to copy all HTML files from the server’s present working directory to your local system:
mget *.html
Changing File Permissions
You can also change the permissions of a file on the remote server within the interactive terminal using the chmod command. For example, to grant all access permissions to the index.html file, run the following command:
chmod 777 index.html
Exit the sftp Session
To quit the sftp interactive terminal, you can simply type the command bye. Alternatively, you can use the exit or quit commands as well.
Getting Command-Line Help
Instead of memorizing commands, it is best that you learn how to get help from the command line whenever you need it.
Sftp comes with good documentation. Simply type either of the following commands to get help within the interactive terminal window.
help?
Practical Applications of SFTP
This guide has shown you how to transfer data securely and interactively between Linux computers using the sftp command. If you happen to be a Raspberry Pi user, you might also be interested in learning different ways to transfer data from your Raspberry Pi to a PC.