As a Linux system administrator, there are many different ways to transfer files, securely or not, between two different hosts.
During your day job, you may be asked to perform some big transfers between two distant servers.
You may have to backup an entire database on a secure share drive, or you may simply want to get a remote file to your system.
As a consequence, you want to transfer files, but you want to do it in a very secure and safe way.
Luckily for you, there are many different commands that you can use in order to achieve that.
In this tutorial, we are going to see all the different ways to transfer files between hosts on Linux using either ftp, sftp, scp or rsync.
Ready?
Here are all the ways to transfer files on Linux :
Transferring files on Linux using ftp
The first way to transfer files on Linux is to use the ftp command.
By default, the FTP protocol uses the TCP protocol which is unsecured.
It means that if someone were to track data or packets send over the network, it would be able to see what you are sending to remote hosts.
In most cases, this is not something that you want for your file transfers, especially when they are done over Internet.
However, we are still going to provide the syntax for the ftp command as it can be useful to use it for simplicity over local networks.
By default, your distribution may not have the ftp command installed by default.
To check if ftp is installed on your host, you can run the following command
$ command -v ftp
If no outputs are provided to you, it means that the ftp is not available on your system.
Installing ftp on Debian-based distributions
In order to install it on Debian-based distributions, run the following command
In order to install new packages, you need to have sudo privileges on your Debian distribution
$ sudo apt-get install ftp
After the installation, run the “command” command again to make sure that it is correctly available on your system.
$ command -v ftp
/usr/bin/ftp
Installing ftp on Red Hat based distributions
In order to install ftp on Red Hat based distributions, you have to run the following commands
In order to install new packages on RHEL or CentOS, you need to have sudo privileges on your CentOS distribution.
$ sudo yum install ftp
Again, run the “command” command in order to check that your packages were correctly installed
$ command -v ftp
/usr/bin/ftp
Connect to remote hosts with ftp
In order to connect to remote hosts using the ftp command, you have to use the following syntax
$ ftp <destination_ip>
When connecting, this is what you should see if you are connecting to a non-protected FTP server.
Type the username you were assigned to connect to this FTP server.
You will be prompted to enter your password. If the credentials are correct, you should be able to login right into your FTP server.
Downloading files on Linux using ftp
When you are connected to your FTP server, there are many ways for you to interact with it.
First of all, hit “?” in order to see all the commands available to you.
As you can see, the commands available are pretty much identical to the ones you would find on a standard Linux system.
By default, on your FTP server, you will be given a FTP shell access right into your home directory.
In order to see all files available to you in the current directory, run the following command
$ ls
As you can see here, I am listing the entries of my home directory.
In order to navigate in one of the folders, hit the standard “cd” command with the path you want to navigate to.
To download a file from the current directory, you have to run the following command
$ get <file>
The local will be copied to your current working directory on the local machine, meaning the path from where you launched the ftp command in the first place.
As a side note, if you want to download multiple files from your remote hosts, you have to use “mget” command with the following syntax
$ mget <file1> <file2> ... <filen>
Congratulations!
You successfully transferred a file from your distant machine to your local machine using FTP.
Uploading files on Linux using ftp
With FTP, you can download files from your distant hosts but you are also able to upload files to your remote hosts.
To upload files using ftp, you have to use the “put” command with the following syntax
$ put <file>
Your file should now be accessible on your remote host, in the home directory of the user you are using to transfer your files.
$ ls /home/<user>
file.txt
Similarly, if you want to upload multiple files to your remote hosts, you can use “mput” this way
$ mput <file1> <file2> ... <filen>
Congratulations!
You successfully transferred files from your local host to your remote host using ftp.
Transferring files using sftp on Linux
A more secure way to transfer files between two hosts is to use the sftp command.
The sftp command, that stands for “Secure File Transfer Protocol“, is a way to transfer files between two hosts with encrypted traffic.
As a consequence, even if someone catches the traffic between you and the remote host, it won’t be able to decrypt it without having access to your SSH keys.
When using the sftp command, the host you are trying to download or upload files to has to be equipped with a SSH server.
If you are using an OpenSSH server on your remote host, you will be able to connect to it in order to perform file transfers.
By default, sftp should be available on your system, but you can verify it by running the following command.
$ command -v sftp
/usr/bin/sftp
Connect to remote hosts using sftp
In order to connect to remote hosts using sftp, you have to use the following syntax
$ sftp <destination_ip>
By default, if this is the first time that you are connecting to a remote host, you will be asked to add the host to the list of trusted hosts of your system (located at /home/user/.ssh/known_hosts by default)
Now that you are connected on your remote SSH server, the syntax in order to navigate or transfer files is the same as the standard ftp command.
Downloading files on Linux using sftp
First of all, you can hit “?” in order to see all the commands available to you on the SSH server.
First of all, hit the “ls” command to see all files and folders available to you on the remote system.
$ ls
As you can see, this command will list the entire folder I am currently sitting into (which is the home directory in this case).
To navigate to a folder, hit the “cd” command that would normally do on a standard FTP server.
$ cd <path>
To download a file on a SFTP server, use the “get” command using this syntax
$ get <file>
The file will be copied from the current remote directory to the local directory you launched the sftp command on.
Congratulations! You correctly transferred files from your remote host to your local machine.
Uploading files on Linux using sftp
Alternatively, you can transfer files between your local machine to your remote host.
To transfer files using sftp, you have to use the “put” command.
$ put <file>
If you list the entries of your remote directory, you should be able to see your file.
$ sftp> ls -l
Congratulations!
You successfully transferred files from your local host into your remote machine.
Transferring files on Linux using scp
One of the other ways to transfer files between two hosts on Linux is to use the scp command.
$ scp <option> <source_user>@<source_host>:<file> <destination_user>@<destination_host>:<file>
The scp command establishes a secure connection between the two hosts and it uses the standard SSH port in order to transfer files.
As a consequence, you are going to need to have a SSH server standing on your destination host for the file to be transferred.
For example, to transfer a local file from your current machine to a remote host, you would run the following command
$ scp file [email protected]:file
By default, if you don’t specify an absolute path, the files will be copied directly to the home directory of the user you are connecting with.
Now if I check the files on my remote host, the file should sit in the user home directory.
Transferring directories on Linux using scp
With the scp command, it is also possible to transfer entire directories to the destination host.
In order to copy directories, you need to specify the “-r” option.
Let’s take the example of a folder named “directory” with 3 files in it. To copy them, you have to use the following command
$ scp -r directory [email protected]:.
If you inspect the directories on your destination host, you should be able to see your transferred directory.
Note that if you specify a custom directory on the target, it will be created if it does not exist by default.
Transferring files between two remote hosts
In the previous example, we used a local host with a distant host that we contacted using its IP address.
Now, you may have to transfer files between two remote hosts located at two different IP addresses.
In order to transfer files between those two hosts, you are going to execute the following command
$ scp <option> <source_user>@<source_host>:<file> <destination_user>@<destination_host>:<file>
For example, it would lead to
$ scp [email protected]:file [email protected]:file
Note that in this case, you are going to need two SSH servers on both instances, otherwise it won’t work.
Using scp with a progress bar
In order to display statistics for the scp process, you need to specify the “-v” option (that stands for verbose)
$ scp -v <source_user>@<source_host>:<file> <destination_user>@<destination_host>:<file>
Transferring files on Linux using rsync
Finally, the last way to transfer files between two hosts on Linux is to use the rsync command.
The rsync command also uses a secure protocol as it relies on the SSH protocol for transferring data. It can be used in a wide variety of ways, but we are going to focus on file transfers for this tutorial.
The scp and rsync commands are very similar but rsync provides way more functionalities for file transfers. You can choose for example to include certain files while excluding others. You may also choose to keep permissions or hardlinks during a copy.
Overall, rsync is a superset of the scp command.
By default, the rsync command may not be installed on your system. In order to install it, follow those instructions.
Installing rsync on Debian-based distributions
In order to install rsync, you are going to be a sudo user on your host.
To install rsync, run the following command
$ sudo apt-get install rsync
Finally, run the command in order to make sure that it was installed properly.
$ rsync --help
Installing rsync on Red Hat based distributions
In order to install rsync on RHEL or on CentOS, execute the following command
$ sudo yum install rsync
Similarly, run the help option in order to make sure that it was installed correctly.
$ rsync --help
Using rsync to transfer files on Linux
Now that rsync is correctly installed on your host, you can use it in the following way
$ rsync <option> <source_user>@<source_host>:<file> <destination_user>@<destination_host>:<file>
For example, in order to transfer files from a local host to a remote machine, you would run the following command
$ rsync file [email protected]:file
Similarly, to copy files from your remote host to your local machine, you would run
$ rsync [email protected]:file file
Using rsync to transfer directories on Linux
With rsync, you can also transfer complete directories from a local host to a remote one.
You just have to append the “-r” option to your current syntax
$ rsync -r <source_user>@<source_host>:<file> <destination_user>@<destination_host>:<file>
When copying directories, there is a rule that you have to be aware of.
When specifying the source directory, you have the option to add a trailing slash at the end in order to copy the content of the directory and not the directory itself.
To illustrate it, let’s say that you have a directory named “directory”, that you want to transfer to a remote directory named “directory” on your remote machine.
Using a trailing syntax, this is what you would get on your remote host
$ rsync directory/ [email protected]:directory
On the remote host
/home/user/
├── directory
├── file1
├── file2
├── file3
However, if you write rsync without a trailing slash, i.e
$ rsync directory [email protected]:directory
On the remote host
/home/user/
├── directory
├── directory
├── file1
├── file2
├── file3
As a consequence, you may want to include trailing slashes by default when copying files from one host to another in order to avoid duplicate directory names.
Using rsync with progress bar
One very handy way to use rsync is to have a progress bar associated with it for long transfers.
To have a progress bar when using rsync, you have to use the “–progress” option (for human readable)
$ rsync --progress <source_user>@<source_host>:<file> <destination_user>@<destination_host>:<file>
Conclusion
In this tutorial, you learnt how you can easily transfer files on Linux using the ftp command, the sftp, scp or rsync commands.
You saw the differences between those commands and how they can be used in order to transfer complete directories.
1 comment
[…] : we already wrote a guide on transferring files and folders over the network, if you need an extensive guide about […]