| 両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン | 
| linux:openssh:client [2025/02/28 10:16]  – [公開/秘密キーのペアの生成]  ともやん | linux:openssh:client [2025/09/25 13:11] (現在)  – [SSH 接続を一定期間放置すると Read from remote host xxx: Connection reset by peer が発生して切れる😅]  ともやん | 
|---|
| </WRAP> | </WRAP> | 
|  |  | 
| ==== リモートマシンから切断されて ssh がフリーズする🤪 ==== | ===== リモートマシンから切断されて ssh がフリーズする🤪 ===== | 
| ssh エスケープ文字 <html><code>~</code></html> につづけて  <html><code>.</code></html> で切断できる😉\\ | ssh エスケープ文字 <html><code>~</code></html> につづけて  <html><code>.</code></html> で切断できる😉\\ | 
| <WRAP color_term> | <WRAP color_term> | 
|  |  | 
| ===== トラブルシューティング ===== | ===== トラブルシューティング ===== | 
|  |  | 
|  | ==== SSH 接続を一定期間放置すると Read from remote host xxx: Connection reset by peer が発生して切れる😅 ==== | 
|  | <WRAP color_term> | 
|  | <WRAP color_result><html><pre> | 
|  | Read from remote host xxx: Connection reset by peer | 
|  | Connection to xxx closed. | 
|  | client_loop: send disconnect: Broken pipe | 
|  | </pre></html></WRAP> | 
|  | </WRAP> | 
|  |  | 
|  | クライアント側での設定 (''~/.ssh/config'')\\ | 
|  | <WRAP color_term> | 
|  | <WRAP color_command><html><pre> | 
|  | <font color="#0087FF"><b>$</b></font> <font color="#26A269">sed</font> <font color="#A347BA">-i</font> <font color="#A2734C">'1i Host *\n    ServerAliveInterval 60\n    ServerAliveCountMax 3\n'</font> <u style="text-decoration-style:solid">~/.ssh/config</u> | 
|  | <font color="#0087FF"><b>$</b></font> <font color="#26A269">bat</font> <u style="text-decoration-style:solid">~/.ssh/config</u> | 
|  | </pre></html></WRAP> | 
|  | <WRAP color_result><html><pre class=Bat> | 
|  | <font color="#839496">   1</font> <font color="#F92672">Host</font><font color="#F8F8F2"> </font><font color="#F92672">*</font> | 
|  | <font color="#839496">   2</font> <font color="#F8F8F2">    </font><font color="#F92672">ServerAliveInterval</font><font color="#F8F8F2"> </font><font color="#BE84FF">60</font> | 
|  | <font color="#839496">   3</font> <font color="#F8F8F2">    </font><font color="#F92672">ServerAliveCountMax</font><font color="#F8F8F2"> </font><font color="#BE84FF">3</font> | 
|  | <font color="#839496">   4</font> | 
|  | </pre></html></WRAP> | 
|  | </WRAP> | 
|  | * ''sed -i'': ファイルをインプレース編集(上書き保存)。バックアップしたい場合は -i.bak でバックアップ作成。 | 
|  | * ''1i'': ファイルの1行目に挿入。 | 
|  | * ''Host *\n    ServerAliveInterval 60\n    ServerAliveCountMax 3\n'': 挿入する内容。''\n'' で改行、インデント(スペース 4 つ)で設定を記述。 | 
|  | * ''~/.ssh/config'': 対象ファイル。 | 
|  |  | 
|  | * ''ServerAliveInterval 60'': 60 秒ごとにクライアントにパケット送信。 | 
|  | * ''ServerAliveCountMax 5'': 5 回応答なしで切断。 | 
|  |  | 
|  | [[linux:tmux|tmux]] 内では ssh 接続が持続する🤔\\ | 
|  | 理由は ''status-interval 15'' だから😅\\ | 
|  | ステータスバーに ''#[fg=green]%H:%M'' (時計)が 15 秒おきに接続を維持してる😂\\ | 
|  | <WRAP color_term> | 
|  | <WRAP color_command><html><pre> | 
|  | <font color="#0087FF"><b>$</b></font> <font color="#26A269">tmux</font> show-options <font color="#A347BA">-g</font> status-interval | 
|  | </pre></html></WRAP> | 
|  | <WRAP color_result><html><pre> | 
|  | status-interval 15 | 
|  | </pre></html></WRAP> | 
|  | </WRAP> | 
|  |  | 
|  | === サーバーのデフォルト設定の問題 === | 
|  | <WRAP color_term> | 
|  | <WRAP color_command><html><pre> | 
|  | <font color="#0087FF"><b>$</b></font> <font color="#26A269"><u style="text-decoration-style:solid">sudo</u></font> <font color="#26A269">cat</font> <u style="text-decoration-style:solid">/etc/ssh/sshd_config</u> <font color="#12488B"><b>|</b></font> <font color="#26A269">bat</font> <font color="#A347BA">-l</font> conf <font color="#A347BA">-H123</font> <font color="#A347BA">-H126:127</font> | 
|  | </pre></html></WRAP> | 
|  | <WRAP color_result><html><pre> | 
|  | <font color="#839496"> 107</font> <font color="#75715E">#PermitTTY yes</font> | 
|  | <font color="#839496"> 108</font> <font color="#75715E">#PrintMotd yes</font> | 
|  | <font color="#839496"> 109</font> <font color="#75715E">#PrintLastLog yes</font> | 
|  | <font color="#839496"> 110</font> <span style="background-color:#333333"><font color="#75715E">#TCPKeepAlive yes</font></span><span style="background-color:#333333">                                                                                                    </span> | 
|  | <font color="#839496"> 111</font> <font color="#75715E">#PermitUserEnvironment no</font> | 
|  | <font color="#839496"> 112</font> <font color="#75715E">#Compression delayed</font> | 
|  | <font color="#839496"> 113</font> <span style="background-color:#333333"><font color="#75715E">#ClientAliveInterval 0</font></span><span style="background-color:#333333">                                                                                               </span> | 
|  | <font color="#839496"> 114</font> <span style="background-color:#333333"><font color="#75715E">#ClientAliveCountMax 3</font></span><span style="background-color:#333333">                                                                                               </span> | 
|  | <font color="#839496"> 115</font> <font color="#75715E">#UseDNS no</font> | 
|  | <font color="#839496"> 116</font> <font color="#75715E">#PidFile /var/run/sshd.pid</font> | 
|  | <font color="#839496"> 117</font> <font color="#75715E">#MaxStartups 10:30:100</font> | 
|  | </pre></html></WRAP> | 
|  | </WRAP> | 
|  | * TCPKeepAlive yes: | 
|  | * 意味: OSレベルのTCPキープアライブを有効(デフォルト間隔はOS依存、通常7200秒=2時間)。 | 
|  | * 影響: TCP層で接続を維持するが、間隔が長すぎる(例: Fedoraのデフォルト ''net.ipv4.tcp_keepalive_time = 7200'')ため、ファイアウォール/ロードバランサの短いタイムアウト(例: 60秒)に対応できない。 | 
|  | * 問題: セキュリティリスク(スプーフィングに弱い)があり、''ClientAliveInterval'' の方が安全。 | 
|  | * ClientAliveInterval 0: | 
|  | * 意味:サーバーからクライアントへのキープアライブパケットを送信しない(0: 無効)。 | 
|  | * 影響:アイドル状態のSSH接続が、ファイアウォールやロードバランサ(例: AWS ELB のデフォルト60秒タイムアウト)で切断されやすい。 | 
|  | * 問題:「Connection reset by peer」や「Broken pipe」が発生する主因。ネットワーク機器がアイドルと判断し、接続をリセット。 | 
|  | * ClientAliveCountMax 3: | 
|  | * 意味:キープアライブ応答がない場合、3回試行後に接続を切断。 | 
|  | * 影響:''ClientAliveInterval 0'' なので実質無効(パケット送信がないため)。設定変更しない限り影響なし。 | 
|  |  | 
|  | <WRAP color_term> | 
|  | <WRAP color_command><html><pre> | 
|  | <font color="#0087FF"><b>$</b></font> <font color="#26A269"><u style="text-decoration-style:solid">sudo</u></font> <font color="#26A269">nano</font> <u style="text-decoration-style:solid">/etc/ssh/sshd_config</u> | 
|  | </pre></html></WRAP> | 
|  | <WRAP color_result><html><pre> | 
|  | <font color="#839496"> 107</font> <font color="#75715E">#PermitTTY yes</font> | 
|  | <font color="#839496"> 108</font> <font color="#75715E">#PrintMotd yes</font> | 
|  | <font color="#839496"> 109</font> <font color="#75715E">#PrintLastLog yes</font> | 
|  | <font color="#839496"> 110</font> <span style="background-color:#333333"><font color="#75715E">#TCPKeepAlive yes</font></span><span style="background-color:#333333">                                                                                                    </span> | 
|  | <font color="#839496"> 111</font> <font color="#F92672">TCPKeepAlive</font><font color="#FFFFFF"> no</font> | 
|  | <font color="#839496"> 112</font> <font color="#75715E">#PermitUserEnvironment no</font> | 
|  | <font color="#839496"> 113</font> <font color="#75715E">#Compression delayed</font> | 
|  | <font color="#839496"> 114</font> <span style="background-color:#333333"><font color="#75715E">#ClientAliveInterval 0</font></span><span style="background-color:#333333">                                                                                               </span> | 
|  | <font color="#839496"> 115</font> <font color="#F92672">ClientAliveInterval</font><font color="#FFFFFF"> 30</font> | 
|  | <font color="#839496"> 116</font> <span style="background-color:#333333"><font color="#75715E">#ClientAliveCountMax 3</font></span><span style="background-color:#333333">                                                                                               </span> | 
|  | <font color="#839496"> 117</font> <font color="#F92672">ClientAliveCountMax</font><font color="#FFFFFF"> 5</font> | 
|  | <font color="#839496"> 118</font> <font color="#75715E">#UseDNS no</font> | 
|  | <font color="#839496"> 119</font> <font color="#75715E">#PidFile /var/run/sshd.pid</font> | 
|  | <font color="#839496"> 120</font> <font color="#75715E">#MaxStartups 10:30:100</font> | 
|  | </pre></html></WRAP> | 
|  | </WRAP> | 
|  | * TCPKeepAlive no: ''ClientAliveInterval'' で十分。 | 
|  | * ClientAliveInterval 30: 30 秒ごとにキープアライブ送信。 | 
|  | * ClientAliveCountMax 5: 回応答なし(150 秒)で切断。 | 
|  |  | 
| ==== Fedora 41 で RSA 鍵認証が失敗する場合 ==== | ==== Fedora 41 で RSA 鍵認証が失敗する場合 ==== |