I have an instance of an application running in the cloud on an Amazon EC2 instance, and I need to connect to it from my local Ubuntu. It works fine on one local ubuntu and also laptop. I got this message, Permission denied (publickey)., when trying to SSH to EC2 from a different local Ubuntu.

I'm thinking there may be problems with security settings on the Amazon EC2, which has limited IP access to one instance; or maybe a certificate needs to regenerate.

Does anyone know a solution to the Permission denied error?

  • 12
    "It used to work before" -- before what?
    – womble
    Jul 13, 2009 at 8:13
  • I have an Elastic Beanstalk EC2 instance. As at Aug-2013 the solution was to access the instance as the ec2-user user which made the Permission Denied (publicKey) error go away. Viz: ssh -i ./mike-key-pairoregon.pem [email protected]. Of course you have to all the other stuff as per stackoverflow.com/questions/4742478/…
    – mikemay
    Sep 1, 2013 at 7:09
  • 4
    You get this issue if you have the wrong user name specified. The aws docs (docs.aws.amazon.com/AWSEC2/latest/UserGuide/…) currently give an example with username ec2-user [ssh -i /path/my-key-pair.pem [email protected]], whereas my (old) ubuntu box has a username of ubuntu, so when I used the example I received this error, changing to the correct username resolves. Jul 12, 2016 at 16:21
  • @david.barkhuizen, your comment helped me. I had a similar problem; it turned out it had to do with the username. Thanks. Mar 23, 2018 at 9:05
  • If someone is here for Bitnami WordPress, then 'bitnami' is the username which you have to use for SSH connection
    – Seeker
    Jan 3, 2021 at 19:15

17 Answers 17


The first thing to do in this situation is to use the -v option to ssh, so you can see what types of authentication is tried and what the result is. Does that help enlighten the situation?

In your update to your question, you mention "on another local Ubuntu". Have you copied over the ssh private key to the other machine?

  • 2
    I've copied over the ssh private key to the other machine as @Greg suggested. It works now. Thanks! Jul 16, 2009 at 2:03
  • 3
    FYI you can use the -i flag to point to the path of the keys without installing them Nov 3, 2010 at 23:35
  • 21
    In my case, I was using a bitnami .ami and didn't realise that you need to log in as the user called bitnami, like: ssh -i <keyfile> bitname@<ec2-address>. Unfortunately the -v option didn't help me find this, but it's still very useful to check! Jan 20, 2012 at 4:44
  • 7
    well, in my case i was using the wrong username. was using "ubuntu" instead of "bitnami". like this: ssh -i key.pem bitnami@hostaddress Apr 15, 2012 at 21:44
  • 3
    A good lead is also the remote node itself, look into /var/log/auth.log, sometimes you will see the following messages: Authentication refused: bad ownership or modes for file /var/lib/jenkins/.ssh/authorized_keys or something else Jan 31, 2017 at 9:11

As it hasn't been explicitly mentioned, sshd is by default very strict on permissions on for the authorized_keys files. So, if authorized_keys is writable for anybody other than the user or can be made writable by anybody other than the user, it'll refuse to authenticate (unless sshd is configured with StrictModes no)

What I mean by "can be made writable" is that if any of the parent directories are writable for anybody other than the user, users permitted to modify those directories can start modifying permissions in such a way that they can modify/replace authorized_keys.

Furthermore, if the /home/username/.ssh directory is not owned by the user, and thus the user has no permissions to read the key you can run into problems:

drwxr-xr-x 7 jane jane 4096 Jan 22 02:10 /home/jane
drwx------ 2 root root 4096 Jan 22 03:28 /home/jane/.ssh

Note that jane does not own the .ssh file. Fix this via

chown -R jane:jane /home/jane/.ssh

These sorts of filesystem permission issues will not show up with ssh -v, and they won't even show up in the sshd logs (!) until you set the log level to DEBUG.

  • Edit /etc/ssh/sshd_config. You want a line that reads LogLevel DEBUG in there somewhere. Reload the SSH server using the mechanism provided by the distro. (service sshd reload on RHEL/CentOS/Scientific.) A graceful reload will not drop existing sessions.
  • Try authenticating again.
  • Work out where your auth facility logs go and read them. (IIRC, /var/log/auth.log on Debian-based distros; /var/log/secure on RHEL/CentOS/Scientific.)

Much easier to work out what's going wrong with the debug output which includes filesystem permission errors. Remember to revert the change to /etc/ssh/sshd_config when done!

  • 5
    That "can be made writable" bit is what got me
    – wmarbut
    Aug 14, 2012 at 3:20
  • 7
    FWIW the correct permissions for the key files are 600 (see here) Nov 28, 2012 at 4:22
  • 1
    Yep, my .authorized_keys file was writeable by group so it refused to accept.
    – Aditya M P
    Aug 7, 2013 at 8:45
  • 2
    I was beating my head against the wall! My user folder had the wrong permissions. Thank you!
    – XJones
    Jun 12, 2014 at 23:19
  • 4
    same goes for the ~/.ssh folder itself. you may get following error message: Authentication refused: bad ownership or modes for directory Jul 30, 2014 at 23:01

I received this error, because I forgot to add -l option. My local username was not the same as on the remote system.

This does not answer your question, but I got here looking for an answer to my problem.

  • 27
    ssh host -l user is the same as ssh user@host, right? Feb 1, 2011 at 14:08
  • 3
    @Znarkus yes, it is the same.
    – cregox
    Apr 19, 2011 at 16:53
  • Yup, this solved my problem causing the "Permission denied (publickey)" error as well. Jun 2, 2011 at 21:30
  • 1
    This was the problem for me. I was expecting user "root" to work, but I was using an Ubuntu EC2 image which had the default user "ubuntu".
    – Cerin
    Jun 14, 2013 at 19:54

I got this message on a new instance based off the Ubuntu AMI. I was using the -i option to provide the PEM but it was still showing the "Permission denied (publickey)".

My problem was that I wasn't using the correct user. By running the ssh with ubuntu@ec2... it worked like normal.

  • Yeah... I was running the command with sudo, which is why it wasn't working.
    – thaddeusmt
    Sep 17, 2011 at 12:56

Something that's easier to read than ssh -v (in my opinion of course), is tail -f /var/log/auth.log. That should be run on the server you are trying to connect to, while attempting to connect. It will show errors in plain text.

This helped me solve my issue:

User [username] from xx.yy.com not allowed because none of user's groups are listed in AllowGroups

  • this the server log. for RHEL/CentOS 7: tail -f /var/log/secure Jul 13, 2015 at 22:05

Check your /etc/ssh/sshd_config file. There, find the line which says

PasswordAuthentication no

That line needs to be modified to say yes instead of no. Also, restart the sshd server afterwards.

sudo /etc/init.d/ssh restart
  • 21
    That would make the server less secure. Feb 1, 2011 at 13:45
  • This was the problem I had: I wanted to set up an account for another user, authenticating with just a password. I also wanted to be able to log in as myself from places where I didn't have my private key.
    – Daniel
    Apr 10, 2011 at 22:28
  • 2
    How can we go to /etc/ssh/sshd_config - if we can't even get into the server ?
    – code-8
    Jun 27, 2015 at 12:50
  • To get into the server itself, you must use the PEM file that they gave out when you created the instance. The instructions go after that. Jun 29, 2015 at 18:27
  • This worked for me, although restarting sshd required the following command: sudo service sshd reload Oct 19, 2018 at 19:23

Perhaps not relevant to the current poster, but might help others who find this when searching for answers to similar situations. Instead of letting Amazon generate the ssh keypair, I recommend uploading your own, standard, default public ssh key to Amazon and specifying that when you run an EC2 instance.

This lets you drop the "-i" type syntax in ssh, use rsync with standard options, and also lets you use the same ssh key across all EC2 regions.

I wrote an article about this process here:

Uploading Personal ssh Keys to Amazon EC2

  • +1 Looked up this question exactly for this reason. May 30, 2013 at 18:36
  • i see this error in following your article. regions=$(ec2-describe-regions | cut -f2) Required option '-K, --private-key KEY' missing (-h for usage)
    – KashifAli
    Sep 1, 2013 at 14:01
  • @KashifAli You'll want to set up the EC2 API command line tool credentials so you don't always have to pass the credentials on every command line. Sep 5, 2013 at 7:14

Strangely, my problem turned out to be that the server had been restarted and it was issued a new DNS name. I was using the old DNS name. I know this sounds stupid but it took me a while to figure this out.

  • Thank you! This was exactly my problem. I didn't realize the DNS name changed when you restart an instance.
    – Tim Swast
    Jan 2, 2012 at 3:36
  • In my case, the *.compute.amazonaws.com URL changed when I assigned an elastic IP. Jan 26, 2014 at 22:14

If you're trying to connect to a CyanogenMod phone running Dropbear, you should run the following lines to make sure everything is all permission'd right:

chmod 600 /data/dropbear/.ssh/authorized_keys


chmod 700 /data/dropbear/.ssh/authorized_keys # In case of MacOS X 10.6-10.8


chmod 755 /data/dropbear/ /data/dropbear/.ssh

This fixed it for me, otherwise nothing can connect.

  • "when trying to access SSH to EC2 on another local Ubuntu." Oct 8, 2015 at 10:42
  • 1
    What if I don't have authorized_keys? Aug 18, 2016 at 9:00

If you're using CentOS 5, you may want to set StrictModes no in /etc/ssh/sshd_config. I'm sharing /home directory using NIS/NFS, and I set all the permissions correctly, but it always prompted me with the password. After I set StrictModes no, the problem disappeared!


Greg's answer explains how to trouble shoot it better, however the actual issue is that you have an ssh key set on one side of the transaction (the client), which is attempting public key authentication rather than password based authentication. As you don't have the corresponding public key on the EC2 instance, this won't work.

  • 2
    How to you resolve the issue? May 15, 2011 at 1:49

I had the same problem, and after trying tons of solutions which failed to work i opened the SSH port on my router's firewall (my router's firewall control panel is a mess, so it's hard to tell what's going on). Anyway, that fixed it :)

Super bloody annoying that the error you get is Permission Denied, implying that there was some kind of connection made, grr.


I was having the same problem even though I was supposedly following all the steps including

$ ec2-authorize default -p 22

However, I had started my instance in us-west-1 region. So the above command should also specify that.

$ ec2-authorize default -p 22 --region us-west-1

After this command I was able to ssh into the instance. I spent a little while before I realized the issue and hope this post helps others.


This is a rare case, but if you have selinux enabled and you are using nfs for directory with the authorized_keys (e.g. shared home directories), you'll need to either disable selinux (not recommended for security reasons, but you can temporarily disable it to see if this is causing the problem) or allow selinux to use nfs home directories. I'm not clear on the details, but this worked for me setsebool -P use_nfs_home_dirs 1


I've just experienced the same issue after inadvertently adding group write permissions to the users home directory.

I discovered this was the cause by running tail -f /var/log/secure on the machine and seeing the error Authentication refused: bad ownership or modes for directory /home/<username>.


TL;DR - make sure you use the correct username - ubuntu/ec2-user/etc.

I'm using a bastion-host (ec2) which is Amazon Linux OS, (ec2-user), and tried to from the bastion-host to a private-host (ec2) which is Ubuntu 18.04 OS (ubuntu)

This might sound funny since you should know the username of the machine you're SSHing to, but since I SSH to multiple machines, sometimes I make those mistakes.


If your .ssh dir has more than 1 key (ex: an id_rsa.pub, and an id_ed25519.pub), try ensuring you are executing ssh with the <correct_key_file>, by explicitly adding its path as an arg (and while you're at it, may as well explicitly pass <correct_username> too):

ssh -i ~/.ssh/<correct_key_file> <correct_username>@<host_server_address>

The defaults being used without explicitly specifying, may not be correct.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .