Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

scp to an EC2 instance fails with Permission denied (publickey)

Errorec2-user@ec2-203-0-113-25.compute-1.amazonaws.com: Permission denied (publickey).

Short answer

scp does not read your ssh habits. It needs the same key and user as ssh, passed on the command line. Most failures come from a missing -i key, the wrong user name for the AMI, or a key file that other users can read. Put every option before the source, then the key, user and host.

  • Applies to OpenSSH 9, Amazon Linux 2023, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Run the same login with ssh and the exact key and user you intend to give scp. If ssh fails too, the problem is the login, not the copy.

    ssh -i /path/key-pair-name.pem ec2-user@instance-public-dns-name
  2. 2Match what you see, then jump to its fix

    1. 1ssh works because it uses -i or a config entry, scp has neitherThe key is not passed to scpFix
    2. 2Permission denied (publickey) for a user that does not exist on that imageThe user name does not match the AMIFix
    3. 3A warning about an unprotected private key file before the denialThe private key file is readable by other usersFix

What does the error look like?

  • ec2-user@ec2-203-0-113-25.compute-1.amazonaws.com: Permission denied (publickey).
  • Unprotected private key file

Cause 1 of 3

The key is not passed to scp

1Check

scp takes the key with -i, exactly like ssh. Options go before the sources; the synopsis lists them first.

history | grep scp | tail -n 3

2Fix

Add -i with the full path of the .pem file. For a directory, add -r in the same options block, before the paths.

scp -i /path/key-pair-name.pem -r ./my-folder ec2-user@instance-public-dns-name:~/

3Verify

The files list as they copy, with no password prompt.

ssh -i /path/key-pair-name.pem ec2-user@instance-public-dns-name ls ~/my-folder

Confirmed by man.openbsd.org

Did this fix it?

Cause 2 of 3

The user name does not match the AMI

1Check

The default user depends on the AMI: ec2-user for Amazon Linux, ubuntu for Ubuntu, admin for Debian, rocky for Rocky Linux and so on.

ssh -i /path/key-pair-name.pem ubuntu@instance-public-dns-name

2Fix

Use the AMI's default user in the scp target, the part before the @.

scp -i /path/key-pair-name.pem -r ./my-folder ubuntu@instance-public-dns-name:~/

3Verify

The copy starts and ends without the publickey error.

ssh -i /path/key-pair-name.pem ubuntu@instance-public-dns-name 'ls ~'

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 3 of 3

The private key file is readable by other users

1Check

For SSH connections only you may read the private key. If you do not set this, you cannot connect with that key pair.

ls -l /path/key-pair-name.pem

2Fix

Restrict the key to your user.

chmod 400 /path/key-pair-name.pem

3Verify

Repeat the scp command; the warning is gone and the login works.

scp -i /path/key-pair-name.pem ./my-file.txt ec2-user@instance-public-dns-name:~/

Confirmed by docs.aws.amazon.com

Did this fix it?

How do I stop it coming back?

  • Add the host, user and IdentityFile to ~/.ssh/config so ssh and scp share one setup.
  • Keep one key file per instance group and chmod 400 it on first download.
  • Use rsync over ssh for large or repeated copies; AWS names it faster than scp for that.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Related fixes

Servers

dnf cannot install MySQL on Amazon Linux 2023

dnf cannot find a mysql-server package on Amazon Linux 2023

Amazon Linux 2023 documents MariaDB, not MySQL, as its database server, with packages named mariadb105 and mariadb105-server.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.