When accessing a Linux server, you have a few options. If it is a server with a UI, you can use VNC, but the most common option is to connect to a headless server with Secure Shell or SSH. It is an encrypted and secure protocol that gives you access to a remote system through an SSH client. However, you might just want to send some files to a server. You can do this by using a program called Secure Copy, or SCP, which runs over robust SSH protocol to quickly transfer files over your network to a remote system. Here we show you how to transfer files securely using SCP in Linux.
On your server (or any other remote system you want to access), you will need to install an SSH server. The most common server in Linux is the OpenSSH server. To install it, you can run one of the following commands:
Depending on your distribution, you may need to allow SSH through some software firewalls. On Ubuntu this problem is nonexistent, but on CentOS you will also need to run the following commands:
At this point you will need an SSH client. On most distributions, the OpenSSH client is installed. But if you don’t, install it with the following command:
Connecting to your system via SSH
Let’s make sure SSH is working before we try to play with SCP. Before you can connect using SSH, you need to find out the server’s IP address. On graphics servers, the IP address is displayed in the Network applet in the system settings. On most servers, you should use the
ip command on the terminal.
In the output, find the line starting with
enp1s0, depending on how your network interface is connected to the system. In my case, it’s 192.168.122.201.
To test the SSH connection, go to the Linux client machine and type:
Replace “user” with the actual user name on the server.
Enter the password for this account and you are in business. If you receive a question about “the authenticity of the host cannot be established”, simply answer “yes” to the question. This is a security check designed to ensure that you are connecting to your real server and not to an impostor. You should see the same prompt appear on your client system that you see when you connect directly to the server, which means your connection was successful. You should also configure your SSH connections for maximum security before proceeding to the next step.
Using SCP to transfer files
Now that you have tested the SSH connection, you can start copying files between the two machines. The secure copy is performed using the
scp order. The basic format of
scp the command is:
For example, to copy the “backup.txz” file from the local machine to the “backups” folder in the home directory of the “maketecheasier” user on the remote server with the IP address 192.168.1.101, use:
Similar to when you log in using
ssh, you will be prompted to enter the password. You will not be prompted for the username, as specified in the command.
You can also use wildcard characters like the following:
To copy a file from the remote server to the local machine, just reverse the settings:
Note the period at the end of the command which means “the current directory”, as it does with the standard
mv orders. You can just as easily specify another directory if you wish.
And the same with the jokers:
To recursively copy a directory to a remote server, use the
And to copy a recursive copy of a directory from the remote server to the local machine, use:
Other SCP commands to try
To progress further, try to experience the
-C , which allows compression when copying or
-l option, which limits the bandwidth when copying.
To transfer a file without having to type a password each time, you can also generate a private SSH key to connect to your server.
Is this article useful?