LoginSignup
0
0

More than 1 year has passed since last update.

GitHub用のSSH keys作成と設定

Last updated at Posted at 2023-02-03

久しぶりにログインしたら鍵が爆発してたのでメモ

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

2. 鍵をgithubに登録

SSH and GPG keys -> [New SSH key]をクリック

  • title
    • $ hostnameの実行結果でも入れておく、他に適切な名前があればそれで
  • Key type
  • 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.
0
0
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
0
0