技術をかじる猫

適当に気になった技術や言語、思ったこと考えた事など。

Github の git アクセスを維持するために ssh 鍵認証

なぜ?

Github は公式に、Git アクセスのパスワード認証を停止していくと宣言したためです。

github.blog

曰く 2021/8/13 にパスワード認証を廃止するとの事です。

August 13, 2021 – Token (or SSH key) authentication will be required for all authenticated Git operations.

やり方

これは Windows/Mac/Linux 全部共通です。

git-scm.com

Windows に git をインストールした場合、「Git bash」がインストールされています。
Mac/Linux はターミナル開けば ok

f:id:white-azalea:20210113192456p:plain

起動したら $ ssh-keygen -o を叩けばやれます

azale@LAPTOP-T5MQIJ18 MINGW64 ~
$ ssh-keygen -o
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/azale/.ssh/id_rsa):
Created directory '/c/Users/azale/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/azale/.ssh/id_rsa
Your public key has been saved in /c/Users/azale/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:XhR0d0uB6se0C+n9YGJB4mJOktzVqlq74u/+wZg29Zk azale@LAPTOP-T5MQIJ18
The key's randomart image is:
+---[RSA 3072]----+
|         .o . ooo|
|          .o o...|
|         o.o.  . |
|    . o o.+. .   |
|     + =S+o.+ .  |
|      =.B..+++   |
|       O.o.E+o.  |
|     .+ o o.oo.  |
|    .o=*o.    .. |
+----[SHA256]-----+

すると、~/.ssh/id_rsa.pub が生成されます。これが ssh 公開鍵です。
そしたらこの中身を開いてコピーします。

$ cat .ssh/id_rsa.pub
~省略(見せられないよ~)~

Windows だと必要ないかもだけど、chmod 600 ~/.ssh/id_rsa もやっておいて、ほかのユーザから参照できないようにしませう。

github に登録

次のリンクから、「New SSH Key」を指定します。

https://github.com/settings/keys

f:id:white-azalea:20210113201907p:plain

f:id:white-azalea:20210113202103p:plain

コピった内容を貼り付けで OK です。

ssh config の生成

そしてホームディレクトリに .ssh ディレクトリができているので、.ssh/config を追加しましょう。
個人的には vi 使いますが、まぁお好きに。

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

ここまで書いたら保存。

接続確認でござる

$ ssh github
The authenticity of host 'github.com (52.192.72.89)' can't be established.
RSA key fingerprint is SHA256:...

中略

Hi Sunao-Yoshii! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.

successfully authenticated, と出ていれば設定は成功です。