Permission denied (publickey) with Git on Ubuntu

On December 28, 2010, in Git, by Anuj Gakhar

I’ve been working on a Ubuntu Server today (for a change) and had to clone a Git repository from via ssh. Following are the steps I followed and the errors I got on the way and in the end is the solution :-

  1. Generate SSH keys based on the instructions here and create the key in the github account.
  2. SSH into your Ubuntu server and create a directory where you want to clone the git repository.
    [plain] cd /var/www
    mkdir mywebsite
  3. Receive the following error
    [plain]mkdir: cannot create directory `mywebsite’: Permission denied[/plain]
  4. Try again with sudo
    [plain]sudo mkdir mywebsite[/plain]
  5. CD into the new directory
    [plain] cd mywebsite[/plain]
  6. Clone the Git repo
    git clone git .
  7. Receive the following error
    Cloning into ….
    /var/www/mywebsite/.git: Permission denied


  8. Try again with sudo
    [plain]sudo git clone git .
  9. Receive the following error
    Cloning into ….
    Permission denied (publickey).
    fatal: The remote end hung up unexpectedly


At this point, I was not really sure what to do. As I was not sure what was the problem. After carefully going through this troubleshooting guide, it was apparent that the user that creates the SSH key should be the same as user who clones the Git repo. And in my case, in Step 1, while creating the SSH keys, I did NOT use the “sudo” command. Whereas, while creating the folders with “mkdir” command I had to use the “sudo” command. So all I had to do was make sure those folders have the same owner as the owner of the SSH keys which was the user I was logged on as “ubuntu” and not “root”.

So, this changes the owner :-

sudo chown -R ubuntu /var/www/mywebsite

After this you can run Step 6 again and git clone will now work ๐Ÿ™‚

Tagged with:  

28 Responses to Permission denied (publickey) with Git on Ubuntu

  1. Permissions are a real beast when you first start using *nix environs. ๐Ÿ™‚

  2. Anuj Gakhar says:

    Sure they are ๐Ÿ™‚

  3. Aaron WEst says:

    One thing you can do if you don’t want to type “sudo” before each command, is to switch to the root user temporarily. You do this with the command “su – root” [enter]. You’ll be prompted for the root users password. After executing this command, you can run whatever additional commands (as root) you want, and then exist the root user with “exit” [enter].

  4. radekg says:

    In ubuntu you need to enable root account first

  5. Matt says:

    Thanks! Looked everywhere for the solution to this problem and your answer fixed it.

  6. A. C. Grace says:

    Thank you Anuj,

    Your post helped me finally fix this pervasive permissions problem.

  7. Chase says:

    Hi Anuj,

    That’s why I visit here — because you work out the answers for the rest of us… thanks!

  8. Russell says:

    Great advice.. thanks for sharing it with us

  9. Mark says:

    thanks for sharing this fix… worked great

  10. Louis says:

    This is a great fix.. helped out on a few of our computers

  11. Unmesh says:

    Thanks Anuj. Great post. This solution works!

  12. Zoe says:

    Great site! This a big help to each and everyone. Thanks for sharing!

  13. Barbara says:

    Glad to visit this site! Thanks you so much for the solutions you share. ๐Ÿ™‚

  14. Skashi says:

    Brilliant! I was having the same issue and after changing the file/folder permissions of the /var/www/sitename folder to my logged in user it worked perfectly! THANKS THANKS THANKS!!

  15. Eric Harrison says:

    Awesome. Thanks for giving us an answer to our problem. Thanks for sharing this to us.

  16. Vinod Kumar says:

    Thanks dear, you made my day ๐Ÿ™‚

  17. Dianne says:

    Ola! Anujgakhar,
    I just stumbled across this and “Authorization Denied”. Aaargh! Even though annoying when you are denied access to Access (no pun intended), it can be a godsend if there is someone else trying to access YOUR database and your information, if the protection holds business.
    Keep up the posts!

  18. you saved me from lots of headaches ๐Ÿ™‚

  19. Jason K says:

    This is a great help for us. Thanks indeed.

  20. You just saved me from killing myself. Thanks.

  21. roxxypoxxy says:

    In my case i had weird situation, for eg I could access
    localhost/my_project/admin was throwing 500, in fact url other than project root was not accessible .. after lots of scratching I found this at

    Header set Access-Control-Allow-Origin “*”

    in www/.htaccess and after commenting it out , evreything worked.

  22. Sridevi says:

    Thanks a lot yah really i spent 2-3 hrs in finding the root cause .Atlast i found your solution and it worked like a charm .Thanks

  23. Karthick Paramanantham says:

    Thanks anush — it really helped me

  24. I thank you. and indirectly so does my coworker client and company

  25. Joni Rajput says:

    Thanks a lot. It resolved my issue:)

Leave a Reply

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

ยฉ 2011 Anuj Gakhar