Skip to content

Instantly share code, notes, and snippets.

@maiha
Last active September 6, 2020 20:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maiha/52a938f98064d5e897e3372aa539575d to your computer and use it in GitHub Desktop.
Save maiha/52a938f98064d5e897e3372aa539575d to your computer and use it in GitHub Desktop.

TL;DR

  • SSH Client には ssh コマンドが必要 (Windows10 付属のは動く。puttyは使えない)
  • .ssh/config の ProxyCommand には "ssh" でなく "ssh.exe" と書くべし
  • ssh-agent はプロセス起動でなく、サービスとして起動する
  • ssh-agent はバージョンが古くて自動で鍵認証できないので、最新版を自分で入れる必要あり (7.9以上が必要)

Windows10付属のSSH Clientのインストール

.ssh/ に秘密鍵を作る (puttyから流用)

  1. putty genを起動
  2. 秘密鍵を読み込む
  3. 秘密鍵の変換: [Conversions] > [Export OpenSSH key] > ( C:\Users\youruser\.ssh\id_rsa に保存)
  4. .ssh以下を0700(0600): Explore > [プロパティ] > [セキュリティー] > [詳細設定] > [継承の無効化] (自分だけアクセスできる状態にする)

.ssh/config の設定

  • VSCode から設定画面が開ける (configのファイル名ミスが防げる)
  1. run VSCode
  2. run Remote-SSH: Open Configuration File... in the Command Palette (F1)
  3. select the SSH config file you wish to change, and add (or modify) a host entry in the config file
Host name-of-ssh-host-here
    User your-user-name-on-host
    HostName host-fqdn-or-ip-goes-here
    IdentityFile ~/.ssh/id_rsa
    # ProxyCommand C:\Windows\System32\OpenSSH\ssh.exe -W %h:%p {proxy-host}

.ssh/config の設定 (多段ssh)

  • 例: (local) > user1@entrance.example.com:1234 > user2@office.example.com:5678 > user3@dev.example.com
Host *
    StrictHostKeyChecking no
    TCPKeepAlive yes
    ForwardAgent yes

Host entrance
    User user1
    HostName entrance.example.com
    Port 1234
    IdentityFile ~/.ssh/id_rsa

Host office
    User user2
    HostName office.example.com
    Port 5678
    ProxyCommand C:\Windows\System32\OpenSSH\ssh.exe -W %h:%p entrance

Host dev
    User user3
    HostName dev.example.com
    ProxyCommand C:\Windows\System32\OpenSSH\ssh.exe -W %h:%p office

OpenSSH のバージョンアップ

  • Windows10 付属の OpenSSH のバージョンは 7.7 (2020/09現在)
  • 7.9以上でないと、ssh-agent で認証できない (新しい鍵交換アルゴリズムに対応しておらず、弾かれる)
  • githubにある最新は 8.1 なのでこれを入れる (2020/09現在)
> ssh -V
OpenSSH_for_Windows_7.7p1, LibreSSL 2.6.5

> ssh {remote-server}
warning: agent returned different signature type ssh-rsa (expected rsa-sha2-512)
Enter passphrase for key ...
  1. https://github.com/PowerShell/Win32-OpenSSH/releases から最新版の OpenSSH-Win64.zip をダウンロード
  2. 解凍して、フォルダ名を OpenSSH にする
  3. (万一のために)現在versionを保存 C:\Windows\System32 にて OpenSSHOpenSSH-bundled にリネーム (名前は何でも良い)
  4. 2のフォルダを C:\Windows\System32 に移動
  5. cmdでバージョン確認
> ssh -V
OpenSSH_for_Windows_8.1p1, LibreSSL 2.9.2

ssh-agent サービスの設定(初回のみ)

  • プロセスでなくサービスで起動
  1. [Windows管理ツール] > [サービス] > [OpenSSH Authentication Agent] > [プロパティ]
  2. → [自動] > [適用] > [開始] > [OK]

ssh-agent に鍵を追加

> ssh-add -l
The agent has no identities.  

> ssh-add id_rsa
Enter passphrase for id_rsa:
Identity added: id_rsa (id_rsa)

> ssh-add -l
2048 SHA256:...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment