Skip to content

SSH と鍵認証

💡 学習ガイドgit push のたびにパスワードを入力していませんか?サーバーに接続すると毎回「Permission denied」と表示される?この章では 5 分で SSH 鍵認証の仕組みを理解し、GitHub とサーバーへのパスワードなしログインをワンコマンドで設定する方法を説明します。


0. これらの状況に遭遇したことがあるでしょう

  • git push のたびにパスワード入力ダイアログが表示される——非常に煩わしい
  • SSH でサーバーに接続できず、id_rsaid_ed25519 が何なのか分からない
  • 「公開鍵」と「秘密鍵」という言葉を聞いたことがあるが、どちらを共有し、どちらを保管するのかが不明

核心的な問題:パスワードは安全ではない上に不便。SSH 鍵はセキュリティと利便性を同時に解決するソリューションです。


1. パスワード vs 鍵:なぜ鍵の方が優れているのか

👇 試してみよう:パスワードログインと鍵ログインの違いを比較

SSH Key Authentication: Your Digital IDSymmetric vs asymmetric encryption · key pair generation · authentication flow
🔑
Password login
1Enter username and password
2Password is sent to the server over the network
3Server checks whether the password is correct
4You must enter the password every time
⚠️The password travels over the network and may be intercepted
🔐
Key login
1Put the public key on the server in advance
2Send identity when connecting, without sending the private key
3Server asks a mathematical challenge with the public key
4Your private key answers locally and only sends the answer
The private key never leaves your computer
Core idea:SSH key login is safer than passwords because the private key never travels over the network and cannot be stolen by a man-in-the-middle.

💡 一言でまとめると

パスワードログイン = 毎回パスワードを送信して相手に検証させる(パスワードが傍受される可能性あり); 鍵ログイン = 「鍵を持っていること」を証明するが、鍵自体は見せない(秘密鍵は一切転送されない)。


2. 非対称暗号:公開鍵と秘密鍵

SSH 鍵は非対称暗号に基づいており、一度に 2 つの鍵が生成されます:

秘密鍵 (Private Key)公開鍵 (Public Key)
保存場所あなたのコンピュータ ~/.ssh/id_ed25519サーバー / GitHub
共有できるか❌ 絶対にしない✅ 自由に共有
機能署名(身元を証明)署名検証(身元を確認)
例え錠前

一般的な鍵の種類

種類コマンド推奨度備考
Ed25519ssh-keygen -t ed25519⭐⭐⭐最新、最速、最も安全
RSAssh-keygen -t rsa -b 4096⭐⭐互換性は良いが、やや遅い
ECDSAssh-keygen -t ecdsa通常は非推奨

3. 実践:SSH 鍵の生成と設定

3.1 鍵ペアの生成

bash
ssh-keygen -t ed25519 -C "your@email.com"

実行後にプロンプトが表示されます:

  • ファイルパス:Enter を押してデフォルトパス ~/.ssh/id_ed25519 を使用
  • パスフレーズ:追加の保護を設定可能(空欄でも可)

3.2 公開鍵を GitHub に追加

bash
# 1. 公開鍵の内容をコピー
cat ~/.ssh/id_ed25519.pub | pbcopy  # macOS
cat ~/.ssh/id_ed25519.pub | xclip   # Linux

# 2. GitHub → Settings → SSH and GPG keys → New SSH key を開く
# 3. 公開鍵を貼り付けて保存

# 4. 接続テスト
ssh -T git@github.com
# 成功メッセージ:Hi username! You've been authenticated...

3.3 公開鍵をサーバーに追加

bash
# 方法 1:ssh-copy-id(推奨)
ssh-copy-id user@your-server

# 方法 2:手動コピー
cat ~/.ssh/id_ed25519.pub | ssh user@server "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"

4. SSH Config:長いコマンドに別れを告げる

~/.ssh/config でエイリアスを設定——一度設定すればずっと使えます:

Host dev
  HostName 192.168.1.100
  User deploy
  IdentityFile ~/.ssh/id_ed25519

Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519

設定後の効果:

設定前設定後
ssh -i ~/.ssh/id_ed25519 deploy@192.168.1.100ssh dev
毎回 IP とユーザー名を覚える必要があるエイリアスを 1 つ覚えるだけで OK

5. よくある問題のトラブルシューティング

問題原因解決策
Permission denied (publickey)公開鍵がサーバーに追加されていないssh-copy-id user@server
WARNING: UNPROTECTED PRIVATE KEY FILE秘密鍵ファイルの権限が広すぎるchmod 600 ~/.ssh/id_ed25519
Could not resolve hostnameSSH Config の設定ミス~/.ssh/config の形式を確認
GitHub がまだパスワードを要求するSSH ではなく HTTPS を使用しているgit@github.com:user/repo.git に変更

6. まとめ

📚 重要ポイント

  1. 鍵 > パスワード:秘密鍵は一切転送されないため、パスワードよりはるかに安全
  2. Ed25519 を推奨:最もモダンな鍵アルゴリズム、高速で高いセキュリティ
  3. 公開鍵は自由に共有、秘密鍵は絶対に漏洩させない:この黄金ルールを覚えておく
  4. SSH Config:エイリアスを一度設定すれば、以降は ssh エイリアス で即座に接続
  5. GitHub/GitLab:公開鍵を追加すれば、git push/pull でパスワード入力が不要に

次のステップ