What is SFTP?
Simple File Transfer Protocol (SFTP) is a protocol that is used to transfer files between two devices over the internet. It runs over File Transfer Protocol (FTP) and Secure Shell (SSH) protocols.
While the protocol transfers files, it also protects against password sniffing, man-in-the-middle attack, and sensitive data exposure. It also preserves the integrity of the data with the help of encryption and cryptographic hash functions. It also authenticates both the client and server before giving access. This allows secure transfer of files.
In this tutorial, I will show you how you can easily exchange files between local and remote machines using this super-handy file transfer protocol.
For this guide, I have used Linux Mint Cinamon 20.02 as my local machine and Ubuntu 20.04 LTS as my remote machine.
The tutorial is divided into two parts. In the first part, you will see how to transfer a file from a remote machine to a local machine. In the other part, you will see how to transfer a file from your local machine to a remote machine.
Let’s get started!
Transfer file from a remote machine to a local machine
Step 1: SSH to Remote Machine
The first step is to make an SSH connection to your remote machine. To do that, first, become a superuser using the following command:
Now that you have become a superuser, ssh to the remote machine you want to communicate with to get the file.
ssh [email protected]
Step 2: Locate the file to transfer
Then, locate the file you want to copy. In this tutorial, I will create a new file to transfer it to my local machine.
To do that, I will go to the /etc directory by running the command below:
With the help of the touch command, I will create a new file with the name transfer.txt.
Step 3: Establish an SFTP connection with the remote machine
Now, we will connect to the remote machine using sftp, as shown below:
sftp [email protected]
If you see sftp written like this, that means you are in sftp mode. Now the local and remote machines can exchange files using this protocol.
Step 4: Check which directories sftp interacts with
Before going ahead to transfer files, let’s navigate a little to understand which directories sftp interacts with. Run the command below to know your present working directory:
To see the local machine’s present working directory, we’ll use:
We can also change the directory we want to interact with. It is included in the second part of this guide.
Step 5: Transfer the file
Let’s go to the /etc directory where we created the transfer.txt file.
To send a file from the remote machine to the local machine, we use the get command followed by the filename as shown below:
Now got to your local system’s root directory with the following command:
Use ls to see all the files in the directory.
As you can see, the transfer.txt file is now in our local machine.
After closing the sftp session, you can transfer the file from your root directory to any other folder on your machine.
Transfer file from a local machine to a remote machine
Now that we have seen how to transfer a file from a remote machine to a local one, let’s see how we can go in the opposite direction.
We have another file test.txt on our local machine in the following directory path:
From here we will transfer it to our remote machine.
Step 1: Connect to the remote machine using SFTP
sftp [email protected]
Step 2: Change the local directory sftp interacts with
The local present working directory is:
As I told you earlier, we can change the local directory we want sftp to interact with. To do that, use the lcd command:
Before this, sftp was interacting with the local machine’s root directory. Now it will interact with the /home/azifa/Downloads/folder directory. You can confirm it with the following command:
Step 4: Transfer the file
Next, we use the put command to transfer the file from the local machine to the remote machine.
Run the ls command to see if the file has been successfully transferred to the /home directory of the remote machine.
Yes, the file we transferred is here in the remote machine.
Now you can end the session with a simple bye command.
In this article, I explained to you how easily you can transfer files between a remote machine and a local machine with the help of a file transfer protocol known as SFTP. Using this method, you can exchange large files safely as it is a secure protocol that encrypts your data for protection.
To see how you can set up a ProFTPD Server on Linux Mint 20, visit: