0
1

GitHubとターミナルのSSH接続設定

Posted at

SSH接続

  • SSH(Secure Shell)とは、リモートコンピュータと通信するためのプロトコル
  • 公開鍵認証方式であるため、パスワードを使用せずセキュアな認証を行える
  • SSH接続をするには、SSHキーの作成とGitHubへの公開鍵の登録が必要

1. SSHキーを作成する

$ ssh-keygen -t ed25519 -C 'GitHubに登録したメールアドレス'
Generating public/private ed25519 key pair.
Enter file in which to save the key (/Users/[user_name]/.ssh/id_ed25519): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /Users/[user_name]/.ssh/id_ed25519
Your public key has been saved in /Users/[user_name]/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx [GitHubに登録したメールアドレス]
The key's randomart image is:
+--[ED25519 256]--+
|           .+=Bo=|
|      .   . +*+*o|
|     E . o ooo*+.|
|      o B   +X.=o|
|       =So  .+* B|
|    . o .. o.  .=|
|     o .  o .  . |
|           .     |
|                 |
+----[SHA256]-----+

(何か聞かれたらエンターを押せばOK)

  • ssh-keygen:SSHキーを生成するためのコマンド
  • -t <type>:キーのタイプを指定するオプション
  • -C <comment>:キーにコメントを追加するオプション

このコマンドを実行すると、ED25519方式のSSHキーが生成され、指定したメールアドレスがコメントとしてキーに追加されます。

2. SSH公開鍵をコピーする

1で生成されたキーは、~/.sshディレクトリ下のid_ed25519(秘密鍵)とid_ed25519.pub(公開鍵)というファイルに保存されています。

$ ls ~/.ssh
id_ed25519  id_ed25519.pub

接続設定に必要な公開鍵をコピーしておきます。

$ pbcopy < ~/.ssh/id_ed25519.pub
  • pbcopy:標準入力からデータを読み取り、クリップボードにコピーするコマンド
  • <(リダイレクト演算子):ファイルの内容をコマンドの標準入力として提供するもの

3. GitHubにSSH公開鍵を追加する

GitHubのヘッダー右にあるプロフィール画像を押下し、その中の「Settings」を押下します。

SSH and GPG keys」を選択し、「New SSH key」を押下します。

スクリーンショット 2024-09-07 20.43.51.png

Title」に任意のキー名を、「Key」に2でコピーした公開鍵を入力し、「Add SSH Key」を押下します。

スクリーンショット 2024-09-07 20.40.09.png

これでSSH接続の準備ができました。

4. SSH接続を確認する

$ ssh -T git@github.com
The authenticity of host 'github.com (XX.XX.XXX.XXX)' can't be established.
ED25519 key fingerprint is SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com' (ED25519) to the list of known hosts.
Hi [ユーザー名]! You've successfully authenticated, but GitHub does not provide shell access.
  • ssh:リモートサーバーにシェルセッションを開き、暗号化された通信を使ってリモート接続をするコマンド
  • -T:主に接続テストや認証確認のために、リモートシェルを開かずに接続するオプション

GitHubのユーザー名が表示されたら接続成功です。

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