How to transfer files between local and remote machines using sftp protocol

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.

See also  How often do you apply security patches on Linux?

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 101

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 102

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 103

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 104

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 105

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 106

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 107

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

See also  Join Canonical and the financial services open-source community at OSSF NY
lpwd

How to transfer files between local and remote machines using sftp protocol 108

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 109

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 110

How to transfer files between local and remote machines using sftp protocol 111

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 112

Use ls to see all the files in the directory.

How to transfer files between local and remote machines using sftp protocol 113

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 114

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 115

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 116

Step 2: Change the local directory sftp interacts with

The local present working directory is:

See also  Charmed Kubeflow vs Kubeflow
lpwd

How to transfer files between local and remote machines using sftp protocol 117

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 118

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 119

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 120

How to transfer files between local and remote machines using sftp protocol 121

How to transfer files between local and remote machines using sftp protocol 122

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 123

How to transfer files between local and remote machines using sftp protocol 124

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 125

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.


Discover more from Ubuntu-Server.com

Subscribe to get the latest posts sent to your email.

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply