How to Securely Transfer Files and Directories on Linux

Knowing how to transfer files securely between Linux hosts is a useful skill to have if you are regularly working with Linux servers. You may just need to transfer a handful of files, or you may want to look at backing up files from one Linux host to another. What ever the reason, there are a number of ways you can transfer files securely on Linux. Continue reading to find out about some of the more common or popular ways to transfer files.

How to Transfer Files using SFTP between Linux Hosts

The sftp (Secure File Transfer Protocol) allows you to transfer files between Linux hosts securely because the traffic is encrypted. The sftp command can be found on just about any Linux distribution, which makes it a great option. You can check that it’s available, and it’s location by running the following command:

$ which sftp
/usr/bin/sftp

To connect to a remote linux host using SFTP, simply run the SFTP command, whilst specifying the IP address of the target host. For example:

$ sftp 172.31.35.73

The first time you connect, as with any SSH connection, you will be prompted to add the target host to your list of trusted hosts on your system. This will only happen the first time you connect. If all goes well you should see the sftp> prompt.

$ sftp 172.31.35.73
The authenticity of host '172.31.35.73 (172.31.35.73)' can't be established.
ECDSA key fingerprint is SHA256:RZPTEGRT/V3n60wLtFoahBLdBjN6ngeaoLULePyxFzjHf0.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '172.31.35.73' (ECDSA) to the list of known hosts.
Password:
Connected to 172.31.35.73.
sftp>

Ok – so now we have a valid connection, we can interact with the remote host. First of all we would likely want to do a directory listing. Just as when working on a local system, we can use the ls command:

sftp> ls
Desktop     Documents   Downloads   Music       Pictures    Public      Templates   Videos      backup.zip

If needed we can change directory using the cd command. But in this example I want to securely download the backup.zip file. To do so, I can use the get command:

sftp> get backup.zip
Fetching /home/cloud_user/backup.zip to backup.zip

This has now transferred the backup.zip file to my local system, into the local directory from where I first ran the sftp command. So, now we know how to download a file using SFTP, lets take a look at how to transfer a file in the other direction.

You can upload a file using sftp using the put command. This will allow you to upload a file from the directory where you ran the sftp command to the remote host. Lets take a look:

sftp> put file1.txt
Uploading file1.txt to /home/cloud_user/file1.txt
file1.txt                                                                                                                                                     100%    0     0.0KB/s   00:00

As you can see the file1.txt file uploaded successfully to the remote host. Now you should have a good idea of how you can use sftp to transfer files securely. There are other commands you can use within the sftp cli which I haven’t discussed here, you can bring up the inline help by typing ‘?‘, as shown below:

sftp> ?
Available commands:
bye                                Quit sftp
cd path                            Change remote directory to 'path'
chgrp [-h] grp path                Change group of file 'path' to 'grp'
chmod [-h] mode path               Change permissions of file 'path' to 'mode'
chown [-h] own path                Change owner of file 'path' to 'own'
df [-hi] [path]                    Display statistics for current directory or
                                   filesystem containing 'path'
exit                               Quit sftp
get [-afpR] remote [local]         Download file
help                               Display this help text
lcd path                           Change local directory to 'path'
lls [ls-options [path]]            Display local directory listing
lmkdir path                        Create local directory
ln [-s] oldpath newpath            Link remote file (-s for symlink)
lpwd                               Print local working directory
ls [-1afhlnrSt] [path]             Display remote directory listing
lumask umask                       Set local umask to 'umask'
mkdir path                         Create remote directory
progress                           Toggle display of progress meter
put [-afpR] local [remote]         Upload file
pwd                                Display remote working directory
quit                               Quit sftp
reget [-fpR] remote [local]        Resume download file
rename oldpath newpath             Rename remote file
reput [-fpR] local [remote]        Resume upload file
rm path                            Delete remote file
rmdir path                         Remove remote directory
symlink oldpath newpath            Symlink remote file
version                            Show SFTP version
!command                           Execute 'command' in local shell
!                                  Escape to local shell
?                                  Synonym for help

Now, we will move on to looking at another command you can use to transfer files securely on Linux, the scp command.

How to Transfer Files using SCP between Linux Hosts

SCP is another command that is generally available on all Linux distributions and tends to be my go to command for transferring files securely between Linux systems. The syntax is maybe a bit trickier to remember than with SFTP, but it’s certainly worth learning! A example of what a typical scp command would look like:

$ scp -r * admin@172.16.1.1:/files/

The above command would securely copy all files in the current directory (-r is the recursive option) to the /files directory on my remote Linux host (172.16.1.1). Alternatively, we could just have specified the parent directory name, to copy it and all the files and folders it contains to the remote host. For example:

$ scp -r mydirectory admin@172.16.1.1:/files/

If you wanted to transfer a single file you wouldn’t need the recursive option. Instead, just specify the source and destination files, as follows:

$ scp file1 user@172.16.1.1:file1

In the example above, because I haven’t specified a full path at the destination host, the file (file1) will be transferred to the ‘user’ home directory.

These examples have all been about transferring files from a local system to a remote system. SCP can also be used to transfer files between two remote systems. A command to do so would look something like this:

$ scp user@172.16.1.1:file user@172.16.1.2:file

The difference here than with the earlier examples is that we are also specifying a source host and user from which to copy the file, as well as the destination host.

In this article you have learnt how to transfer files and directories securely using the SFTP and SCP commands on Linux.

Related posts

Mastering the Linux ifconfig Command

Docker Exec Command With Practical Examples

Debugging with Git Bisect

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Read More