久しぶりにログインしたら鍵が爆発してたのでメモ
ENV
- OS: Mac
- Date: 2023/02
1. 鍵の生成
FYI: 新しい SSH キーを生成して ssh-agent に追加する - GitHub Docs
- デフォルトの鍵にして使いまわしたくないので鍵名はgithub.id_rsaに
step1
# 生成
$ ssh-keygen -t ed25519 -C "your_email@example.com" -f ~/.ssh/github.id_rsa
# 生成を確認
$ ls -l ~/.ssh/github.id_rsa*
-rw------- **** ~/.ssh/github.id_rsa
-rw-r--r-- **** ~/.ssh/github.id_rsa.pub
補足
- ex. ディレクトリ作成(必要なら)
$ mkdir -m 700 ~/.ssh
-
github.id_rsa
- 秘密鍵、流出させてはならない
-
github.id_rsa.pub
- 公開鍵
-
-t ed25519
- 鍵生成アルゴリズム(エドワーズ曲線デジタル署名アルゴリズム - Wikipedia)
- OpenSSHが古いと使えない、アップデート推奨、できないなら
-t rsa -b 4096
を
2. 鍵をgithubに登録
SSH and GPG keys -> [New SSH key]をクリック
- title
-
$ hostname
の実行結果でも入れておく、他に適切な名前があればそれで
-
- Key type
- Authentication Key
- 通常はこっちでOK、最初から選択(default)
- Signing Key
- Authentication Key
- Key
- 公開鍵をコピーして貼り付け
step2
$ cat ~/.ssh/github.id_rsa.pub | pbcopy
[Add SSH Key]をクリック
3. SSH configの設定
1で作成した公開鍵をgithubで利用するよう設定
step3
$ cat << EOT >> .ssh/config
Host github.com
HostName github.com
IdentityFile ~/.ssh/github.id_rsa
EOT
# 追記を確認
$ tail -3 .ssh/config
Host github.com
HostName github.com
IdentityFile ~/.ssh/github.id_rsa
補足
- うまく設定できていない時
$ git clone git@github.com:****.git
Cloning into '****'...
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.