ローカル環境や本番サーバーなどと Github を連携させる

環境

  • CentOS7
  • git
  • Github

連携

  • 自分のプライベートリポジトリを git clone で取得できるようになる
  • git pull する時にログイン情報を記述しなくて良い

RSAキー生成

  • 作業ユーザーで操作
$ ssh-keygen -t rsa -C "hoge@example.com" ← github で登録したメールアドレス
Enter file in which to save the key (/Home/userName/.ssh/id_rsa): /Home/userName/.ssh/id_rsa
# あとは全てEnterキー
$ cd

# VirtualBox などで連携したい場合はホスト側 (PC側) に .ssh/config を設定する
$ vi .ssh/config

Host github
Hostname github.com
Port 22
User username ← github の ログインする時の Username
IdentityFile ~/.ssh/id_rsa

# 権限を変えないとうまく動かない
$ chmod 0600 .ssh/config

#中身の文字列をコピー
$ cat .ssh/id_rsa.pub

複数サーバー設定したい場合

$ vi .ssh/config

# 1つのファイルに複数記述できる
# ex)

Host github
Hostname github.com
Port 22
User username ← github の ログインする時の Username
IdentityFile ~/.ssh/id_rsa

Host ホスト名
Hostname 192.168.56.101
Port 2022
User ユーザー名
IdentityFile ~/.ssh/id_rsa

Host ホスト名2
Hostname 192.168.56.102
Port 20022
IdentityFile ~/.ssh/id_rsa

GitHub に SSH 鍵を登録 (GitHub側)

会社などで自分のアカウント以外のプライベートリポジトリを使用する際にも
自分のアカウントで以下の手順を行う

Settings → SSH and GPG Keys → New SSH key

  • Title は分かりやすい名前
  • 先程コピーした id_rsa.pub の中身を Key にペースト

関連投稿