How to use scp command with SSH key file

Overview

Anyone who administers Linux machines likely knows secure shell. Without this tool, administering those servers remotely would be quite challenging. It would also become harder to move files back and forth, at least with a modicum of security. That’s where secure copy comes into play. With the SCP command, you can copy files to and from a remote Linux server through an encrypted SSH tunnel.

The scp (secure copy) command lets you securely transfer files between your local machine and a remote server over SSH.
When you authenticate using an SSH key instead of a password, you use the -i option to specify your private key file.

SSH keys

SSH keys are a secure, password‑less way to log into servers. They’re used with SSH (Secure Shell) to authenticate you without typing a password every time.

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

ssh -i YourFile-OpenSSH-Internal.pem ec2-user@3.142.90.222

How They Work (Quick & Simple)

SSH keys come in a pair:

1. Private Key

  • Stays on your computer
  • Never share it
  • Usually stored in ~/.ssh/id_rsa or id_ed25519

2. Public Key

  • Goes on the server
  • Stored in the server’s ~/.ssh/authorized_keys

When you connect, the server checks if your private key matches the public key.
If yes → you’re in. No password needed.

Generate a key (example)

ssh-keygen -t ed25519 -C "your_email@example.com"

This creates:

  • ~/.ssh/id_ed25519 → private key
  • ~/.ssh/id_ed25519.pub → public key

Key File Permission Requirements

SSH private keys must have restricted permissions:

chmod 600 ~/.ssh/id_rsa

If permissions are too open, you’ll see the following error:

Bad permissions: key file is too open

 

Basic Syntax

scp -i /path/to/private_key source destination
# Copy a local file to the remote destination
scp -i ~/.ssh/id_rda ./file.txt ec2-user@123.34.56.78:/home/ec2-user/data/

Some common usages

# Copy a remote file to the local machine
scp -i ~/.ssh/id_rsa user@remote-host:/home/user/file.txt ./

# Copy an entire directory recursively
scp -i ~/.ssh/id_rsa -r ./myfolder user@remote-host:/home/user/

# Specify a custom SSH port
scp -i ~/.ssh/id_rsa -P 2222 file.txt user@remote-host:/home/user/

Speed up transfers (optional)

# Use compression
scp -i ~/.ssh/id_rsa -C file.txt user@remote-host:/home/user/

# Increase performance for large transfers
scp -i ~/.ssh/id_rsa -o "Cipher aes128-ctr" file.txt user@remote-host:/path/

Tips & Alternatives

Use rsync for large or repeat transfers

rsync -avz -e "ssh -i ~/.ssh/id_rsa" ./folder user@remote-host:/path/

For Windows with PuTTY .ppk key

Convert the key using PuTTYgen → export to OpenSSH format.

References

[1] How To Use SCP (Secure Copy) With SSH Key Authentication, accessed on 26th Dec 2025

[2] Tutorial: Secure Copy (SCP) – Transfer Files Securely Over SSH, accessed on 26th Dec 2025

Nguyen Vu Ngoc Tung

I love making new professional acquaintances. Don't hesitate to contact me via nguyenvungoctung@gmail.com if you want to talk about information technology, education, and research on complex networks analysis (i.e., metabolic networks analysis), data analysis, and applications of graph theory. Specialties: researching and proposing innovative business approaches to organizations, evaluating and consulting about usability engineering, training and employee development, web technologies, software architecture.

https://www.itersdesktop.com/author/nvntung/

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.