Blog Post
How to Securely Transfer Files on Linux Using the SFTP Command
Commands

How to Securely Transfer Files on Linux Using the SFTP Command

File Transfer Protocol is an easy way of accessing and transferring files or programs over a network. Better known as FTP, this protocol allows users to access various types of data from a remote computer or server. Although clients can sometimes connect anonymously to a host using FTP, the connection is generally not considered to be entirely safe. That’s where SFTP comes in.

SFTP is another type of protocol that works just like the standard FTP but comes with the additional benefit of offering a secure connection. This is thanks to the fact that SFTP comes bundled with another network protocol known as Secure Shell or simply SSH. SFTP was designed specifically to provide a secure channel for services using an unsecured network and should be used instead of the regular FTP whenever possible.

A Couple of Things to Note About SFTP Connections

Making use of this secure protocol on Linux is very easy thanks to the sftp command. For obvious reasons, you’re going to need write permission from the remote system you want to access before you can transfer any files using this command.

Another thing to note is that you should run the command inside a tmux or GNU Screen terminal whenever you’re transferring large files. Using one of these utilities will allow you to easily resume your session should your connection drop during the transfer.

SFTP makes use of the SSH protocol for creating a connection, which means that it also uses the same authentication methods. By default, users can use passwords to connect to the remote server, however, this method is not recommended. Using traditional passwords means that your connection is not entirely secure even when using SFTP.

Instead, it is strongly recommended that you set up SSH keys when connecting via SFTP. Your public key will first need to be transferred to the remote system before you can connect to it. However, doing so will allow you to set up an SFTP login that doesn’t require any passwords. Not only is this method more secure but it also saves you some time in the future as you will no longer need to input a password every time you establish a connection.

How to Establish an SFTP Connection

In order to establish an SFTP connection start by typing the sftp command in the terminal followed by your remote username and the IP address or domain name you want to connect to. The command will look something like this:

$ sftp remote_username@server_ip_or_hostname

If you decided against creating SSH keys and you’re using a traditional password, you will now be prompted to input said password in order to connect. If the password is correct, the remote system will confirm that the connection has been established and the standard prompt will change to sftp>.

If you’re not using the standard port 22 or if the remote system is not listening to it, you can specify a different port by typing in the following command:

sftp > –oPort=custom_port remote_username@server_ip_or_hostname

Useful Commands

You should be familiar with many of the commands recognized by the SFTP prompt as they are identical to the ones you can use in the regular Linux shell prompt. This includes help, which is by far one of the most useful commands you should be aware of. Calling this command in the SFTP prompt is as easy as typing help or ?.

Upon doing so, you will be presented with a list of other available commands along with their purpose. For example, bye or exit can be used to quit the SFTP prompt while cd path will change the remote directory to ‘path’.

Navigating Using SFTP

Once a remote connection has been established, you will initially find yourself in the root directory of the server you just accessed. The exact name of said directory can be checked by typing in the following command:

sftp> pwd

The prompt output will then return:

Remote working directory:  /home/remote_username

Viewing the files and folders contained inside the working directory requires the use of another command you might be familiar with:

sftp> ls

To move to a different directory you can use the following command:

sftp>  cd /exampleDirectory

If you would like to print the contents of the directory you are currently in you can use:

sftp> cd lpwd

Meanwhile, if you just want to list the contents of the directory you can use the command:

sftp> cd lls

Downloading Files Using SFTP

The primary reason for using a protocol like SFTP is to transfer and download files from a remote host to your local machine. In many situations, you can use a desktop client like Filezilla to make things easier but that might not always be an option. There are scenarios when you’re working with a server that doesn’t feature a graphical user interface. This is when the sftp command will come in handy.

In order to download a particular file from the remote server to your local system you’re going to need to use the following command:

sftp> get exampleFile

Upon typing this command you may notice that the remote file will be downloaded on your local system under its original name. So exampleFile will remain exampleFIle once the transfer is complete. However, you can change that and rename the transferred file to something else using the following command:

sftp> get exampleFile local_exampleFile

If instead of a single file you would like to download an entire directory you’ll need to use the recursive flag –r alongside the get command. For example:

sftp> get –r exampleDirectory

If your connection drops or the file transfer is interrupted for any other reason, you can resume it by simply using the reget command. In this case, simply replace get with reget while typing the command:

sftp> reget exampleFile

Uploading Files Using SFTP

SFTP can be used for uploading files just as easily as it can be used for downloading them. If the file you want to transfer is found in your local working directory, use the following command:

sftp> put exampleFile

The flag mentioned earlier works just as well for instances when you want to upload an entire directory instead of a single file. For example:

sftp> -r put exampleDirectory

Meanwhile, resuming an interrupted upload also works similarly and the command will look something like this:

sftp> reput exampleFile

File Manipulations Using SFTP

Being able to check how much disk space is left on the remote server is very useful when uploading and downloading files, especially large ones. To check the remaining disk space as well as display other useful statistics you can use the command:

sftp> df

If you want to change the owner of a particular file you can do so with the following command:

sftp> chown userID file

Meanwhile, if you only want to change the permissions of a file you can use:

sftp> chmod 644 file_name

You can create a new directory on the remote server with the command:

sftp> mkdir directory_name

And you can delete a directory from the remote server with:

sftp> rmdir directory_name

To delete a file use the following command:

sftp> rm file_name

Or if you want to simply rename a file on the remote server use the following:

sftp> rename file_name new_file_name

Once you’re done uploading, downloading, or manipulating all the files you wanted you can close the connection to the remote server by typing exit or bye.

Final Thoughts

Learning how to efficiently use SFTP in Linux will make your job a lot easier when transferring files to or from a remote server. Regular FTP is also a viable option as it uses very similar commands to the ones discussed above, however, it might not be the best solution in many situations.

The primary reason why you would want to use SFTP is security, which is something other protocols lack. Another benefit of SFTP is the ease of use because by setting up SSH keys you can connect to a remote server without having to type in any passwords. Therefore, you avoid exposing yourself to unnecessary security risks in the process.

Related posts

Leave a Reply

Required fields are marked *

Copyright © 2020 Blackdown.org. All rights reserved.