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?

WSLにteratermで接続できるようにした

Posted at

目的

  • wslをteratermで開けるようにする

課題

  • WSLは動的にeth0のIPアドレスを変える
    • WSLは仮想マシンなのでIPアドレスの固定は難しいらしい
    • じゃあ動的にとってしまおう

環境

  • Windows 11
  • WSL バージョン: 2.6.1.0
  • Tera Term 5,4,0

準備

sshできるようにする

  • WSLにsshできるようにOpenSSHをWSLに入れる
sudo apt update
sudo apt install openssh-server

:warning: SSHサーバの起動

  • WSL2 では systemd を使った service ssh start は失敗する(仮想マシンあるある?)
  • 手動でsshdを起動することにする
    • OpenSSH デーモンは privilege separation 用に /run/sshd ディレクトリを必要とする
    • WSL2 では /run が再起動のたびに消えるため、/run/sshd を毎回作る必要がある
# --- WSL SSH サーバー起動(WSL2対応) ---
wsl -d Ubuntu --exec sudo mkdir -p /run/sshd
wsl -d Ubuntu --exec sudo chmod 0755 /run/sshd
wsl -d Ubuntu --exec sudo /usr/sbin/sshd

SSH鍵の設定

Windows側で鍵を生成してWSLのauthorised_keysに保存する

# 鍵の生成
ssh-keygen
# Enterを押し続ける

スクリプト作成

実行しやすさのためバッチファイルでいろいろ実行する。
:warning: ファイルはS-JISで保存する

実行スクリプト

  • このファイルをたたけば起動する
  • 途中で管理者権限が聞かれる
    • ポートフォワードを使用するため
  • 非同期処理にしているがつくりが微妙。改善の余地あり
:: start-wsl-ttl.bat
@echo off
chcp 65001 >nul

echo Running update-wsl-ssh.bat...
:: 1) WSL IP取得&portproxy更新
call "update-wsl-ssh.bat"

:: 2) 少し待つ(portproxy反映用)
timeout /t 6 >nul

:: 3) Tera Term 起動
:: 起動後に別ウィンドウで開くので、バッチはすぐ閉じてもOK
"C:\Program Files (x86)\teraterm5\ttermpro.exe" /m="C:\Users\<WIN-USER名>\Documents\command\connect_wsl.ttl"

pause
exit /b

WSLのIPアドレス取得とポートフォワーディング

  • powershellを管理者権限でたたくためのbatファイル
:: update-wsl-ssh.bat
@echo off
chcp 65001 >nul
:: PS スクリプト実行(PSfolder がバッチと同じ階層にある想定)
powershell -NoProfile -ExecutionPolicy Bypass -File "%~dp0PSfolder\update-wsl-ssh.ps1"

echo.
echo "完了しました。localhost:2222 で接続できるか確認して下さい。"
echo "例: ssh -p 2222 <User>@localhost"
  • 実行箇所の本体
  • フォワーディングはlocalhostの2222番に設定
:: PSfolder\update-wsl-ssh.ps1
# =========================================
# update-wsl-ssh.ps1
# WSL IP取得 + portproxy設定 + 管理者権限昇格
# =========================================

# --- 管理者権限チェック ---
If (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
    $psi = New-Object System.Diagnostics.ProcessStartInfo
    $psi.FileName = "powershell.exe"
    $psi.Arguments = "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`""
    $psi.Verb = "runas"
    try { [System.Diagnostics.Process]::Start($psi) | Out-Null } catch { Write-Error "Administrator privileges are required." }
    exit
}

# --- WSL SSH サーバー起動 ---
Write-Host "Starting WSL SSH service..."
wsl -d Ubuntu --exec sudo mkdir -p /run/sshd
wsl -d Ubuntu --exec sudo chmod 0755 /run/sshd
wsl -d Ubuntu --exec sudo /usr/sbin/sshd

Start-Sleep -Seconds 1

# --- WSL IP取得 ---
$ipRaw = wsl -d Ubuntu --exec hostname -I
if (-not $ipRaw) { Write-Error "Cannot get WSL IP. Make sure WSL is running."; exit 1 }
$wsl_ip = $ipRaw.Split() | Where-Object { $_ -and ($_ -ne "127.0.0.1") } | Select-Object -First 1
Write-Host "WSL IP: $wsl_ip"

# --- 既存の portproxy 削除&追加 ---
netsh interface portproxy delete v4tov4 listenaddress=127.0.0.1 listenport=2222 2>$null
netsh interface portproxy add v4tov4 listenaddress=127.0.0.1 listenport=2222 connectaddress=$wsl_ip connectport=22

Write-Host "Port forward updated: localhost:2222 -> $wsl_ip:22"

Tera Term起動スクリプト

; connect_wsl.ttl
; Tera Term マクロで WSL に SSH 接続

username = '<WSL-USER名>'
hostname = '127.0.0.1'
userkeyfile = 'C:\Users\<WIN-USER名>\.ssh\id_ed25519' ; 秘密鍵を指定
portnum = '2222'

msg = hostname
strconcat msg ':'
strconcat msg portnum
strconcat msg ' /ssh /2 /auth=publickey /user='
strconcat msg username
strconcat msg ' /keyfile='
strconcat msg userkeyfile

connect msg

最後に

  • AIのおかげですぐに完成した
  • Tera Termで開けて何が良いかというと、ログがとれるくらいかな
  • 管理者権限の選択ボタンが出てしまうのは改善点
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?