How to Transfer Files Securely Using SCP in Linux

Scp File Transfer Featured

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.

Configuring SSH

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:

# Debian/Ubuntu-based server
sudo apt install openssh-server
 
# Fedora/Centos
sudo dnf install openssh-server
Scp Dnf Install

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:

sudo firewall-cmd --add-service=ssh --permanent
sudo firewall-cmd --reload
Scp Firewall Cmd Add Service
Scp Firewall Cmd Reload

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:

# Debian/Ubuntu-based distro
sudo apt install openssh-client
 
# Fedora
sudo dnf install openssh-client

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.

ip a s
Scp Server Ip

In the output, look for the line starting with inet under eth0 or 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:

ssh user@192.168.122.201

Change the “user” to the actual username in the server.

Scp Ssh Test Client

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:

scp /PATH/TO/FILE USER@IP-ADDRESS:PATH/TO/DESIRED/DESTINATION

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:

scp backup.txz maketecheasier@192.168.122.201:~/backups/
Scp Test

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:

scp *.txz maketecheasier@192.168.122.201:~/backups/

To copy a file from the remote server to the local machine, just reverse the parameters:

scp maketecheasier@192.168.122.201:~/backups/backup.txz .

Notice the dot at the end of the command which means “the current directory,” as it does with the standard cp or mv commands. You could just as easily specify some other directory if you wanted to.

scp -r maketecheasier@192.168.122.201:~/backups/ backups-from-server/

And the same with wild cards:

scp maketecheasier@192.168.122.201:~/backups/*.txz .

To recursively copy a directory to a remote server, use the -r option:

scp -r backups/ maketecheasier@192.168.122.201:~/backups/

And to copy a recursively copy of a directory from the remote server to the local machine use:

scp -r scp -r maketecheasier@192.168.122.201:~/backups/ .

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.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox