Question

ssh2_auth_pubkey_file : Can't Get To Work

I am trying to copy a file from one server to another server. I can do this with ssh2_auth_password on server’s without Keys, but I can’t find a way to do it withssh2_auth_pubkey_file on servers with Keys. I’m not using root with any of this. I haven’t enabled Private Networking.

On the sending server I have generated … a public key at /home/username/.ssh/id_rsa.pub a private key at /home/username/.ssh/id_rsa

On the receiving server I, too, have generated … a public key at /home/username/.ssh/id_rsa.pub a private key at /home/username/.ssh/id_rsa

On the receiving server, I have placed the sending server’s public key into /home/username/.ssh/authorized_keys

My code …

<?php 
$host = "IP_address_receiving_server”;
$port = 22; 
$conn = ssh2_connect($host, $port); 
if ($conn) 
{ 
echo "Can connect to server"; 
}else 
{ 
echo "Can Not connect to server"; 
} 

… it seems I can connect. The rest of the code.

$username = "username";
$pub_key = "ssh-rsa, numbers and letters, a /n, ending with username@receiving server name";
$pri_key = “MIIE, numbers and letters, ending with ==";
$passph = ""; //null
if (!ssh2_auth_pubkey_file($conn, $username, $pub_key, $pri_key, $passph)) 
{ 
echo "Authentication rejected by server"; 
}
?>

After it is run I receive the PHP and OS code message … Can connect to server PHP Warning: ssh2authpubkey_file(): Authentication failed for <username> using public key: Unable to open public key file in <address of file> on line 50 Autentication rejected by server

For the public and private key variables, I’m assuming they’re ones generated on the receiving server. However, I’ve tried keys generated on the sending server and that doesn’t work either.

From the PHP manual … http://php.net/manual/en/function.ssh2-auth-pubkey-file.php … I am leaving off this part of their code with ssh2_connect.

array('hostkey'=>'ssh-rsa')

Other Internet articles on the subject don’t include it, and I haven’t, simply because I don’t know what to substitute in for hostkey and ssh-rsa. I’m writing functionally, too. I’ve exhausted what I can do. Any assistance is much appreciated.

Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

This comment has been deleted

This comment has been deleted

This comment has been deleted