Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating SSH keys for 'apache' user

How do I add SSH keys for 'apache' user in Linux?

BACKGROUND

I am trying to add a service hook to github to notify a URL once I push to my repo. I have the following php page set up:

<?php `git pull origin master`;   

However I get the following output:

sh: git: Permission denied 

This is because the keys I generated for github access were generated by my 'root' user. However when I exectue a command from php it is the 'apache' user that runs it.

The keys therefore do not correspond and permission is denied to pull.

As I cannot switch user from the terminal to generate keys as 'apache', I am not too sure what to do. Can anyone suggest a solution?

like image 601
Kit Avatar asked Sep 05 '11 10:09

Kit


People also ask

How do I generate a SSH key for a user?

Open a terminal and use the ssh-keygen command with the -C flag to create a new SSH key pair. Replace the following: KEY_FILENAME : the name for your SSH key file. For example, a filename of my-ssh-key generates a private key file named my-ssh-key and a public key file named my-ssh-key.

Can I generate SSH keys for someone else?

"ssh keys should never be generated for another user": That is true in the simple case. But consider multiple identities of the same physical person, for example.


2 Answers

As you are root, you can try it sudo -u apache ssh-keygen -t rsa

like image 45
yvan Avatar answered Oct 22 '22 05:10

yvan


You may have to copy the root generated keys in the .ssh directory of your apache user.

Assuming the homedir of apache is /var/www (check /etc/passwd) and the named key is id_rsa-git :

mkdir -p /var/www/.ssh/ cp /root/.ssh/id_rsa-git /var/www/.ssh/id_rsa 

No need to copy the public key.

Note : by default the key used are id_rsa or id_dsa. You may change the name of the copied key to match this.

You may also change ownership of the id_rsa key and .ssh directory:

chown -R apache:apache /var/www/.ssh chmod 0700 /var/www/.ssh chmod 0600 /var/www/.ssh/id_rsa 
like image 98
Vincent Avatar answered Oct 22 '22 05:10

Vincent