SSH鍵完全ガイド:生成、設定、複数アカウント管理とセキュリティ強化

開発ツール

SSH鍵:パスワードより安全な認証方式

比較項目 パスワード SSH鍵
認証方式 知識因子(知っているもの) 所持因子(持っているもの)
ブルートフォース攻撃 可能 ほぼ不可能
フィッシングリスク 高い 低い(秘密鍵は手元のマシンから出ない)
サーバー侵害時 平文/ハッシュ化パスワードが解読される可能性 公開鍵のみ保存、秘密鍵は逆算不可
利便性 毎回入力 一度設定すれば自動認証

一、鍵ペアの生成

Ed25519(推奨)

ssh-keygen -t ed25519 -C "your@email.com"
パラメータ 説明
-t ed25519 アルゴリズムの種類(Ed25519、最も安全で高速)
-C コメント(通常はメールアドレスで識別)

RSA(互換性重視の代替)

ssh-keygen -t rsa -b 4096 -C "your@email.com"
アルゴリズム 鍵長 安全性 性能 推奨
Ed25519 256 bit 最高 最速 ✅
ECDSA P256 256 bit 高い 速い 選択肢
RSA 4096 4096 bit 高い 遅い 互換性重視の代替
RSA 2048 2048 bit 中程度 遅い ❌ 非推奨

生成の流れ

$ ssh-keygen -t ed25519 -C "dev@example.com"
Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/user/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase): ****
Enter same passphrase again: ****
Your identification has been saved in /home/user/.ssh/id_ed25519
Your public key has been saved in /home/user/.ssh/id_ed25519.pub

パスフレーズの設定を強く推奨:秘密鍵が漏洩しても、パスフレーズがあればそのまま使われることを防げます。


二、公開鍵のデプロイ

GitHub / GitLab

# 公開鍵をコピー
cat ~/.ssh/id_ed25519.pub | clip

# GitHub → Settings → SSH and GPG keys → New SSH key
# 公開鍵の内容を貼り付け

サーバー

# 方法1:ssh-copy-id
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server.com

# 方法2:手動で追記
cat ~/.ssh/id_ed25519.pub | ssh user@server.com 'mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys'

接続確認

ssh -T git@github.com
# Hi username! You've successfully authenticated...

ssh user@server.com
# パスワードなしで直接ログイン

三、複数アカウントの設定

シナリオ:仕事用GitHub+個人用GitHub

# 2つの鍵ペアを生成
ssh-keygen -t ed25519 -C "work@company.com" -f ~/.ssh/id_work
ssh-keygen -t ed25519 -C "personal@gmail.com" -f ~/.ssh/id_personal

SSH Config

# ~/.ssh/config
Host github-work
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_work
  IdentitiesOnly yes

Host github-personal
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_personal
  IdentitiesOnly yes

Gitでの使用

# 仕事用リポジトリ
git clone git@github-work:company/project.git

# 個人用リポジトリ
git clone git@github-personal:username/project.git

複数プラットフォームの設定

# ~/.ssh/config
Host github.com
  User git
  IdentityFile ~/.ssh/id_github

Host gitlab.com
  User git
  IdentityFile ~/.ssh/id_gitlab

Host gitee.com
  User git
  IdentityFile ~/.ssh/id_gitee

Host dev.azure.com
  User git
  IdentityFile ~/.ssh/id_azure

四、SSH Agentの管理

Agentの起動

eval "$(ssh-agent -s)"

鍵の追加

ssh-add ~/.ssh/id_ed25519
# Enter passphrase: ****

# 追加済みの鍵を表示
ssh-add -l
# 256 SHA256:xxx... dev@example.com (ED25519)

自動起動(bashrc)

# ~/.bashrc または ~/.zshrc
if [ -z "$SSH_AUTH_SOCK" ]; then
  eval "$(ssh-agent -s)"
  ssh-add ~/.ssh/id_ed25519 2>/dev/null
fi

keychainで永続化

# macOS:Keychainにパスフレーズを保存
ssh-add --apple-use-keychain ~/.ssh/id_ed25519

# Linux:keychainを使用
eval "$(keychain --eval id_ed25519)"

五、踏み台サーバーの設定

シナリオ:ローカル → 踏み台 → 本番サーバー

# ~/.ssh/config
Host jumpbox
  HostName jump.example.com
  User jump-user
  IdentityFile ~/.ssh/id_jump

Host production
  HostName 10.0.1.100
  User deploy
  IdentityFile ~/.ssh/id_prod
  ProxyJump jumpbox
# 本番サーバーへ直接接続(自動的に踏み台経由)
ssh production

Agent転送

Host jumpbox
  HostName jump.example.com
  ForwardAgent yes

セキュリティ上の注意:踏み台サーバーを信頼できる場合のみAgent転送を有効にしてください。


六、セキュリティ強化

サーバー側 sshd_config

# /etc/ssh/sshd_config
PermitRootLogin no                    # rootログインを禁止
PasswordAuthentication no             # パスワードログインを禁止
PubkeyAuthentication yes             # 鍵認証のみ許可
AuthorizedKeysFile .ssh/authorized_keys
MaxAuthTries 3                       # 最大試行回数
LoginGraceTime 30                    # ログインタイムアウト30秒
AllowUsers deploy admin              # ホワイトリストユーザー

鍵のパーミッション

# 秘密鍵は必ず600
chmod 600 ~/.ssh/id_ed25519

# .sshディレクトリは必ず700
chmod 700 ~/.ssh

# authorized_keysは必ず600
chmod 600 ~/.ssh/authorized_keys

鍵のローテーション

# 1. 新しい鍵を生成
ssh-keygen -t ed25519 -C "dev@example.com" -f ~/.ssh/id_ed25519_new

# 2. 新しい公開鍵をすべてのサーバー/Gitプラットフォームにデプロイ
ssh-copy-id -i ~/.ssh/id_ed25519_new.pub user@server

# 3. 新しい鍵が使えることを確認
ssh -i ~/.ssh/id_ed25519_new user@server

# 4. 古い公開鍵を削除
# サーバーの ~/.ssh/authorized_keys を編集し、古い公開鍵の行を削除

# 5. 古い鍵を削除
rm ~/.ssh/id_ed25519_old

七、よくある問題の切り分け

問題 原因 解決策
Permission denied 公開鍵が未デプロイ ssh-copy-id
Too many authentication failures Agent内の鍵が多すぎる IdentitiesOnly yes
Are you sure you want to continue connecting 初回接続 StrictHostKeyChecking=accept-new
接続が遅い DNS逆引き UseDNS no(サーバー側)
鍵が効かない パーミッションエラー 秘密鍵にchmod 600

デバッグモード

ssh -vvv user@server.com
# 接続プロセスの詳細を出力し、認証失敗の原因を特定
#SSH#密钥#Git#免密登录#安全