Authenticate using a public key
Example 2346. Authentication using a public key
Code Examples / Notes » ssh2_auth_pubkey_file
This is probably incredibly insecure but if you are in a closed environment, you can run it at your own risk.
I was trying to get ssh access from server A to server B but server A was running apache as 'apache' and
I needed files on server B that were owned by root. So I needed some way for the apache user to connect
to server B as root. Here's what I did (both servers running linux - specifically CentOS4):
1. ssh-keygen -t rsa -f id_rsa generate with no passphrase
2. append the id_rsa.pub file to server B /root/.ssh/authorized_keys2
3. copy the id_rsa and id_rsa.pub files to a directory like /var/www/.ssh/
(happens to be apache's home dir on CentOS)
4. chown -R apache.apache /var/www/.ssh
Then you can connect like this:
$connection = ssh2_connect($server,22,array('hostkey'=>'ssh-rsa'));
echo "no success :-(";
The public key must be on one line starting with the key type, 1 space and followed by the keydata (no newlines) and not followed by comments. This is a limitation of libssh2, so remove any excess data from the file after creating it with your key generation tools.
So it would look something like this:
The key files need to be in OpenSSH's format.
For example the pubkey file should look like this: