How To Use SFTP to Securely Transfer Files with a Remote Server

Published on March 13, 2024

Secure Shell (SSH) comes with Safe File Transfer Protocol (SFTP), which allows users to transfer files securely between remote systems. Once you establish the remote connection via SFTP, you can perform basic administrative tasks, including managing the files and directories between the remote systems.

This post explains how to use SFTP to securely transfer files with a remote server. We will start by establishing the remote connection and then proceed to some of the examples. Read on!

Establishing an SFTP Connection

To connect using SFTP, the local system and your remote server must be connected using a public SSH key pair. Moreover, you must have an active internet connection and the IP address of your remote system.

That said, use the syntax below to establish the connection.

Copy

Notice how the command is similar to establishing an SSH connection. If this is your first time connecting, you will be prompted to verify the public pair key. Ensure you have SSH running on your local system and on your server. You can use the systemctl command to list the running systems and verify that SSH is active.

Next, you must enter the password for the remote system.

Once your authentication succeeds, you will have accessed the remote server and can start transferring files.

Transferring Files via SFTP

With our connection established, the next step is to understand what commands are required to transfer files between the local system and the remote server.

Let’s quickly run the help command to view the available options that we can use.

Let’s create a few files and directories on our remote server, which we will transfer to our local system via SFTP.

On the SFTP window, you can check the files we’ve created using the ls command. Since we are connected to our remote server, we get an output of the files we created earlier. The pwd command also confirms that we are in the home directory of our remote server.

1. Transferring Files via SFTP

SFTP offers the get command to retrieve a file from the remote server and save it in the current directory on your local system. The get command follows the syntax below.

Copy

For our first example, we are transferring the ‘names.txt’ into our local system. You will get an output showing that the fetching has been done and that the transferred file uses the same name.

To verify that we’ve transferred our file using SFTP, run the lls command on the SFTP terminal to display all the transferred files on your local system.

The below output shows we have the “names.txt” file, which we transferred.

The default behavior of SFTP when transferring files is that it retains the same name. However, you can choose to change the name of the transferred file when saving it on your local system.

To do so, add the new name you want to use for your file after the file you are transferring. The syntax below will guide you.

Copy

We are fetching the “names.txt” from our remote server and naming it “retrieved.txt” on our local system.

Rechecking the existing files on our local system, we confirm that our fetched file has the new name we specified when transferring it.

2. Transferring Directories via SFTP

Similar to transferring files, transferring a directory from your remote server to a local system is possible. Earlier on, we created the Hosts directory on our remote server. To transfer it, we will use the get command but with the -r option for directory.

Here’s the syntax to follow.

Copy

You will get an output showing that the directory has been fetched and stored in your home directory on your local system.

Suppose you want to maintain the directory’s permissions even after transferring it. Adding the -P option will do the trick.

Our new command would be:

Copy

To verify that the directory has been successfully transferred, run the lls command to show all the files and directories on your local file system. The output below shows an entry showing that the directory is available.

3. Transferring Files to a Remote Server

You can transfer your local files to your remote server, but instead of using the get command, we will use the put command.

The command works similarly to the get command. For instance, we can transfer a local file, “remote.txt,” to our remote server with the command below.

Copy

To transfer a local directory, we will use the put command with the -r option, then specify the target local directory.

You will get an output showing the file has been uploaded and entered into your remote server.

The last step is to verify that the files and directories have been successfully transferred from our local system to our remote server. Running the ls command on our remote server confirms that we have all the files and directories we transferred using the put command on the SFTP terminal interface.

Conclusion

SFTP offers a secure way of transferring files between a remote server and a local system. This post has discussed SFTP and how to transfer files and directories. Moreover, we’ve presented practical examples, making it easy to understand how to use SFTP to transfer files with a remote server.

New to LinuxMeta? Get Started Now! 

Instantly Deploy Linux & Windows KVM VPS at a Cheap Price