scp with ssh key

How to Use SCP with PEM File (SSH Key)

SCP is a popular tool to copy files to and from remote servers to your local system. Generally, clients use password-based authentication before file transfer with remote servers. But some cloud-based services like AWS require you to perform SSH key-based authentication. This key is available as .pem or .ppk file. In this article, we will learn how to SCP with PEM file. It provides a more secure way to copy files to/from remote server.


How to Use SCP with PEM File (SSH Key)

Secure Shell (SSH) is a popular way to connect and communicate between client systems and remote servers, especially in Linux environments. By default, SCP (Secure Copy) command provides a secure way to copy files to remote server. Using SSH encrypted connections make it even more secure. Here are the steps for it.


1. Generate SSH Keys

You need ssh-keygen utility to generate SSH keys for authentication. Almost every Linux system has ssh-keygen tool installed by default. Open terminal and run the following command to create SSH keys for your system.

$ ssh-keygen -t rsa

You will be asked for a filename to name the keys and also an optional passphrase that you can enter as blank.

After you enter the required values, it will display the generated key’s randomart prints.


2. Transfer SSH Key to Remote Server

You can copy the SSH key to remote server with the following command.

$ ssh-copy-id USER@SERVER

You will be prompted for password for USER. Once you enter the password, it will automatically copy your public SSH key to the remote server. When you try to connect to remote server from your client system, the remote server will authenticate you using this public key which you have copied.


3 Use SCP with SSH Key

Here is the basic SCP command to copy file FILENAME to /home/USER/FILENAME at remote server.

$ scp -i ~/.ssh/id_rsa.pub FILENAME USER@SERVER:/home/USER/FILENAME

Replace FILENAME, USER, SERVER with the filename, username and server IP address or hostname as per your requirement.

If you have entered passphrase during SSH key generation, you will be asked for this password. If you have left it as blank, then the SCP command will authenticate user using the SSH key alone and transfer your files.

If you want to copy files from remote server to local system, just swap the source and destination paths in the above command.

$ scp -i ~/.ssh/id_rsa.pub USER@SERVER:/home/USER/FILENAME /home/USER/FILENAME

In this article, we have learnt how to use SCP with SSH keys in Linux. You can run these commands directly from terminal or from within shell script or cronjobs. You can also use these steps to copy files to cloud services such as AWS and Microsoft Azure’s virtual machines.

Also read:

How to Ignore SSL Certificate Errors in cURL
How to Get Creation Date for File & Directory in Linux
How to Delete Iptables Rules
How to Use Rsync with SSH Key
How to Run Python Scripts in Sequence

Leave a Reply

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