0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

VagrantでRHEL 10系LinuxへのSSH接続が失敗する

0
Last updated at Posted at 2026-08-17

VagrantでRocky Linux 10を使っていたところ、プロビジョニング中にSSH接続が失敗するようになりました。

Ansibleプロビジョナの場合は、以下のようにUNREACHABLEになります。

fatal: [node-1]: UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: kex_exchange_identification: Connection closed by remote host",
    "unreachable": true
}

ゲスト側のsshdログを確認すると、以下が出力されています。

Timeout before authentication for connection from 172.23.135.238 to 10.0.2.15, pid = 965
Timeout before authentication for connection from 172.23.135.238 to 10.0.2.15, pid = 1190
Timeout before authentication for connection from 172.23.135.238 to 10.0.2.15, pid = 1534
Timeout before authentication for connection from 172.23.135.238 to 10.0.2.15, pid = 1903
drop connection #0 from [172.23.135.238]:36300 on [10.0.2.15]:22 penalty: exceeded LoginGraceTime

RHEL 10系Linuxで使用されているOpenSSHではPerSourcePenaltiesがデフォルトで有効になっており、認証を完了しない状態がLoginGraceTimeを超過すると、接続元にペナルティが課されます。

今回はRocky Linux 10で確認していますが、RHEL 10、AlmaLinux 10などでも同じ条件で発生する可能性があります。

調査すると、VagrantがVMの起動時にSSH接続を確認した際の接続が残っていました。

Vagrantが使用しているRubyのNet::SSHライブラリでは、TCP接続後にSSHサーバとのバージョンネゴシエーションがタイムアウトすると、作成したソケットがクローズされずに残ります。

Net::SSH 7.3.0のlib/net/ssh/transport/session.rbでは以下の処理になっています。

          @server_version = ServerVersion.new(socket, logger, options[:timeout])

          @algorithms = Algorithms.new(self, options)
          @algorithms.start
          wait { algorithms.initialized? }
        rescue Errno::ETIMEDOUT
          raise Net::SSH::ConnectionTimeout
        end

SSHサーバとのバージョンネゴシエーション中にNet::SSH::ConnectionTimeoutが発生した場合、この例外処理では捕捉されないため、ソケットがクローズされずに残ります。

また、Errno::ETIMEDOUTが発生した場合も、Net::SSH::ConnectionTimeoutに変換して再送出していますが、ソケットをクローズする処理はありません。

Net::SSH単体でも、TCP接続を受け付け、SSHのバージョン情報を返さないサーバを用意すると再現しました。

タイムアウトを3回発生させた後も、接続がESTABLISHEDのまま残ります。

$ ss -ntp | grep ':22222'
ESTAB 0 0 127.0.0.1:44040 127.0.0.1:22222 users:(("ruby",pid=227,fd=5))
ESTAB 0 0 127.0.0.1:44056 127.0.0.1:22222 users:(("ruby",pid=227,fd=6))
ESTAB 0 0 127.0.0.1:50084 127.0.0.1:22222 users:(("ruby",pid=227,fd=7))

以下のように、タイムアウト時にソケットをクローズするよう変更して確認しました。

@@ -88,7 +88,11 @@
           @algorithms.start
           wait { algorithms.initialized? }
         rescue Errno::ETIMEDOUT
+          @socket&.close
           raise Net::SSH::ConnectionTimeout
+        rescue Net::SSH::ConnectionTimeout
+          @socket&.close
+          raise
         end

         def host_keys

変更後はタイムアウトした接続がESTABLISHEDのまま残らなくなり、Vagrantで発生していたSSH接続失敗も再現しなくなりました。

この問題について、Net::SSHのIssueを登録しています。

Socket is not closed when server version negotiation times out #1011

2026年8月17日時点では、まだ回答はありません。

なお、上記パッチは原因確認のためのもので、Net::SSHで正式に採用された修正ではありません。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?