×


Using SFTP Protocol to Transfer Files Between Local and Remote Machines

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).

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.

Here at Ibmi Media, we shall look into how you can easily exchange files between local and remote machines using this super-handy file transfer protocol.

Here, we will use Linux Mint Cinamon 20.02 as our local machine and Ubuntu 20.04 LTS as the remote machine.

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.

To see how you can set up a ProFTPD Server on Linux Mint 20, visit: https://linuxapt.com/blog/1032-install-proftpd-on-linux-mint-20


a. Steps to Transfer file from a remote machine to a local machine

1. SSH to Remote Machine

To begin, make an SSH connection to your remote machine. To do that, first, become a superuser using the following command:

$ sudo -i

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

$ ssh user@192.168.10.23


2. Locate the file to transfer

Now, locate the file you want to copy. In this tutorial, We 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

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

$ touch transfer.txt


3. Establish an SFTP connection with the remote machine

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

$ sftp user@192.168.10.23

If you see sftp written like this:

sftp> pwd
Remote working directory: /home

This means you are in sftp mode. Now the local and remote machines can exchange files using this protocol.


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

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

lpwd

We can also change the directory we want to interact with. It is included in the second part of this guide.


5. Transfer the file

Navigate to the /etc directory where we created the transfer.txt file:

$ cd /etc

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

$ get transfer.txt
Fetching /etc/transfer.txt to transfer.txt

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

$ cd ~

Use ls to see all the files in the directory:

$ ls

You will now see that 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.


b. Transfer file from a local machine to a remote machine

We have another file test.txt on our local machine in the following directory path:

$ /home/linuxAPT/Downloads/folder

From here we will transfer it to our remote machine.

1. Connect to the remote machine using SFTP:

$ sftp user@192.168.10.23


2. Change the local directory sftp interacts with

The local present working directory is:

$ lpwd
Local working directory: /root

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

$ lcd /home/linuxAPT/Downloads/folder

Before this, sftp was interacting with the local machine’s root directory. Now it will interact with the /home/linuxAPT/Downloads/folder directory. You can confirm it with the following command:

$ lpwd
working directory: /home/linuxAPT/Downloads/folder


4. Transfer the file

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

$ put test.txt
uploading test.txt to /home

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

$ ls

Now the file we transferred is here in the remote machine.

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

$ bye


[Need to fix SFTP Connection Linux issues ? We can help you. ]


Conclusion

This article covers how you can transfer files between a remote machine and a local machine with the help of a file transfer protocol known as SFTP. In fact, SFTP has the ability to leverage a secure connection to transfer files and traverse the filesystem on both the local and remote system.