タイトル: エラー一覧
SEOタイトル: SSH よくあるエラー集と対処完全ガイド — Permission denied/Host key/Timeout
| この記事の要点 |
|
SSH エラー調査の基本
# verbose(必須)
ssh -v user@host # 軽い情報
ssh -vv user@host # 認証フェーズ詳細
ssh -vvv user@host # 全プロトコル詳細
# 鍵をどれを使ったか
ssh -vvv user@host 2>&1 | grep -i "offering\|identity"
# サーバ側(root で)
journalctl -u sshd -f
tail -f /var/log/auth.log # Debian/Ubuntu
tail -f /var/log/secure # CentOS/RHEL
1. Permission denied (publickey)
user@host: Permission denied (publickey).
原因の切り分け:
| 原因 | 確認 | 対処 |
|---|---|---|
| 公開鍵未登録 | サーバの ~user/.ssh/authorized_keys | ssh-copy-id or 手動追記 |
| ~/.ssh パーミッションが甘い | ls -la ~/.ssh | chmod 700 ~/.ssh; chmod 600 ~/.ssh/* |
| 違う鍵を提示 | ssh -vvv で Offering を確認 | -i で明示 or ~/.ssh/config |
| ユーザ名違い | ssh ec2-user@ / ubuntu@ 等 | 正しいユーザに |
| サーバ側 SELinux | ausearch -m AVC | restorecon -R ~user/.ssh |
| sshd_config で公開鍵拒否 | PubkeyAuthentication | yes に |
# クライアント側パーミッション修正
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub
chmod 600 ~/.ssh/config
chmod 644 ~/.ssh/known_hosts
# サーバ側(重要!甘いとログイン拒否される)
ssh user@host
# 入れない場合は別経路(コンソール等)で
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
chown -R user:user ~user/.ssh
2. Permission denied (password)
user@host's password:
Permission denied, please try again.# 単にパスワード違い → 正しいパスワード
# ユーザ名違いの可能性
ssh -v user@host 2>&1 | grep "Authenticating"
# サーバで PasswordAuthentication が無効化されている
grep -i "PasswordAuthentication" /etc/ssh/sshd_config
# PasswordAuthentication no ← 公開鍵認証のみ許可
# root 直接ログインが禁止
grep -i "PermitRootLogin" /etc/ssh/sshd_config
# PermitRootLogin no
# → 一般ユーザでログイン後 sudo
3. Connection refused
ssh: connect to host example.com port 22: Connection refused
TCP 接続自体が拒否されています。原因はサーバ側:
# 1. sshd が起動しているか(サーバ側で確認)
systemctl status sshd
sudo systemctl start sshd
sudo systemctl enable sshd
# 2. リッスンポート確認
ss -tlnp | grep :22
# LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=1234,fd=3))
# 3. ポートを変えてある?
grep -i "^Port" /etc/ssh/sshd_config
# Port 2222
ssh -p 2222 user@host
# 4. firewall (Linux)
sudo firewall-cmd --list-all # firewalld
sudo iptables -L -n | grep 22 # iptables
sudo ufw status # Ubuntu
# 開放例
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --reload
sudo ufw allow 22/tcp
# 5. クラウドの SecurityGroup / NSG
# AWS: Security Group inbound 22 を 0.0.0.0/0 or 自 IP に
# Azure: NSG / Network Security Group
# GCP: VPC firewall-rules
4. Connection timed out
ssh: connect to host example.com port 22: Connection timed out
TCP SYN が返ってこない = 経路上で破棄。
# 到達性確認
ping example.com # ICMP(firewall でブロックの可能性あり)
traceroute example.com # 経路確認
mtr example.com # 持続的な品質確認
# TCP 22 への到達
nc -vz example.com 22
telnet example.com 22
# クラウド側で IP が変わっていないか
nslookup example.com
dig example.com
# VPN / 社内 NW 経由が必要かも
# 自宅から VPN 接続 → 再試行
5. Host key verification failed
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
Host key verification failed.
サーバ側のホスト鍵が以前接続時と変わっています。正常な再構築か中間者攻撃かを判断する必要があります。
# 正常な再構築と分かっている場合 → 古いキー削除
ssh-keygen -R example.com
ssh-keygen -R 192.0.2.1
# /home/user/.ssh/known_hosts updated.
# 再接続
ssh user@example.com
# → 新しい鍵を accept するか聞かれる
# 新しい fingerprint を事前検証
ssh-keyscan example.com
# 別経路(コンソール等)で取得した fingerprint と比較
ssh-keygen -lf <(ssh-keyscan example.com 2>/dev/null)
# 全 known_hosts をリセット(最終手段)
> ~/.ssh/known_hosts
6. Too many authentication failures
Received disconnect from 1.2.3.4 port 22:2: Too many authentication failures
ssh-agent に多数の鍵が登録され、全部試して上限を超えています。
# Agent の鍵を確認
ssh-add -l
# → 沢山あるはず
# 一旦全削除
ssh-add -D
# 必要な鍵だけ追加
ssh-add ~/.ssh/id_ed25519_for_host
# または -o で他の鍵試行を止める
ssh -o IdentitiesOnly=yes -i ~/.ssh/id_target user@host
# config に書く
Host target
HostName target.example.com
IdentityFile ~/.ssh/id_target
IdentitiesOnly yes
7. Bad permissions on ~/.ssh
Permissions 0644 for '/home/user/.ssh/id_ed25519' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.# 秘密鍵は 600 必須
chmod 600 ~/.ssh/id_*
chmod 700 ~/.ssh
8. Agent admitted failure to sign using the key
# パスフレーズ入れ間違え or 鍵が壊れている
ssh-add -D
ssh-add ~/.ssh/id_ed25519
# パスフレーズを正しく入力
# 鍵を新規生成して登録
ssh-keygen -t ed25519
ssh-copy-id user@host
9. Algorithm negotiation failed (古いサーバ)
Unable to negotiate with 1.2.3.4 port 22: no matching key exchange method found.
Their offer: diffie-hellman-group1-sha1
古い SSH サーバが脆弱なアルゴリズムしか提示できないケース。OpenSSH 7.0+ では既定で無効です。
# 緊急回避(セキュリティ低下を理解した上で)
ssh -oKexAlgorithms=+diffie-hellman-group1-sha1 user@host
ssh -oHostKeyAlgorithms=+ssh-rsa user@host
ssh -oCiphers=+aes128-cbc user@host
# 本筋: サーバ側を更新する
# /etc/ssh/sshd_config を最新化、OS の OpenSSH を更新
10. その他のレアエラー
| エラー | 原因 | 対処 |
|---|---|---|
| No supported authentication methods available (server sent: publickey) | サーバが公開鍵のみ許可 | 公開鍵を登録 |
| Could not chdir to home directory | ホームディレクトリ未作成 | mkdir ~user; chown user:user ~user |
| shell request failed on channel 0 | シェル /bin/false 等で禁止 | chsh -s /bin/bash user |
| Connection reset by peer | fail2ban / TCP RST 送信 | fail2ban 設定確認、IP 解除 |
| kex_exchange_identification: read: Connection reset | TCP wrapper で拒否 | /etc/hosts.deny 確認 |
| Write failed: Broken pipe | セッションタイムアウト | ServerAliveInterval 60 |
| Operation timed out | NAT セッションタイムアウト | 同上 |
fail2ban で BAN された場合
# サーバ側で
sudo fail2ban-client status sshd
# Banned IP list: 192.0.2.50
# 解除
sudo fail2ban-client set sshd unbanip 192.0.2.50
# whitelist 設定 (jail.local)
[sshd]
ignoreip = 127.0.0.1/8 ::1 192.0.2.0/24
切り分けフローチャート
SSH 接続できない
│
├─ "Connection refused"
│ → sshd 起動 / ポート / firewall 確認
│
├─ "Connection timed out"
│ → 経路 / SecurityGroup / NAT 確認
│ → ping / traceroute
│
├─ "Host key verification failed"
│ → ssh-keygen -R で削除 → 再接続
│
├─ "Permission denied (publickey)"
│ → ~/.ssh パーミッション
│ → authorized_keys に登録あるか
│ → ssh -vvv で Offering 鍵を確認
│
├─ "Too many authentication failures"
│ → ssh-add -D → 必要鍵のみ追加
│ → -o IdentitiesOnly=yes
│
└─ それ以外
→ ssh -vvv で詳細
→ サーバ側 /var/log/auth.log
FAQ
Q: AWS EC2 で繋がらない
A: ① SecurityGroup の 22 開放、② キーペア(pem ファイル)正しく指定、③ ユーザ名は AMI 依存(ec2-user / ubuntu / centos)、④ パブリック IP 変動(停止再起動)。
Q: ログインはできるが git push で「Permission denied (publickey)」
A: GitHub 用の公開鍵が登録されていない。~/.ssh/id_*.pub を GitHub Settings → SSH keys に登録。ssh -T git@github.com で接続テスト。
Q: 多段 SSH の途中で詰まる
A: ~/.ssh/config の ProxyJump + 中継サーバへの認証が通ることが前提。ssh -J bastion target で個別に試して切り分け。