0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AmazonEC2にTeratermでSSH接続したまま放置すると、Teratermが落ちてしまう問題の対処

0
Last updated at Posted at 2026-02-14

EC2:SSH(Tera Term)切断対策

AWSの学習を始めたばかりの頃に引っかかった事象について、自分用のメモも兼ねて書いてみる。

■ 現象

・Tera TermでEC2にSSH接続すると、数分で切断される。
・何も操作していない状態(アイドル状態)で接続が切れる。

■ 原因

EC2側のSSH設定で KeepAliveが無効 の場合、以下の機器が
「通信が停止している」と判断し、接続を切断することがあります。

・家庭用ルータ
・Wi-Fi機器
・VPN
・会社のファイアウォール

■ 解決策

<対策の概要>

EC2のSSHサーバに KeepAlive設定 を行い、
定期的に通信(生存確認)を送ることで接続を維持します。

設定対象ファイル:

/etc/ssh/sshd_config

<対策手順>

① 現在の設定を確認

sudo grep -E "ClientAlive|TCPKeepAlive" /etc/ssh/sshd_config

以下のように コメントアウトされている場合:

#ClientAliveInterval 0
#ClientAliveCountMax 3
#TCPKeepAlive yes

→ KeepAliveは実質無効

② 設定ファイルを編集

sudo vi /etc/ssh/sshd_config

ファイルの末尾に以下を追加:

ClientAliveInterval 60
ClientAliveCountMax 5
TCPKeepAlive yes

設定内容の意味
ClientAliveInterval 60 :60秒ごとにクライアントへ生存確認を送信
ClientAliveCountMax 5 :応答が5回失敗するまで接続を維持
TCPKeepAlive yes    :OSレベルのKeepAliveを有効

※ 上記設定をすると、約5分間通信がなくても接続が維持されます

③ 設定を反映

sudo systemctl restart sshd

④ 設定確認

sudo grep -E "ClientAlive|TCPKeepAlive" /etc/ssh/sshd_config

以下のように表示されれば設定完了:

ClientAliveInterval 60
ClientAliveCountMax 5
TCPKeepAlive yes

■ 追加対策(推奨)
クライアント側(Tera Term)でもKeepAliveを設定すると、さらに安定します。

<Tera Term設定>
設定 → SSH
KeepAliveを有効
間隔:30秒
設定 → 設定の保存

■ 動作イメージ

対策前
通信なし → ルータ等が接続削除 → SSH切断
対策後
60秒ごとに通信 → セッション維持 → 切断されない
0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?