この内容は古いバージョンです。最新バージョンを表示するには、戻るボタンを押してください。
バージョン:11
ページ更新者:guest
更新日時:2026-06-11 07:12:00

タイトル: Linux(Mac)クライアントからLinuxサーバーへのSSH鍵接続
SEOタイトル: Linux/Mac → Linux サーバ SSH 鍵認証完全ガイド(ssh-keygen / ssh-copy-id / config)

この記事の要点
  • 鍵生成: ssh-keygen -t ed25519 -C "mail@example.com"。秘密鍵 ~/.ssh/id_ed25519、公開鍵 id_ed25519.pub
  • 公開鍵をサーバへ: ssh-copy-id user@host 一発で ~/.ssh/authorized_keys に追記
  • 権限: ~/.ssh は 700、authorized_keys は 600。これを誤ると鍵認証が拒否される
  • ~/.ssh/config で Host エイリアス(Host prod)、鍵ファイル指定、ProxyJump も
  • ssh-agent でパスフレーズを 1 回入力すれば以後セッション中再入力不要、ForwardAgent で踏み台越しも可

SSH 鍵認証の仕組み(30 秒で理解)

SSH 鍵認証は非対称暗号を使った認証方式です。クライアント側に秘密鍵、サーバ側に公開鍵を置くと、サーバが乱数を投げ、クライアントが秘密鍵で署名して返し、サーバが公開鍵で検証 → ログイン許可、という流れです。パスワード認証より遥かに強固で、辞書攻撃の被害を受けません。

STEP 1: クライアントで鍵ペア生成

# 推奨: Ed25519(短い、速い、安全)
ssh-keygen -t ed25519 -C "you@example.com"

# RSA を使う場合(互換性重視)
ssh-keygen -t rsa -b 4096 -C "you@example.com"

# 対話内容:
# Enter file in which to save the key (~/.ssh/id_ed25519): [Enter]
# Enter passphrase (empty for no passphrase): ********
# Enter same passphrase again:                ********

# ファイル名を変えたい場合
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_work -C "work@example.com"

生成されるファイル:

ファイル用途権限
~/.ssh/id_ed25519秘密鍵(絶対渡さない)600
~/.ssh/id_ed25519.pub公開鍵(サーバに配布する)644

パスフレーズの是非

  • 付ける: ノートPC が盗難 / 紛失したときの最後の砦。実務では必須
  • 付けない: CI / cron / 自動デプロイ用。代わりに鍵ファイルへのアクセスを厳しく制限

STEP 2: 公開鍵をサーバへ配布

方法 A: ssh-copy-id(簡単)

# 最初の 1 回はパスワード認証で接続して鍵を入れる
ssh-copy-id user@server.example.com

# 鍵を指定する場合
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server.example.com

# 別ポートの場合
ssh-copy-id -p 2222 user@server.example.com

# サーバ側で起きていること:
# 1. ~/.ssh ディレクトリが無ければ作成(700)
# 2. ~/.ssh/authorized_keys に公開鍵を追記(600)

方法 B: 手動でコピー

# クライアント: 公開鍵を表示してコピー
cat ~/.ssh/id_ed25519.pub
# ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI... you@example.com

# サーバ側で(パスワード認証で接続後)
mkdir -p ~/.ssh
chmod 700 ~/.ssh
echo "ssh-ed25519 AAAAC3... you@example.com" >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys

# 所有者がユーザ自身であることを確認
chown -R $(whoami):$(whoami) ~/.ssh

STEP 3: 接続テスト

# 鍵認証で接続
ssh user@server.example.com

# verbose で何が起きているか確認
ssh -v user@server.example.com

# 詳細表示中に
# Authentications that can continue: publickey,password
# Offering public key: ~/.ssh/id_ed25519
# Server accepts key

# パスフレーズ入力を求められたら鍵認証成功
# パスワード入力を求められたら鍵認証失敗 → 後述のトラブルシュート

権限まわりのトラブル

SSH は権限が緩いと鍵を信用しません。サーバ側で次を満たすこと:

パス権限意味
~ (ホーム)755 以下(他人書込み不可)ホームに書き込めると改ざんリスク
~/.ssh700本人のみアクセス
~/.ssh/authorized_keys600本人のみ読み書き
~/.ssh/id_ed25519 (クライアント)600秘密鍵
~/.ssh/id_ed25519.pub644公開鍵
# 一括修正
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub ~/.ssh/known_hosts

~/.ssh/config でエイリアス管理

サーバが増えてくると ssh -p 2222 -i ~/.ssh/key user@host のような長いコマンドを覚えるのは現実的でない。~/.ssh/config でエイリアス化:

# ~/.ssh/config
Host prod
    HostName prod.example.com
    User ubuntu
    Port 22
    IdentityFile ~/.ssh/id_ed25519
    IdentitiesOnly yes

Host stg
    HostName stg.example.com
    User ubuntu
    IdentityFile ~/.ssh/id_ed25519_stg

# 踏み台経由
Host internal
    HostName 10.0.1.50
    User admin
    ProxyJump bastion

Host bastion
    HostName bastion.example.com
    User ops
    IdentityFile ~/.ssh/id_ed25519_bastion

# ワイルドカード
Host *.example.com
    User myuser
    IdentityFile ~/.ssh/id_ed25519

# 全ホスト共通
Host *
    ServerAliveInterval 60
    ServerAliveCountMax 3
    AddKeysToAgent yes
    UseKeychain yes        # macOS のみ

これで ssh prod だけで接続できます。scp file prod:/tmp/rsync prod:/var/log/ ./ も同様に使えます。

ssh-agent でパスフレーズ入力を 1 回に

# エージェント起動
eval "$(ssh-agent -s)"

# 鍵を追加(パスフレーズ入力)
ssh-add ~/.ssh/id_ed25519

# 登録済鍵を確認
ssh-add -l

# 全削除
ssh-add -D

# macOS の場合(Keychain に保存)
ssh-add --apple-use-keychain ~/.ssh/id_ed25519
# 以後、再ログインしてもパスフレーズ不要

# ~/.ssh/config に書いて自動追加
# AddKeysToAgent yes

ForwardAgent(踏み台越しに鍵を共有)

踏み台サーバ経由で別サーバへ繋ぐとき、踏み台にも秘密鍵を置くのは危険。ForwardAgent で手元の ssh-agent を踏み台越しに使えます:

# ~/.ssh/config
Host bastion
    HostName bastion.example.com
    User ops
    ForwardAgent yes      # ★

Host private-app
    HostName 10.0.1.100
    User ubuntu
    ProxyJump bastion
# 接続
ssh private-app

# 踏み台上の git clone なども手元の鍵で OK
# bastion$ git clone git@github.com:org/private.git

注意: ForwardAgent を許可した踏み台が乗っ取られると鍵を悪用される。信頼できる踏み台でのみ使う。代替として OpenSSH 7.3+ の ProxyJump (-J) なら踏み台に鍵を渡さずに済みます。

鍵を増やす運用

シーン推奨
個人開発全部鍵 1 個でも可
仕事 / 個人を分ける鍵 2 個 + config で振り分け
本番 / ステージ / 開発環境ごとに別鍵
CI / 自動デプロイパスフレーズ無し専用鍵 + 専用ユーザ + command= 制限

authorized_keys でコマンド制限

# サーバ側 ~/.ssh/authorized_keys
# 特定コマンドのみ許可、ポートフォワード禁止
command="/usr/local/bin/deploy.sh",no-port-forwarding,no-X11-forwarding ssh-ed25519 AAAA... ci-bot

サーバ側の sshd_config 推奨設定

# /etc/ssh/sshd_config
Port 22
PermitRootLogin no                  # root 直接ログイン禁止
PasswordAuthentication no           # 鍵認証のみに(鍵設定後)
ChallengeResponseAuthentication no
PubkeyAuthentication yes
AllowUsers ubuntu deploy            # 接続許可ユーザ限定
MaxAuthTries 3
LoginGraceTime 30
ClientAliveInterval 300
ClientAliveCountMax 2

# 反映
# sudo systemctl restart sshd

# ★ 設定変更前に必ず別セッションで動作確認してから sshd を restart

トラブルシューティング

症状原因対処
鍵認証なのにパスワード聞かれるサーバ側 authorized_keys の権限 / 内容サーバの /var/log/auth.log 確認
Permission denied (publickey)該当ユーザの authorized_keys に公開鍵が無い再投入 / scp で再送
UNPROTECTED PRIVATE KEY FILE秘密鍵の権限が緩いchmod 600 ~/.ssh/id_ed25519
Host key verification failedサーバ側鍵変更(再構築・MITM 警告)ssh-keygen -R host で known_hosts から削除
Too many authentication failuresagent に登録済鍵が多すぎて全部試して失敗IdentitiesOnly yes + 鍵明示

FAQ

Q: Ed25519 と RSA、どっちを使うべき?
A: 新規は Ed25519 一択。短くて速くて安全。古いサーバ(OpenSSH 6.4 未満)に繋ぐ必要があれば RSA 4096。

Q: 秘密鍵を別マシンに移したい
A: scp ~/.ssh/id_ed25519* newhost:~/.ssh/。コピー後に新マシンで権限 600 を確認。または新マシンで鍵を作って公開鍵だけサーバに追加する方法も。

Q: GitHub にも同じ鍵を使ってよい?
A: 技術的には可。セキュリティ的には用途別に分けるのが望ましい。~/.ssh/configHost github.com に専用鍵を指定。