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 github.com 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.
     cd /var/www
    mkdir mywebsite
    
  3. Receive the following error
    mkdir: cannot create directory `mywebsite': Permission denied
  4. Try again with sudo
    sudo mkdir mywebsite
  5. CD into the new directory
     cd mywebsite
  6. Clone the Git repo
    git clone git @github.com:companyname/repo.git .
    
  7. Receive the following error
    Cloning into ....
    /var/www/mywebsite/.git: Permission denied
    
    
  8. Try again with sudo
    sudo git clone git @github.com:companyname/repo.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. Matt says:

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

  5. A. C. Grace says:

    Thank you Anuj,

    Your post helped me finally fix this pervasive permissions problem.

  6. Chase says:

    Hi Anuj,

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

  7. Russell says:

    Great advice.. thanks for sharing it with us

  8. Mark says:

    thanks for sharing this fix… worked great

  9. Louis says:

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

  10. Unmesh says:

    Thanks Anuj. Great post. This solution works!

  11. Zoe says:

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

  12. Barbara says:

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

  13. 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!!

  14. Eric Harrison says:

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

  15. Vinod Kumar says:

    Thanks dear, you made my day ๐Ÿ™‚

  16. 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!

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

  18. Jason K says:

    This is a great help for us. Thanks indeed.

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

  20. roxxypoxxy says:

    In my case i had weird situation, for eg I could access
    localhost/my_project
    but
    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.

  21. 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

  22. Karthick Paramanantham says:

    Thanks anush — it really helped me

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

  24. Joni Rajput says:

    Thanks a lot. It resolved my issue:)

Leave a Reply

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 445 other subscribers

ยฉ 2011 Anuj Gakhar
%d bloggers like this: