scp to an EC2 instance fails with Permission denied (publickey)
ec2-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.
Fix it fast
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-name2Match what you see, then jump to its fix
- 1
ssh works because it uses -i or a config entry, scp has neitherThe key is not passed to scpFix - 2
Permission denied (publickey) for a user that does not exist on that imageThe user name does not match the AMIFix - 3
A warning about an unprotected private key file before the denialThe private key file is readable by other usersFix
- 1
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 32Fix
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-folderConfirmed by man.openbsd.org
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-name2Fix
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
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.pem2Fix
Restrict the key to your user.
chmod 400 /path/key-pair-name.pem3Verify
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
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
Security
ssh ignores your EC2 .pem file because its permissions are too open
Permissions 0644 for 'my-key.pem' are too open.
OpenSSH refuses a private key that other users can read or write, then falls back to Permission denied (publickey).
3 causes, each with a fixServers
SSH to EC2 fails: refused key, or told to log in as ec2-user not root
Server refused our key
Almost always the instance is fine and the login is not.
4 causes, each with a fixServers
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 fixServers
Ansible hangs, then times out waiting for the sudo prompt
a playbook using become seems to hang
Ansible ran sudo on the target and never saw the answer it expected.
3 causes, each with a fixServers
Apache on Ubuntu returns 404 Not Found for my site
The requested URL was not found on this server.
On Ubuntu a 404 from Apache usually means the request maps to a path that has no file.
3 causes, each with a fixServers
Apache returns 504 Gateway Timeout when PHP takes too long
Quick requests work, slow ones end in a 504 after a fixed wait
A 504 from Apache means the backend did not answer within the proxy timeout.
2 causes, each with a fix