Using SCP to Transfer Files with SSH keys Ubuntu

This tutorial intends to teach you Using SCP to Transfer Files with SSH keys Ubuntu. As you must know, every Linux Administrator uses a different utility to access and manage the remote server. The most popular utilities are SCP and SSH. Let’s get familiar with these utilities and use them to transfer files on Ubuntu.

What is SCP and SSH?

SCP which stands for secure copy protocol is a command line utility used for transferring files between servers in a secure mode. It is based on the SSH protocol. And SSH which stands for the secure shell is a network protocol that you can use to transfer data between SSH client and SSH server in a secure channel.

Is SCP the same as SSH?

At this point, you may face the question of whether the SCP and SSH are the same or not. The main difference between them is that SSH is used for logging into remote systems and controlling those systems while SCP is used for transferring files among remote computers in a network.

How to Use SCP with SSH keys?

You can use the SCP command to transfer files from a remote server with an encrypted SSH tunnel. Also, you can use SSH keys to make your transfer action more secure. To do this, you can follow the steps below to see how you can do it on the Ubuntu server.

Steps To Using SCP to Transfer Files with SSH keys Ubuntu

At this point, we want to use SCP to transfer files with SSH keys (PEM file). PEM file format is mostly used to store the cryptographic keys. Now you must have access to your Ubuntu server as a root user, and generate a Pem file, and follow the steps below to see how to use SCP to transfer the files.

In this guide, we use Ubuntu 22.04 server to show the guide steps.

Step 1 – Generate SSH Keys and a PEM File on Ubuntu

The first step is to generate the SSH keys on Ubuntu. To do this, you can run the command below:

ssh-keygen -t rsa -b 2048

When you run the above command, you will be asked to enter a file name for your key and set a passphrase for your file. Here we named our file scp_server, you can choose your desired name. When you are done, you will get the following output:

Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): scp_server
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in scp_server
Your public key has been saved in
The key fingerprint is:
SHA256:g+1BO/+9TCd2RyZpVL8mUchAN3HBGtkekDZXjjFAs6E root@...
The key's randomart image is:
+---[RSA 2048]----+
|          .+B*@+=|
|           ..%+@.|
|        . E o.O +|
|       + .   o.o.|
|      . S    .+oo|
|       . =   .o+ |
|        . .   + +|
|           . = +.|
|            . +. |

At this point, you can list your SSH key files with the following command:


Then, you should see that you can two SSH key files (public and private):


Next, you must generate a Pem file with your Private SSH key by using the following command:

cp -p scp_server scp_server.pem

Now when you list the files, you must see the pem file is generated too.

scp_server  scp_server.pem

Step 2 – Copy the Ubuntu SSH Public Key to the Remote Server

At this point, you must copy the public key to the server you want to access. To do this, you can use the following command:

ssh-copy-id -i public-key username@remote-server-ip

For example, our public key is, and remember to replace the username and IP with the remote server you want to access.

You will be asked to enter the remote server’s password. You should get the following output:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@...'"
and check to make sure that only the key(s) you wanted were added.

Then, in the Ubuntu local machine, set the correct permissions for the Pem file with the following command:

chmod 400 scp_server.pem

You can verify everything is working fine by using the command below:

ssh -i scp_server.pem username@remote-server-ip

Enter the passphrase of the SSH key file you have generated, then, you should see:

Enter passphrase for key 'scp_server.pem':
Welcome to Ubuntu 22.04.1 LTS (GNU/Linux 5.15.0-46-generic x86_64)

 * Documentation:
 * Management:
 * Support:

  System information as of Sat Sep 30 06:55:34 AM UTC 2023

  System load:           0.09423828125
  Usage of /:            7.7% of 48.63GB
  Memory usage:          5%

Now that you have logged in successfully with the Pem file, you can start using the SCP command to transfer the files.

Step 3 – Use the SCP command with SSH keys (Pem File) To Transfer Files

At this point, you can easily use the SCP command to transfer files with SSH keys on Ubuntu. You just want to specify the path to the Pem file:

scp -i /path/to/file.pem /path/to/local/file

For example, to copy a single file with a Pem file to a remote server, you can run the command below:

scp -i scp_server.pem test-file username@remote-server-ip:/home/username/

Also, you can copy an entire directory by using the -r option. For example:

scp -i scp_server.pem -r Test-Directory username@remote-server-ip:/home/username/

Next, if you want to transfer a file from the remote server to the local machine, you can use the following syntax:

scp <options> remote-server@ip-addres:/path-to-file <folder_local_system>

That’s it, you are done. With this option, you can easily transfer files securely.


At this point, you have learned a secure way to transfer files between a local and remote machine. You can easily generate a Pem file and use SCP with this file to transfer the data between servers. Hope you enjoy this guide on Using SCP to Transfer Files with SSH keys Ubuntu.

You may be interested in these articles:

Using the ssh-copy-id Command

Web-based SSH Client To Linux

How To SSH into a Windows Machine

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Your email address will not be published. Required fields are marked *

Stay informed and not overwhelmed, subscribe now!