LoginSignup
4
6

More than 3 years have passed since last update.

鍵認証でGitHubに接続する方法

Last updated at Posted at 2019-08-01

GitHubで鍵認証をするための秘密鍵と公開鍵のペアを作成する

まずはローカル環境で公開鍵と秘密鍵のペアを作成します。

$ mkdir ~/.ssh
$ cd ~/.ssh
$ ssh-keygen -t rsa

秘密鍵のパーミッションを変更します。

$ chmod 600 id_rsa

上記のコマンドでid_rsaという秘密鍵とid_rsa.pubという公開鍵を作成することができました。
この鍵のペアをGitHubとの鍵認証に使っていきます。

秘密鍵をローカルに、公開鍵をGitHubに登録する

$ vi ~/.ssh/config

Host github.com
  User git
  Hostname github.com
  IdentityFile ~/.ssh/id_rsa

次に公開鍵をGitHubに登録していきます。

まずは、GitHubのマイページにある『Settings』を選択します。
image.png

『SSH and GPG keys』という項目を選択し、『New SSH key』のボタンを押します。
image.png

すると、以下のようなテキストフィールドが出てくるのでここに公開鍵(今回の例で言えばid_rsa.pub)の中身の文字列をコピペします。

image.png

ここまでできたら公開鍵と秘密鍵のペアの設定が完了です。

以下のコマンドを実行してGitHubとssh接続できるのか確認してみます。

初回のssh接続のときには接続をするかどうかを聞かれるので、yesと答えます。
Hi [ユーザー名]と返信がくればsshで接続ができていることになります。

$ ssh -T git@github.com
The authenticity of host 'github.com (192.30.255.112)' can't be established.
RSA key fingerprint is xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,192.30.255.112' (RSA) to the list of known hosts.
Hi nishina555! You've successfully authenticated, but GitHub does not provide shell access.

さいごに

鍵認証でGitHubに接続することができればユーザー名とパスワードを入力することなくpushを実行することができるようになります。
パスワード認証なしてGitHubにpushする手順は『Gitにパスワード認証なしでPushするまでの手順』で紹介していますので、興味のある方はご覧になってください。

ツイッター(@nishina555)やってます。フォローしてもらえるとうれしいです!

4
6
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
4
6