How to Transfer Files Between Local and Remote Machines Using SFTP Protocol 2

How to Transfer Files Between Local and Remote Machines Using SFTP Protocol

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.

Installation Guide

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:

sudo -i

How to Transfer Files Between Local and Remote Machines Using SFTP Protocol 3

Now that you have become a superuser, ssh to the remote machine you want to communicate with to get the file.

ssh nginx@192.168.10.23

How to Transfer Files Between Local and Remote Machines Using SFTP Protocol 4

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:

cd /etc

How to Transfer Files Between Local and Remote Machines Using SFTP Protocol 5

With the help of the touch command, I will create a new file with the name transfer.txt.

touch transfer.txt

How to Transfer Files Between Local and Remote Machines Using SFTP Protocol 6

Step 3: Establish an SFTP connection with the remote machine

Now, we will connect to the remote machine using sftp, as shown below:

sftp nginx@192.168.10.23

How to Transfer Files Between Local and Remote Machines Using SFTP Protocol 7

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.

How to Transfer Files Between Local and Remote Machines Using SFTP Protocol 8

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:

pwd

How to Transfer Files Between Local and Remote Machines Using SFTP Protocol 9

To see the local machine’s present working directory, we’ll use:

lpwd

How to Transfer Files Between Local and Remote Machines Using SFTP Protocol 10

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.

cd /etc

How to Transfer Files Between Local and Remote Machines Using SFTP Protocol 11

To send a file from the remote machine to the local machine, we use the get command followed by the filename as shown below:

get transfer.txt

How to Transfer Files Between Local and Remote Machines Using SFTP Protocol 12

How to Transfer Files Between Local and Remote Machines Using SFTP Protocol 13

Now got to your local system’s root directory with the following command:

cd ~

How to Transfer Files Between Local and Remote Machines Using SFTP Protocol 14

Use ls to see all the files in the directory.

How to Transfer Files Between Local and Remote Machines Using SFTP Protocol 15

As you can see, the transfer.txt file is now in our local machine.

How to Transfer Files Between Local and Remote Machines Using SFTP Protocol 16

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:

/home/azifa/Downloads/folder

From here we will transfer it to our remote machine.

How to Transfer Files Between Local and Remote Machines Using SFTP Protocol 17

Step 1: Connect to the remote machine using SFTP

sftp nginx@192.168.10.23

How to Transfer Files Between Local and Remote Machines Using SFTP Protocol 18

Step 2: Change the local directory sftp interacts with

The local present working directory is:

lpwd

How to Transfer Files Between Local and Remote Machines Using SFTP Protocol 19

As I told you earlier, we can change the local directory we want sftp to interact with. To do that, use the lcd command:

 lcd /home/azifa/Downloads/folder

How to Transfer Files Between Local and Remote Machines Using SFTP Protocol 20

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:

lpwd

How to Transfer Files Between Local and Remote Machines Using SFTP Protocol 21

Step 4: Transfer the file

Next, we use the put command to transfer the file from the local machine to the remote machine.

How to Transfer Files Between Local and Remote Machines Using SFTP Protocol 22

How to Transfer Files Between Local and Remote Machines Using SFTP Protocol 23

How to Transfer Files Between Local and Remote Machines Using SFTP Protocol 24

Run the ls command to see if the file has been successfully transferred to the /home directory of the remote machine.

ls

How to Transfer Files Between Local and Remote Machines Using SFTP Protocol 25

How to Transfer Files Between Local and Remote Machines Using SFTP Protocol 26

Yes, the file we transferred is here in the remote machine.

Now you can end the session with a simple bye command.

bye

How to Transfer Files Between Local and Remote Machines Using SFTP Protocol 27

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:

https://linuxways.net/mint/install-proftpd-on-linux-mint-20/

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications including CCNA RS, SCP, and ACE. As an IT engineer and technical author, he writes for various websites.

Leave a Comment

Your email address will not be published. Required fields are marked *