SSH configファイルとは
以下のような役割を持つ
- SSH 接続時の複雑なコマンドライン引数を簡素化
- SSH 設定の一元管理
使用例:
Host my-server
HostName 192.168.1.100
User admin
Port 2222
IdentityFile ~/.ssh/id_rsa_server# 設定前:複雑なコマンド
ssh -i ~/.ssh/id_rsa_server -p 2222 admin@192.168.1.100
# 設定後:シンプルなコマンド
ssh my-serverGitHub 向け
個人と仕事用のGitHubアカウントを使い分けたい、といった場合に設定ファイルでそれぞれ異なるSSH鍵の指定などが可能
GitHub SSH URLの構造
git@github.com:username/repository.gitユーザー名: git
ホスト名: github.com
# 個人用GitHub
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_personal
IdentitiesOnly yes
# 仕事用GitHub
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_work
IdentitiesOnly yes使用する際は以下のように使い分ける:
# 個人用
git clone git@github-personal:myname/personal-blog.git
# 仕事用
git clone git@github-work:company/work-project.git設定ファイルの書き方
参考: https://man.openbsd.org/ssh_config.5
記法的な構造:
# コメント行
Host [パターン名]
設定項目1 値1
設定項目2 値2
設定項目3 値3
Host
SSH接続時のエイリアス(別名)を定義するもの。
使用できるパターンの記法はドキュメントのPATTERNSを参照。
HostName
接続先サーバーのアドレスを指定
Host production-web
HostName prod-web-01.company.com
# IPアドレスも可能
# HostName 203.0.113.15
Port
SSH接続に使用するポート番号を指定
Host secure-server
HostName server.example.com
Port 2222 # デフォルトの22以外のポート
User
SSH接続時のユーザー名を指定
Host web-server
HostName web.example.com
User webmaster
IdentityFile
認証に使用するSSHの秘密鍵のパスを指定
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_work
IdentitiesOnly
SSH-agent の鍵を思考せず、IdentityFileのみを使用する
SSH-agentに複数の鍵が登録されている場合、自動的に全ての鍵を施行しようとするためそれを避けられる
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_work
IdentitiesOnly yes