LoginSignup
2
0

More than 3 years have passed since last update.

configファイルでSSH接続を管理

Last updated at Posted at 2021-03-29

はじめに

新しいパソコンの購入やパソコンの切り替え際に、再セットアップを必要あると思います。
そのとき前のPCで行った初期設定は、なかなか思い出せないです。
一番困るのはsshキーの管理です。理由は個人と会社のGitHub, Bitbucket, GitLabとサーバー(AWS, Azure, GCP)などのアカウントに繋がるキー複数あるからです。
sshキー管理について、覚書として纏めます。

~/.ssh/config を分ける

個人と会社用ssh通信を以下のように分ける。

.ssh
├── conf.d
│   ├── personal
│   │   ├── config
│   │   ├── id_rsa
│   │   ├── id_rsa.pub
│   │   └── keys
│   │       └── *.pem
│   └── company
│       ├── config
│       ├── id_rsa
│       ├── id_rsa.pub
│       └── keys
│           └── *.pem
├── config
└── known_hosts

各ファイルに記述しているものを載せていきます。

# ~/.ssh/config
Include ~/.ssh/conf.d/**/config

Host *
  ServerAliveInterval 300
  AddKeysToAgent yes

※各フォルダのconfig例は後ほどで設定します。

SSHの秘密鍵・公開鍵を生成

個人用(Personal)

必要な情報

- Default Github Default Gitlab
SSH Key Name id_rsa_github id_rsa_gitlab
email name.github@example.com name.gitlab@example.com

キーの作成

ssh-keygen -f "~/.ssh/conf.d/personal/id_rsa_github" -t rsa -b 4096 -C "name.github@example.com"
ssh-keygen -f "~/.ssh/conf.d/personal/id_rsa_gitlab" -t rsa -b 4096 -C "name.gitlab@example.com"

会社用(Organization)

必要な情報

- Default Github Default Gitlab
SSH Key Name id_rsa_github_companyName id_rsa_gitlab_companyName
email name.github@company.com name.gitlab@company.com

キーの作成

ssh-keygen -f "~/.ssh/conf.d/company/id_rsa_github_companyName" -t rsa -b 4096 -C "name.github@company.com"
ssh-keygen -f "~/.ssh/conf.d/company/id_rsa_gitlab_companyName" -t rsa -b 4096 -C "name.gitlab@company.com"

SSH接続の設定をする

公開鍵を各アカウント(Github, Gitlab)に登録する

  1. 各アカウントのブラウザを開き、Settings->SSH and GPG keysに行きます。
  2. SSH keysのNew SSH keyをクリックします。
  3. Titleに適当な名前を付けます。
  4. 下のKeyに公開鍵を貼り付ける。

SSHキーをSSH-Agentに追加

# Run ssh-agent
ssh-agent bash

# Add the personal keys
ssh-add ~/.ssh/conf.d/personal/id_rsa_github
ssh-add ~/.ssh/conf.d/personal/id_rsa_gitlab

# Add the organisation keys
ssh-add ~/.ssh/conf.d/company/id_rsa_github_companyName
ssh-add ~/.ssh/conf.d/company/id_rsa_gitlab_companyName

# Confirm
ssh-add -l

各configを作成

個人用(Personal)のconfig

~/.ssh/conf.d/personal/config

Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/conf.d/personal/id_rsa_github

Host gitlab.com
    HostName gitlab.com
    User git
    IdentityFile ~/.ssh/conf.d/personal/id_rsa_gitlab

Host AWS
    HostName 12.123.1.123
    User ec2-user  
    Port 22  
    IdentityFile ~/.ssh/conf.d/personal/keys/awskey.pem 
    TCPKeepAlive yes
    Identitiesonly yes

会社用(Organization)のconfig

~/.ssh/conf.d/company/config

Host companyname.github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/conf.d/company/id_rsa_github_companyName

Host companyname.gitlab.com
    HostName gitlab.com
    User git
    IdentityFile ~/.ssh/conf.d/company/id_rsa_gitlab_companyName

Host companynameAWS
    HostName 12.123.1.123
    User ec2-user  
    Port 22  
    IdentityFile ~/.ssh/conf.d/company/keys/awskey.pem
    TCPKeepAlive yes
    Identitiesonly yes

説明

キーワード 内容
Host ホスト名
HostName ホスト名またはIPアドレス
User ログインユーザ名
IdentityFile ログインするための秘密鍵のパス
Port ポート番号(デフォルトは22)
TCPKeepAlive 接続状態を継続したい場合:yes 継続しない場合:no
IdentitiesOnly IdentityFileが必要な場合:yes 必要ない場合:no
ServerAliveInterval 一定期間サーバからデータが送られてこないときに、タイムアウトする秒数。(例) 120

接続の確認

ssh -T git@github.com
# 成功結果
# Hi [Username]! You've successfully authenticated, but GitHub does not provide shell access.

ssh -T git@gitlab.com

ssh AWS

ssh companynameAWS
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0