1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

GithubにSSHキー登録する手引き

Last updated at Posted at 2021-11-15

まえがき

GithubのSSHの設定を教える機会が何度かあったので記事として残しておこうと思い筆を取りました.
メモ書き程度ですが利用していだだけたら幸いです.

①公開鍵・秘密鍵を作る

  1. .sshディレクトリに移動
$ cd $HOME/.ssh/
  1. キー生成
$ ssh-keygen -t ed25519
  1. キーの名前を入力
    デフォルトはid_ed25519
    鳩屋敷はid_<サービス名>_ed25519で統一(あくまで一例)
Enter file in which to save the key (/home/hato_yashiki/.ssh/id_ed25519): (ここで入力)

ちなみにpathを含めた命名になるためフルパスで指定すれば始めのcdはいらなかったりする.
入力し終えたらプロンプトが返ってくるまで全部エンターで良し.(公開鍵を使うためのパスワードが設定できる.気になる人は設定してもいいかも)

②公開鍵をGithubに登録

  1. 公開鍵のコピー
    catでさっき作ったキーの公開鍵(拡張子が.pubのファイル)の中身を見て,コピーしておく.
  2. Githubを開く
    Githubで自分のページのSettingsを開き,SSH and GPG keysを開く.
  3. 公開鍵の登録
    New SSH keyボタンを押し,Keyに先ほどコピーした公開鍵を貼り付ける.
    Titleはなくてもいいが,どのPCのどのターミナルのものか,わかるよう命名しておく方が身のため.
    Add SSH Keyを押して登録.

configファイルの設定

Host github.com
    Identityfile ~/.ssh/<さっき作成した秘密鍵(拡張子はいらない)>
    User git

上記の内容を~/.ssh/configに記述してください。vimでもvscodeでもechoでもメモ帳でも何で記述してもOKです。

④SSH疎通の確認

$ ssh -T git@github.com

ちゃんとconfigファイルができていれば

Hi <ユーザー名>! You've successfully authenticated, but GitHub does not provide shell access.

と返ってくるはず.
これでユーザーネームとかパスワードとかトークンとか気にせずに

$ git clone git@github.com:UserName/Repository.git

とかのコマンドが叩けるわけですね.

おまけ

$ git remote get-url origin

を叩くとoriginのURLが取得できる.これがHTTPSのURLだとSSH通信でpushやpullができない.
GithubからSSH用のURLをコピーしてoriginのURLを更新する必要がある.

$ git remote set-url origin <ssh用のURL>

これでpushやpullも問題なく行えるはず.

また別途SSH通信ができない場合はおまけに追記していくかも.

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?