0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【個人開発 #3】WindowsとRocky LinuxからGitHubへSSH接続する

0
Posted at

はじめに

前回WindowsからRocky LinuxへのSSH接続が成功したので、次は両側からGithubへの公開鍵認証でのSSH接続を行います。Githubはアカウント作成、privateのプロジェクトを作成した状態になっており、SSH鍵はまだ登録していません。

参照記事

Windowsからの接続

SSH鍵の作成

PowerShellでSSH鍵の確認、作成を行う

Get-ChildItem ~/.ssh

→ 存在しないため、SSH鍵を作成する

ssh-keygen -t ed25519 -f ~/.ssh/id_git_ed25519 -C "your_email@example.com"
 #~ … ホームディレクトリ
 #-t … 鍵のアルゴリズムのタイプ
 #-f … ファイル
 #-C … 鍵にコメントを付ける(認証には使われず、管理しやすくするためのメモ)

パスフレーズを設定
image.png
 → 失敗

ls ~/.ssh

 → そもそもディレクトリから存在しなかったので作成する

mkdir -p ~/.ssh
ssh-keygen -t ed25519 -f ~/.ssh/id_git_ed25519 -C "your_email@example.com"
 #-p … 存在しない親ディレクトリもまとめて作成し、既に存在していてもエラーにしない

 → またも失敗
絶対パスで実行する

ssh-keygen -t ed25519 -f C:/Users/yourname/.ssh/id_git_ed25519 -C "your_email@example.com"

 → 成功
SSH鍵ファイルが作成されていることを確認する

ls ~/.ssh | ? Name -like "*git*"
     #? … Where-Object のエイリアス

image.png
出力した公開鍵を表示しコピー

cat ~/.ssh/id_git_ed25519.pub

GithubのWeb UIにて、SSH and GPG keysにてコピーした公開鍵をペーストし登録
※参考記事の通り実施しましたので割愛させていただきます
 
.ssh配下に接続先ごとに使用する秘密鍵を設定する config を作成

notepad ~\.ssh\config
 → メモに以下を入力して保存
 ---
Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_git_ed25519
   IdentitiesOnly yes
 ---
 #User git … GithubのSSHサーバで受け付けるUserは「git」固定
 #IdentitiesOnly yes … IdentityFileに登録した以外のファイルを試さない

config.txtとして保存してしまったためRename-Item

Rename-Item ~\.ssh\config.txt ~\.ssh\config

Githubへの接続確認

ssh -T git@github.com
     #-T … 疑似端末(PTY)を割り当てない(認証のみ行う)

問いが続くため答える
image.png
→ 「yes」
image.png
→ パスフレーズを入力
image.png
→ 接続成功
 #「but GitHub does not provide shell access」
 … 「GitHubはシェル(ログイン用の端末)は提供していません」
  → 「git push」などのGit通信のみ受け付ける

Rocky Linuxからの接続

Linuxでも流れは同じ
ディレクトリ確認

ls -la ~/.ssh

SSH鍵作成

ssh-keygen -t ed25519 -f ~/.ssh/id_git_ed25519 -C "your_email@example.com"

公開鍵を表示してコピー

cat ~/.ssh/id_git_ed25519.pub

GithubのWeb UIにて、SSH and GPG keysにてコピーした公開鍵をペーストし登録
※Windowsのものとは別に登録

.ssh配下に接続先ごとに使用する秘密鍵を設定する config を作成
SSHは設定ファイルの権限が緩いと読み込まないことがあるそうで、所有者のみ読み書き可能に設定する

cat > ~/.ssh/config << EOF
Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_git_ed25519
    IdentitiesOnly yes
EOF
chmod 600 ~/.ssh/config

接続確認

ssh -T git@github.com

→ 成功

メモ

Get-ChildItem(ls) から更にファイルを絞りたい

そもそも何の要素で抽出できるの?
→ Get-Member でオブジェクトのプロパティとメソッドを確認する

Get-ChildItem | Get-Member
ls | Get-Member
↓
TypeName: System.IO.FileInfo
Name                      MemberType     Definition
----                      ----------     ----------
LinkType                  CodeProperty   System.String LinkType{get=GetLinkType;}
…
AppendText                Method         System.IO.StreamWriter AppendText()
…
PSChildName               NoteProperty   string PSChildName=XXXXXXXXX
…
Attributes                Property       System.IO.FileAttributes Attributes {get;set;}
…
BaseName                  ScriptProperty System.Object BaseName {get=if ($this.Extension.Length -gt 0){$this.Name.Re...
…
  • Where-Object <プロパティ名> <比較演算子> ~ で抽出
ls | Where-Object name -like "*git*"
  • (Code,Note,Script)Propertyは、Select-Object で表示させることが可能
ls | Select-Object LinkType,PSChildName,BaseName
  • Method は .NET のメソッドで、呼び出すことが可能。ただし、Powershellではオブジェクトの種類を超えて統一的に操作できるコマンドレット(Get-Item,Remove-Itemなど)を使用するのが一般的

感想、次回にむけて

  • スムーズにgithubにつなげられてよかった
  • WindowsとLinuxでほぼ同じコマンドが使えたため、OSが違ってもSSHの仕組み自体は共通であることが実感できた
  • PowerShellがオブジェクトを扱うシェルであることも知ることができ、オブジェクト指向への理解もほんの少しだけ深まった
  • 次はGit通信を試したい
  • 実施した手順をすべて書き出しているが、冗長で見辛い。もっと「何を失敗してどう解決した」だとかの重要な部分にフォーカスして書くようにしていきたい
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?