目次
GitHubにSSHのkeyを登録する方法とは?
今回は、生成したSSHのkeyをGitHubに登録し、SSH接続でリポジトリにアクセスする方法を紹介します。
GitHubに登録していることが前提ですので、登録しておいてください。また、GitHubにリポジトリも作成しておいてください。
GitHubにSSHのkeyを登録する方法に興味のある方はぜひご覧ください。
環境情報
ここでは、WSL2(Windows Subsystem for Linux 2)のubuntuからGitHubにSSH接続します。
ubuntuのバージョン情報は以下です。
1
2
3
4
5
|
ubuntu:~$ cat /etc/issue
Ubuntu 20.04.1 LTS \n \l
ubuntu:~$ ssh -V
OpenSSH_8.2p1 Ubuntu-4ubuntu0.1, OpenSSL 1.1.1f 31 Mar 2020
ubuntu:~$
|
秘密鍵と公開鍵の生成
ubuntuでSSH接続のための秘密鍵と公開鍵を生成します。
以下のコマンドを実行します。CオプションにはGitHubの登録メールアドレスを指定するのが一般的です。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
ubuntu:~$ ssh-keygen -t rsa -b 4096 -C '${GitHubの登録メールアドレス}'
Generating public/private rsa key pair.
Enter file in which to save the key (/home/${ユーザ名}/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/${ユーザ名}/.ssh/id_rsa
Your public key has been saved in /home/${ユーザ名}/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:GFAH98J1vBlIfSi42E44atJxS3PbMNuMZjb6Yc09MTA ${GitHubの登録メールアドレス}
The key's randomart image is:
+---[RSA 4096]----+
| ..o.ooo+.. |
| . +.oo.= . |
| .+o.E. = |
| . BoB. oo |
| . =.BSX o |
| . + . Xo+. o |
| o =o.o o |
| .. . . |
| .. |
+----[SHA256]-----+
ubuntu:~$
|
~/.sshフォルダに秘密鍵(id_rsa)、公開鍵(id_rsa.pub)が生成されました。
GitHubに公開鍵を登録
作成した公開鍵をGitHubに登録します。以下のURLにアクセスしてください。
https://github.com/settings/keys
「New SSH key」ボタンをクリックし、Titleに何かしらのタイトルを入力します。Keyに公開鍵(id_rsa.pub)の内容を入力します。その後、「Add SSH key」ボタンをクリックします。
SSH接続確認
ubuntuからGitHubにSSH接続の確認をします。以下のコマンドを入力してください。
1
2
3
4
5
6
7
8
|
ubuntu:~$ ssh -T git@github.com
The authenticity of host 'github.com (52.192.72.89)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com,52.192.72.89' (RSA) to the list of known hosts.
Enter passphrase for key '/home/${ユーザ名}/.ssh/id_rsa':
Hi ${ユーザ名}! You've successfully authenticated, but GitHub does not provide shell access.
ubuntu:~$
|
「You’ve successfully authenticated, but GitHub does not provide shell access.」のメッセージが表示されれば成功しています。
.ssh/configファイルを作成すれば、SSHコマンドを簡潔に記述できます。.ssh/configファイルには以下のように記述します。
1
2
3
4
|
Host github github.com
HostName github.com
IdentityFile ~/.ssh/id_rsa
User git
|
再度SSH接続の確認をします。
1
2
3
4
5
|
ubuntu:~$ ssh -T github
Warning: Permanently added the RSA host key for IP address '52.69.186.44' to the list of known hosts.
Enter passphrase for key '/home/${ユーザ名}/.ssh/id_rsa':
Hi ${ユーザ名}! You've successfully authenticated, but GitHub does not provide shell access.
ubuntu:~$
|
git clone
GitHubのリポジトリに対して、SSH接続でcloneする方法を紹介します。GitHubのリポジトリにアクセスし、「Code」のプルダウンをクリックします。接続方式をHTTPSからSSHに変更し、コマンドをコピーします。
コピーしたコマンドをubuntuのコマンドプロンプトに貼り付けます。今回は「test」リポジトリを対象とします。
1
2
3
4
5
6
7
8
9
10
11
12
|
ubuntu:~$ git clone git@github.com:${ユーザ名}/test.git
Cloning into 'test'...
Enter passphrase for key '/home/${ユーザ名}/.ssh/id_rsa':
remote: Enumerating objects: 110, done.
remote: Counting objects: 100% (110/110), done.
remote: Compressing objects: 100% (84/84), done.
remote: Total 110 (delta 6), reused 110 (delta 6), pack-reused 0
Receiving objects: 100% (110/110), 66.03 KiB | 436.00 KiB/s, done.
Resolving deltas: 100% (6/6), done.
ubuntu:~$ ls
test
ubuntu:~$
|
cloneが成功すると、リポジトリのディレクトリが生成されていることが分かります。
git commit→git push
GitHubのリポジトリに対して、commit、pushしてみましょう。ディレクトリを移動してreadme.txtを作成します。
1
2
3
4
5
|
ubuntu:~$ cd test/
ubuntu:~/test$ git config --global user.email "${GitHubの登録メールアドレス}"
ubuntu:~/test$ git config --global user.name "${ユーザ名}"
ubuntu:~/test$ echo "readme" > readme.txt
ubuntu:~/test$ git add readme.txt
|
以下のコマンドでcommitします。
1
2
3
4
|
ubuntu:~/test$ git commit -m 'add readme.txt'
[master 6dcc53f] add readme.txt
1 file changed, 1 insertion(+)
create mode 100644 readme.txt
|
以下のコマンドでpushします。
1
2
3
4
5
6
7
8
9
10
11
12
|
ubuntu:~/test$ git push
Warning: Permanently added the RSA host key for IP address '13.114.40.48' to the list of known hosts.
Enter passphrase for key '/home/${ユーザ名}/.ssh/id_rsa':
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 2 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 280 bytes | 93.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To github.com:${ユーザ名}/test.git
11cc2f5..6dcc53f master -> master
ubuntu:~/test$
|
pushが成功しました。GitHubのリポジトリ画面を更新すると、readme.txtが登録されていることが分かります。
まとめ
いかがでしたでしょうか。生成したSSHのkeyをGitHubに登録し、SSH接続でリポジトリにアクセスする方法を紹介しました。SSH接続でcloneし、commit、pushするまでの手順をコマンドを交えて紹介しました。
ぜひご自身でコマンドを書いて、理解を深めてください。
インフラエンジニア専門の転職サイト「FEnetインフラ」
FEnetインフラはサービス開始から10年以上『エンジニアの生涯価値の向上』をミッションに掲げ、多くのエンジニアの就業を支援してきました。
転職をお考えの方は気軽にご登録・ご相談ください。