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 :-
- Generate SSH keys based on the instructions here and create the key in the github account.
- SSH into your Ubuntu server and create a directory where you want to clone the git repository.
[plain] cd /var/www
mkdir mywebsite
[/plain] - Receive the following error
[plain]mkdir: cannot create directory `mywebsite’: Permission denied[/plain] - Try again with sudo
[plain]sudo mkdir mywebsite[/plain] - CD into the new directory
[plain] cd mywebsite[/plain] - Clone the Git repo
[plain]
git clone git @github.com:companyname/repo.git .
[/plain] - Receive the following error
[plain]
Cloning into ….
/var/www/mywebsite/.git: Permission denied[/plain]
- Try again with sudo
[plain]sudo git clone git @github.com:companyname/repo.git .
[/plain] - Receive the following error
[plain]
Cloning into ….
Permission denied (publickey).
fatal: The remote end hung up unexpectedly[/plain]
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 :-
[plain]
sudo chown -R ubuntu /var/www/mywebsite
[/plain]
After this you can run Step 6 again and git clone will now work ๐
Permissions are a real beast when you first start using *nix environs. ๐
Sure they are ๐
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].
@Aaron, I did think about that. But I could not login as root on the Amazon EC2 Instance I was working on. Probably something I should look into further. Thanks for the tip though.
In case you can’t login as root but zou’re among the sudoers, the easy way to switch temporarily to a root shell is:
sudo bash
In ubuntu you need to enable root account first http://www.ducea.com/2006/06/21/ubuntu-how-to-enable-the-root-account/
Thanks for the tip, Radek.
Thanks! Looked everywhere for the solution to this problem and your answer fixed it.
Thank you Anuj,
Your post helped me finally fix this pervasive permissions problem.
Hi Anuj,
That’s why I visit here — because you work out the answers for the rest of us… thanks!
Great advice.. thanks for sharing it with us
thanks for sharing this fix… worked great
This is a great fix.. helped out on a few of our computers
Thanks Anuj. Great post. This solution works!
Great site! This a big help to each and everyone. Thanks for sharing!
Glad to visit this site! Thanks you so much for the solutions you share. ๐
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!!
Awesome. Thanks for giving us an answer to our problem. Thanks for sharing this to us.
Thanks dear, you made my day ๐
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!
you saved me from lots of headaches ๐
This is a great help for us. Thanks indeed.
You just saved me from killing myself. Thanks.
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.
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
Thanks anush — it really helped me
I thank you. and indirectly so does my coworker client and company
Thanks a lot. It resolved my issue:)