3v-Hosting Blog

Transferring files using SSH in Linux


4 min read

When constantly working with Linux servers, there is often a need to transfer files or folders from one server to another, and the easiest way to do this is to transfer using SSH. Any self-respecting system administrator should have this simple method of transferring files in order to transfer the necessary files from server to server very quickly and without additional settings, if necessary.
But despite its simplicity, this tool is very powerful, allowing you to use various parameters when transferring.



Understanding SSH

Since in this method we will use SSH to transfer files, let’s repeat a little what it is. So, SSH is a secure and encrypted protocol designed for secure data transfer. In terms of file transfer, SSH allows users to securely move files between local and remote servers. This process is not only secure but also efficient, making it the preferred choice for many hosting professionals.
The basic command for connecting to a remote server via SSH is the command:

ssh username@your_server_ip


Replace 'username' with your server username and 'your_server_ip' with the actual IP address of your server. Now let's move on directly to file transfer. 



Basic SSH File Transfer Commands

Uploading Files (Local to Remote)

To upload a file from a local computer to a remote server, use the scp command in such a way that the first argument, immediately after the scp command, is the path to the local file, and the second is the path to the directory on the remote server where you want to upload the file:

scp path/to/local/file username@your_server_ip:/path/to/remote/directory

Replace 'path/to/local/file' with the file on your local machine and 'path/to/remote/directory' with the desired directory on the server.
Pay attention to the path to the remote directory. It consists of two parts separated by a colon. The first is to connect to the remote server, similar to the ssh command, and then, separated by a colon, specify the full path to the desired directory on the remote server, starting with the root directory (/).



Downloading Files (Remote to Local)

To download files from a remote server to the local computer, use the same scp command, but in this case the arguments are arranged in the opposite way, i.e. First, the first argument is the connection to the remote server and the path to the desired file, and the second argument is the path to the local directory where you want to download the file:

scp username@your_server_ip:/path/to/remote/file path/to/local/directory

This will transfer the specified file from the remote server to your local directory.




Advanced features for increased efficiency

Recursive transfers

To migrate entire directories and their contents, add the -r option to the scp command:

scp -r path/to/local/directory username@ip_of_your_server:/path/to/remote/directory


This ensures that all files and subdirectories are transferred.



Saving file attributes

Use the -p option to preserve file attributes such as timestamps:

scp -p path/to/local/file user_name@ip_of_your_server:/path/to/remote/directory


This preserves the file's original metadata during transfer.



Bandwidth Limit

Control bandwidth usage during transfer by specifying the -l option followed by the desired limit (in Kbps):

scp -l 500 path/to/local/file user_name@ip_of_your_server:/path/to/remote/directory


Set the limit according to your preferences and network conditions, so as not to interfere with the main activity of the server, for example.





Mastering file transfer over SSH in Linux is a valuable skill for any hosting professional. By using these simple commands and learning additional options from the official manual, you can optimize your file management processes, ensuring the security and efficiency of your hosting infrastructure.