LoginSignup
53
44

More than 5 years have passed since last update.

ssh 複数アカウント設定

Last updated at Posted at 2017-07-16

環境 MacOS

鍵の生成

$ cd ~/.ssh/
$ ssh-keygen -t rsa -C '[メールアドレス]' -f ~/.ssh/id_rsa_[ファイル名]

# 例
# - 個人のGitHub
$ ssh-keygen -t rsa -C 'mail@gmail.com' -f ~/.ssh/id_rsa_github

# - 会社のGitHub
$ ssh-keygen -t rsa -C 'mail@gmail.com' -f ~/.ssh/id_rsa_github_company

# - 個人のGitLab
$ ssh-keygen -t rsa -C 'mail@gmail.com' -f ~/.ssh/id_rsa_gitlab_private

# - 会社のGitLab
$ ssh-keygen -t rsa -C 'mail@gmail.com' -f ~/.ssh/id_rsa_gitlab_company

公開鍵をコピーする

pbcopy < ~/.ssh/id_rsa_[ファイル名].pub

# 例
pbcopy < ~/.ssh/id_rsa_github.pub
pbcopy < ~/.ssh/id_rsa_github_company.pub
pbcopy < ~/.ssh/id_rsa_gitlab_private.pub
pbcopy < ~/.ssh/id_rsa_gitlab_company.pub

設定ファイルでHostを設定する

設定ファイルの場所👇。存在しない場合は新規作成する

~/.ssh/config

設定ファイルに追加する

~/.ssh/config
Host [ホスト名]
  HostName [サービスのドメイン or IPアドレス]
  User [ユーザー名: GitHubの場合は`git`、 Bitbucketの場合はアカウントのユーザ名(ユーザ名が`hello`の場合`hello`)]
  Port 22
  IdentityFile [秘密鍵の場所]
  IdentitiesOnly yes

# 例 個人GitHubの場合
Host github.com
  HostName github.com
  User git
  Port 22
  IdentityFile ~/.ssh/id_rsa_github
  IdentitiesOnly yes

# 例 会社GitHubの場合
Host github.com.company
  HostName github.com
  User git
  Port 22
  IdentityFile ~/.ssh/id_rsa_github_company
  IdentitiesOnly yes

# 例 個人GitLabの場合
Host gitlab.com
  HostName gitlab.com
  User git
  Port 22
  IdentityFile ~/.ssh/id_rsa_gitlab_private
  IdentitiesOnly yes

# 例 会社GitLabの場合
Host git.company.co.jp
  HostName git.company.co.jp
  User git
  Port 2233
  IdentityFile ~/.ssh/id_rsa_gitlab_company
  IdentitiesOnly yes

接続確認

アカウントにssh接続できるか確認する

$ ssh -T [ホスト名]

# 例
# - 個人のGitHub
$ ssh -T github.com
# Hi[username]! You've successfully authenticated, but GitHub does not provide shell access.

# - 会社のGitHub
$ ssh -T github.com.company
# Hi[username]! You've successfully authenticated, but GitHub does not provide shell access.

# - 個人のGitLab
$ ssh -T gitlab.com
# Welcome to GitLab,[username]!

# - 会社のGitLab
$ ssh -T git.company.co.jp
# Welcome to GitLab,[username]!

リポジトリにアクセス

git@[ホスト名」:「サービスのユーザ名]/hello.git

# 例
# - 個人のGitHub
git@github.com:username/hello.git

# - 会社のGitHub
git@github.com.company:username/hello.git

# - 個人のGitLab
git@gitlab.com:username/hello.git

# - 会社のGitLab
git@git.company.co.jp:username/hello.git
53
44
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
53
44