Techioz Blog

Net::SFTP がcurve25519-sha256 アルゴリズムを使用して接続しない

概要

私は net-sftp gem を使用して、curve25519-sha256 アルゴリズムを使用している sftp サーバーに接続しています。クライアントがこのアルゴリズムをサポートしていないというエラーが発生します。問題セクションを読み、それに関する多くの議論を見つけ、いくつかの方法を試しましたが、それでも動作させることができません。以下は私のコードです

require 'net/sftp'
Net::SSH::Transport::Algorithms::DEFAULT_ALGORITHMS[:kex].unshift("[email protected]")
Net::SFTP.start('ip_address_of_site', 'password', { password: 'password', append_all_supported_algorithms: true, verify_host_key: :never, verbose: :debug}, verify_host_key: :never) do |sftp|
  ssh.sftp.connect do |sftp|
    puts sftp.inspect
  end
end

次のエラーが発生します

net-ssh-6.1.0/lib/net/ssh/transport/algorithms.rb:407:in `negotiate': could not settle on kex algorithm (Net
::SSH::Exception)
Server kex preferences: curve25519-sha256,[email protected]
Client kex preferences: ecdh-sha2-nistp521,ecdh-sha2-nistp384,ecdh-sha2-nistp256,diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha1,d
iffie-hellman-group-exchange-sha1,diffie-hellman-group1-sha1

Mac ソフトウェアを使用して接続すると正常に動作するため、パスワードに問題はありません。

解決策

たくさん試した結果、次のような解決策があることがわかりました。同じことをしている人の助けになるかもしれません。

Gemfile に gem を追加する

gem 'x25519'

スクリプトに次の行を追加します

require 'x25519'

これにより、そのアルゴリズムを使用してトリック アンド コネクトが実行されます。ただし、これは Mac M1 プロセッサではなく Intel プロセッサでも動作します。

net-ssh ライブラリには次のコードがあります。

if Net::SSH::Authentication::ED25519Loader::LOADED
  DEFAULT_ALGORITHMS[:host_key].unshift(
    '[email protected]',
    'ssh-ed25519'
  )
end

if Net::SSH::Transport::Kex::Curve25519Sha256Loader::LOADED
  DEFAULT_ALGORITHMS[:kex].unshift(
    'curve25519-sha256',
    '[email protected]'
  )
end

これら 2 つの gem (x25519、ed25519) がインストールされている場合は、それらが読み込まれます。そのため、gem をインストールしてロードすると問題が解決します。