MeWrite Docs

ssh: connect to host: Connection timed out

SSH接続がタイムアウトした場合に発生するエラー

概要

ssh: connect to host: Connection timed out は、SSH接続を試みた際に、サーバーからの応答が得られずタイムアウトした場合に発生するエラーです。ネットワークの問題、ファイアウォール、SSHサービスの問題などが原因です。

エラーメッセージ

ssh: connect to host 192.168.1.100 port 22: Connection timed out
ssh: connect to host example.com port 22: Operation timed out
ssh: connect to host example.com port 22: Network is unreachable

原因

  1. サーバーが起動していない: サーバーの電源が入っていない
  2. SSH サービスが停止: sshd が起動していない
  3. ファイアウォール: ポート 22 がブロックされている
  4. ネットワークの問題: ルーティング、DNS の問題
  5. IP アドレスの誤り: 間違ったアドレスに接続
  6. セキュリティグループ: クラウドのネットワーク設定

解決策

1. 基本的な接続確認

1
2
3
4
5
6
7
8
9
# ping で到達性を確認
ping -c 4 example.com

# ホスト名の解決を確認
nslookup example.com
dig example.com

# traceroute でルートを確認
traceroute example.com

2. ポートの確認

1
2
3
4
5
6
# ポートが開いているか確認
nc -zv example.com 22
telnet example.com 22

# nmap でポートスキャン
nmap -p 22 example.com

3. サーバー側でSSHサービスを確認

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# SSH サービスの状態確認
sudo systemctl status sshd
sudo systemctl status ssh

# SSH サービスを起動
sudo systemctl start sshd

# SSH がリッスンしているか確認
sudo netstat -tlnp | grep :22
sudo ss -tlnp | grep :22

4. ファイアウォールの確認

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# Ubuntu (ufw)
sudo ufw status
sudo ufw allow ssh
sudo ufw allow 22/tcp

# CentOS/RHEL (firewalld)
sudo firewall-cmd --list-all
sudo firewall-cmd --add-service=ssh --permanent
sudo firewall-cmd --reload

# iptables
sudo iptables -L -n | grep 22
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT

5. AWS Security Group の確認

1
2
3
4
5
6
7
8
# AWS CLI で確認
aws ec2 describe-security-groups --group-ids sg-12345678

# インバウンドルールを確認
# - Type: SSH
# - Protocol: TCP
# - Port Range: 22
# - Source: Your IP または 0.0.0.0/0

6. SSH 設定の確認

1
2
3
4
5
6
7
8
# サーバー側の設定
sudo cat /etc/ssh/sshd_config | grep -E "^(Port|ListenAddress|PermitRootLogin)"

# ポートが変更されている場合
ssh -p 2222 user@example.com

# 複数のアドレスでリッスン
# ListenAddress 0.0.0.0

7. VPN / Proxy の確認

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# VPN 接続を確認
# VPN が必要なネットワークに接続しているか

# ProxyJump を使用(踏み台サーバー経由)
ssh -J jump-host user@target-host

# ~/.ssh/config で設定
Host target
    HostName target-host
    User user
    ProxyJump jump-host

8. タイムアウト設定の調整

1
2
3
4
5
# 接続タイムアウトを延長
ssh -o ConnectTimeout=30 user@example.com

# キープアライブを設定
ssh -o ServerAliveInterval=60 user@example.com
# ~/.ssh/config
Host *
    ConnectTimeout 30
    ServerAliveInterval 60
    ServerAliveCountMax 3

9. デバッグモードで接続

1
2
3
4
5
6
7
8
# 詳細なデバッグ出力
ssh -vvv user@example.com

# 確認ポイント:
# - DNS解決
# - 接続先IP
# - 使用されるポート
# - 認証方法

10. ネットワーク設定の確認

1
2
3
4
5
6
7
8
9
# ローカルのネットワーク設定
ip addr
ip route

# DNS の確認
cat /etc/resolv.conf

# hosts ファイル
cat /etc/hosts

クラウド環境での対処

AWS EC2

1
2
3
4
5
6
7
8
# インスタンスの状態確認
aws ec2 describe-instance-status --instance-ids i-12345678

# システムログを確認
aws ec2 get-console-output --instance-id i-12345678

# Security Group の確認
aws ec2 describe-security-groups --group-ids sg-12345678

GCP

1
2
3
4
5
6
# ファイアウォールルール
gcloud compute firewall-rules list
gcloud compute firewall-rules create allow-ssh --allow tcp:22

# インスタンスの状態
gcloud compute instances describe instance-name

SSH サーバーの再設定

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# SSH の再インストール(Ubuntu)
sudo apt-get purge openssh-server
sudo apt-get install openssh-server

# sshd_config の最小設定
Port 22
ListenAddress 0.0.0.0
PermitRootLogin no
PasswordAuthentication yes
PubkeyAuthentication yes

# 設定を反映
sudo systemctl restart sshd

デバッグのコツ

ログの確認

1
2
3
4
5
6
# サーバー側のログ
sudo tail -f /var/log/auth.log      # Ubuntu
sudo tail -f /var/log/secure        # CentOS

# journalctl
sudo journalctl -u sshd -f

パケットキャプチャ

1
2
3
4
5
# tcpdump で確認
sudo tcpdump -i any port 22

# 特定のホストへの通信
sudo tcpdump -i any host example.com and port 22

接続のシミュレーション

1
2
3
4
5
# curl で接続テスト
curl -v telnet://example.com:22

# OpenSSL で接続
openssl s_client -connect example.com:22

関連エラー

SSH の他のエラー

最終更新: 2025-12-08