When accessing a Linux server, you have a few options. If it’s a server with a user interface, you could use VNC, but the more common option is to log into a headless server with Secure Shell, or SSH. It’s an encrypted, secure protocol that gives you access to a remote system through an SSH client. However, you may just want to send some files to a server. For that, you can use a program called Secure Copy, or SCP, that runs over the stalwart 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 whatever remote system you want to access), you’ll need to install an SSH server. The most common one on Linux is the OpenSSH server. To install it, you can run one of the following commands:
Depending on your distro, you may need to allow SSH through some software firewalls. On Ubuntu, this problem is nonexistent, but on CentOS, you’ll also have to run the following commands:
At this point, you’ll need an SSH client. On most distros, you have the OpenSSH client installed. But, if you don’t, install it with the following command:
Connecting to Your System Via SSH
Let’s make sure SSH works before trying to mess around with SCP. Before you can connect via SSH, you need to discover the IP address of the server. On graphical servers, the IP address is shown in the Network applet in System Settings. On most servers, you should use the
ip command on the terminal.
In the output, look for the line starting with
enp1s0, depending how your network interface is connected to the system. In my case, it’s 192.168.122.201.
To test the SSH connection, move to the Linux client machine and type:
Change the “user” to the actual username in the server.
Enter that account’s password, and you’re in business. If you get a question about the “authenticity of host can’t be established,” just answer “yes” to the question. It is a security check designed to make sure that you are connecting to your actual server and not an impostor. You should see the same prompt come up on your client system that you see when logging directly into the server, and that means that 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 to copy files between the two machines. Secure copying is achieved using the
scp command. The basic format of the
scp command is:
For example, to copy the file “backup.txz” from the local machine to the “backups” folder in the home directory of user “maketecheasier” on the remote server with the IP address of 192.168.1.101, use:
Similar to when you connect using
ssh, you will be prompted for the password. You won’t be prompted for the username, as that was specified in the command.
You can also use wild cards like the following:
To copy a file from the remote server to the local machine, just reverse the parameters:
Notice the dot at the end of the command which means “the current directory,” as it does with the standard
mv commands. You could just as easily specify some other directory if you wanted to.
And the same with wild cards:
To recursively copy a directory to a remote server, use the
And to copy a recursively copy of a directory from the remote server to the local machine use:
Other SCP Commands to Try
To progress further, try experimenting with the
-C option, which enables compression during the copy or the
-l option, which limits the bandwidth during the copy.
To transfer a file without hyving to type in a password everytime, you can also generate a private SSH key to connect to your server.