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

无法使用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());
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 请教一下c语言的代码里有一个地方不懂
  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了