dongxikuo5171
2014-02-09 14:02
浏览 123
已采纳

无法使用ssh2_auth_pubkey_file连接到php中的EC2

I'm trying to connect my EC2 instance with ssh2 php module.

I've obtained the public key from known_hosts file (format: "ssh-rsa XXXX..." putted into "public.txt" file) and i'm using the amazon keypair.pem as private key ("-----BEGIN RSA PRIVATE KEY----- XXXX..." in private.pem).

I'm using this php code:

$connection = ssh2_connect('XXXX.eu-west-1.compute.amazonaws.com', 22, array('hostkey'=>'ssh-rsa'));    
if(!ssh2_auth_pubkey_file(
    $connection,
    'ubuntu',
    'public.txt',
    'private.pem'
))
    die("Auth error");  
$sftp = ssh2_sftp($connection);

The error i get is: ssh2_auth_pubkey_file(): Authentication failed for ubuntu using public key: Username/PublicKey combination invalid

What am i doing wrong?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dougang6178 2014-02-10 19:48
    已采纳

    The public key in the known_hosts file isn't the public key you need to be using. Rather, you need to be using the public key from authorized_keys.

    That said, the fact that libssh2 even requires the public key is stupid. Honestly, I'd recommend you use phpseclib, a pure PHP SSH2 implementation, over libssh2. eg.

    <?php
    include('Net/SFTP.php');
    include('Crypt/RSA.php');
    
    $ssh = new Net_SFTP('www.domain.tld');
    $key = new Crypt_RSA();
    $key->loadKey(file_get_contents('private.pem'));
    if (!$ssh->login('username', $key)) {
        exit('Login Failed');
    }
    
    print_r($ssh->nlist());
    ?>
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题