LoginSignup
8
10

More than 1 year has passed since last update.

WSL2で外部からssh接続

Last updated at Posted at 2020-06-27

概要

windows側のIPアドレスを指定してssh接続できるようにする。
wsl2でsshサーバを起動し、外部からそこに接続
を参考にして、設定しました。

ポートフォワード設定

wslのIPアドレスは、Powesrshell(要管理者権限) で 下記で取得可能.

$ wsl hostname -I
172.22.188.123 

ポートフォワード設定は、Powesrshell(要管理者権限)で下記のように設定する。1
trimend() がポイント。入れないと host name -I の最後空白か、なんかでポートフォワードがうまく動作しないみたです。

$ netsh interface portproxy add listenport=22 connectport=22 connectaddres=(wsl hostname -I).trimend() 

タスクスケジューラに登録

起動毎に wsl のIPが変更されるので、起動時に設定できるようにする。

全般

セキュリティオプションを下記のように設定する

  • ユーザがログインしているかどうかにかかわらずに実行する。
  • 最上位特権で実行するにチェック

トリガー設定

  • スタートアップ時
  • 遅延時間30秒 (早すぎると wsl側のIPの設定より先になってしまうみたいです)

image.png

操作の編集

  • プログラム/スクリプト: %SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe
  • 引数: -Command "C:\Users\xxxx\task\set_portproxy.ps1"
set_portproxy.ps1
netsh interface portproxy delete v4tov4  listenport=22
netsh interface portproxy delete v4tov4  listenport=3000
netsh interface portproxy delete v4tov4  listenport=3001
netsh interface portproxy add v4tov4  listenport=22 connectaddress=(wsl hostname -I).trimend() connectport=22
netsh interface portproxy add v4tov4  listenport=3000 connectaddress=(wsl hostname -I).trimend() connectport=3000
8
10
3

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
8
10