SSH基本(鍵・config)
SSHとは
Secure SHell。暗号化通信で別のコンピュータに安全にログインする仕組み。
鍵の作成
推奨: ed25519(高速・安全)
ssh-keygen -t ed25519 -N "" -f ~/.ssh/id_ed25519
-N "" = パスフレーズなし、-f = 保存先
従来: RSA(互換性重視)
ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa
鍵はペア: 秘密鍵(id_ed25519)と公開鍵(id_ed25519.pub)。秘密鍵は絶対に共有しない。
公開鍵を相手に登録
自動コピー(簡単)
ssh-copy-id user@hostname
手動コピー
cat ~/.ssh/id_ed25519.pub | ssh user@hostname "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
~/.ssh/config(接続ショートカット)
Host myserver
HostName 192.168.1.100
User myname
Port 22
IdentityFile ~/.ssh/id_ed25519
Host github
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
これで ssh myserver だけで接続できる
よく使うオプション
| オプション | 意味 | 例 |
-p | ポート指定 | ssh -p 2222 user@host |
-i | 鍵ファイル指定 | ssh -i ~/.ssh/mykey user@host |
-L | ポート転送 | ssh -L 8080:localhost:80 user@host |
-v | デバッグ出力 | 繋がらない時に原因調査 |
ファイル転送(scp)
ローカル → リモート
scp file.txt user@host:/path/to/dest
リモート → ローカル
scp user@host:/path/to/file.txt ./
ディレクトリごと
scp -r ./dir user@host:/path/
つながらない時
| エラー | 原因 | 対処 |
| Connection refused | SSHサーバーが動いてない | 相手側で sshd 起動 |
| Connection timed out | ネットワーク不通 | IP/ポート/ファイアウォール確認 |
| Permission denied | 鍵が登録されてない | ssh-copy-id を実行 |
| Host key verification failed | 初回接続 or 鍵変更 | ssh-keygen -R hostname で削除後再接続 |
← ガイド一覧に戻る