Using SCP to Transfer Files with SSH keys Ubuntu: 3 Easy Steps
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 scp with ssh key to Transfer Files 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. SSH, which stands for secure shell, is a network protocol that you can use to transfer data between an SSH client and an SSH server in a secure channel.
Is SCP the same as SSH?
At this point, you may face the question of whether 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 key?
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 learn 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 for Using SCP to Transfer Files with SSH keys Ubuntu.
In this guide, we use Ubuntu 22.04 server to show the guide steps for Using SCP to Transfer Files with SSH keys Ubuntu.
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:

At this point, you can list your SSH key files with the following command:
ls
Then, you should see that you have two SSH key files (public and private):
**Output**
scp_server scp_server.pub
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.
ls
**Output**
scp_server scp_server.pem scp_server.pub
Step 2 – Copy the Ubuntu SSH Public Key to the Remote Server
At this point of Using SCP to Transfer Files with SSH keys Ubuntu, 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 scp_server.pub, 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:
**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:

Now that you have logged in successfully with the Pem file, you can start Using SCP to Transfer Files with SSH keys Ubuntu.
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 username@remote.server.com:/path/to/remote/destination
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.
Conclusion
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:
How To SSH into a Windows Machine
[SOLVED] SSH connection reset by peer error](https://bluehoster.info/kex-exchange-identification-connection-reset-by-peer/)
Alternative Solutions for Secure File Transfer
While SCP with SSH keys is a reliable method, here are two alternative solutions for secure file transfer on Ubuntu:
1. Using rsync
over SSH
rsync
is another powerful command-line utility for file transfer and synchronization. When used with SSH, it offers a secure and efficient way to transfer files, especially for incremental backups or synchronizing directories. One significant advantage of rsync
is that it only transfers the differences between files, reducing bandwidth usage and transfer time.
Explanation:
rsync
uses an algorithm to identify the parts of a file that have changed and only transfers those parts. This is particularly useful when dealing with large files where only small portions have been modified. By default, rsync
uses SSH for secure data transfer, leveraging the same authentication mechanisms (including SSH keys) as SCP.
Code Example:
To transfer a directory named my_directory
from your local machine to a remote server using rsync
over SSH, you would use the following command:
rsync -avz -e "ssh -i /path/to/your/private_key" my_directory/ username@remote-server-ip:/path/to/remote/destination/
-a
: Archive mode; preserves permissions, ownership, timestamps, etc.-v
: Verbose mode; provides detailed output.-z
: Compresses data during transfer.-e "ssh -i /path/to/your/private_key"
: Specifies that SSH should be used as the remote shell, and uses the specified private key for authentication. If you have configured your ssh agent, you can omit this and it will use the default key.my_directory/
: The directory to be transferred (the trailing slash is important to copy the contents of the directory, not the directory itself).username@remote-server-ip:/path/to/remote/destination/
: The destination on the remote server.
To transfer a directory named my_directory
from the remote server to your local machine using rsync
over SSH, you would use the following command:
rsync -avz -e "ssh -i /path/to/your/private_key" username@remote-server-ip:/path/to/remote/destination/my_directory/ ./
./
: The destination directory for the contents of my_directory on your local machine.
2. Using sftp
(Secure File Transfer Protocol)
sftp
is a subsystem of SSH that provides an interactive file transfer environment. It’s similar to FTP but operates over a secure SSH connection, offering encryption and authentication. sftp
is useful for interactive file management and transferring multiple files or directories. Most GUI-based FTP clients support sftp
natively.
Explanation:
sftp
provides a command-line interface where you can navigate remote directories, list files, upload, and download files. It uses the same authentication mechanisms as SSH, including password authentication or SSH keys. It is typically pre-installed on most Linux distributions, making it readily available.
Code Example:
- Connect to the remote server:
sftp -i /path/to/your/private_key username@remote-server-ip
- Navigate directories (remote and local):
cd remote_directory
: Change directory on the remote server.lcd local_directory
: Change directory on the local machine.
- Transfer files:
get remote_file local_file
: Download a file from the remote server to the local machine.put local_file remote_file
: Upload a file from the local machine to the remote server.mget remote_file1 remote_file2
: Download multiple files from the remote server to the local machine.mput local_file1 local_file2
: Upload multiple files from the local machine to the remote server.
- Example of downloading a file:
sftp> get remote_file.txt local_file.txt
Fetching /path/to/remote/remote_file.txt to local_file.txt
/path/to/remote/remote_file.txt 100% 12KB 1.2MB/s 00:00
- Terminate the session:
sftp> exit
Both rsync
and sftp
offer secure alternatives to SCP for file transfer on Ubuntu, providing flexibility and efficiency depending on the specific use case. They are also often pre-installed and easy to use with existing SSH key infrastructure.