I am trying to connect with remote server using SSH Key. I am generating SSH keypair using phpseclib 2.0
and I storing keys in the database. Here is how I am trying to connect.
Config::set('remote.connections.production.host',$server->ip);
Config::set('remote.connections.production.username',$server->username);
Config::set('remote.connections.production.keytext',$server->meta()->where('meta_key','private_key')->first()->meta_value);
I have public key corresponding to the private key I am using in remote server's ~/.ssh/authorized_keys
file. The permissions for .ssh
directory is 700
and permissions for authorized_keys
is 600
which is perfect.
First I thought this might be a
issue but then I tried to remove all
from the private key before connecting but I am still getting unable to connect to remote server
. I don't know where the issue is. I am trying since last two days without luck.
I think SSH key gets damaged once it enters into the database. Here is how I am generating and storing SSH keypair in the database.
$rsa = new RSA();
$rsa->setPublicKeyFormat(RSA::PUBLIC_FORMAT_OPENSSH);
$keys = $rsa->createKey(2048);
$privateKey = $keys['privatekey'];
$publicKey = $keys['publickey'];
$server->meta()->create([
'meta_key' => 'private_key',
'meta_value' => $privateKey
]);
$server->meta()->create([
'meta_key' => 'public_key',
'meta_value' => $publicKey
]);
Any help would be appreciated. Keys are being generated for sure because I can see it in the database.