OpenSSH クライアント確認(必要に応じて有効化)
PowerShellを管理者権限で起動します。OpenSSHクライアントがインストールされているか確認します。
Get-WindowsCapability -Online | ? Name -like 'OpenSSH.Client*'次のように表示されていればインストールされています。
Name : OpenSSH.Client~~~~0.0.1.0
State : Installedインストールされていない場合は、次のコマンドを実行します。
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0SSH 鍵を作成(OpenSSH 形式)
ssh-keygen -t ed25519 -C "あなたのGitHubメール等"実行すると次のような保存先を求めるメッセージが表示されます。
Generating public/private ed25519 key pair.
Enter file in which to save the key (C:\Users\xxxx/.ssh/id_ed25519):ファイル名の変更が不要な場合は、そのままエンターを押下します。
同一のPCで複数のアカウントを使い分ける場合など、必要に応じてファイル名を変更します。
例)C:\Users\xxxx/.ssh/id_ed25519_sub などと任意で追記します。
Generating public/private ed25519 key pair.
Enter file in which to save the key (C:\Users\xxxx/.ssh/id_ed25519):C:\Users\xxxx/.ssh/id_ed25519_sub続いてパスフレーズを求められるので、任意で入力します。
パスフレーズを入力後、確認用に同じパスフレーズを入力します。
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in C:\Users\xxxx/.ssh/id_ed25519
Your public key has been saved in C:\Users\xxxx/.ssh/id_ed25519.pubOpenSSHキーが作成されます。
生成された 公開鍵 は ~\.ssh\id_ed25519.pub で拡張子が.pubとなっています。
コマンド構造の分解
| パート | 意味・役割 |
|---|---|
ssh-keygen | SSH鍵を作成するためのコマンド(OpenSSH 標準ツール) |
-t ed25519 | 鍵の暗号化アルゴリズムに「ED25519(推奨)」を使う指定。RSAより高速かつ安全。 |
-C "あなたのGitHubメール等" | 鍵のコメント欄に GitHub アカウントのメールアドレス等を入れることで、鍵の識別をしやすくします(※実際の機能には影響なし) |
ssh-agent を起動して秘密鍵を登録
管理者権限で下記のコマンドを入力します。
(必要に応じて、実際のファイル名に変更してください。)
Get-Service ssh-agent | Set-Service -StartupType Automatic
Start-Service ssh-agent
ssh-add $env:USERPROFILE\.ssh\id_ed25519パスフレーズを入力してエンターを押下して、秘密鍵を登録します。
A. gh コマンドで追加
公開鍵を GitHub に登録
ファイル名は必要に応じて変更してコマンドを実行します。
gh ssh-key add $env:USERPROFILE\.ssh\id_ed25519.pub --title "My Windows PC" --type authenticationトラブルシューティング:権限エラーになる場合
下記のコマンドで「SSH公開鍵の追加ができる」権限をトークンに付与してから再実行します。
gh auth refresh -h github.com -s write:public_keygh auth refresh は、GitHub CLI(gh)が保持しているアクセストークンの権限(スコープ)を後から追加・更新するコマンドです。-s write:public_key を付けることで、「SSH公開鍵の追加ができる」権限をトークンに付与します。
B. 手動で追加(GUI)
公開鍵をクリップボードにコピーします。ファイル名は必要に応じて変更してください。
Get-Content $env:USERPROFILE\.ssh\id_ed25519.pub | Set-Clipboardそのまま以下の「GitHub – SSH keys 設定ページ」を開き「New SSH key」→ 公開鍵を貼り付けます。
- GitHub – SSH keys 設定ページに紐づけたいアカウントでログインします。
https://github.com/settings/keys

接続テスト
次のコマンドを実行して接続をテストします。
ssh -T git@github.comGitHubで複数のSSH鍵ファイルを使い分ける方法については、こちらをご確認ください。
git@ホスト名 の部分を必要に応じて変更してください。
次のようなコメントが表示されればOpenSSHでの接続が成功です。
Hi <username>! You've successfully authenticated, but GitHub does not provide shell access.GitHub CLI を SSH プロトコルに設定
gh config set -h github.com git_protocol ssh
gh auth setup-git- gh config set -h github.com git_protocol ssh
GitHub CLI(gh)が クローン/プッシュ用の既定プロトコルとして SSH を使うよう、ホストgithub.comに対して設定します。 - gh auth setup-git
Git の credential helper に「gh auth git-credentialを使う」設定を入れて、HTTPS リモートの認証をghトークンで処理できるようにします。
公式ドキュメント
- GitHub Docs: 新しい SSH キーを生成して ssh-agent に追加
https://docs.github.com/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent - GitHub Docs: GitHub アカウントに新しい SSH キーを追加する
https://docs.github.com/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account - GitHub Docs: SSH 接続のテスト
https://docs.github.com/authentication/connecting-to-github-with-ssh/testing-your-ssh-connection - GitHub CLI Manual: gh ssh-key add
https://cli.github.com/manual/gh_ssh-key_add - gh auth setup-git
https://cli.github.com/manual/gh_auth_setup-git - GitHub Pricing
https://github.co.jp/pricing
コメント