はじめに
WSL2で、ネットワークがミラーモードだとUSB IPを使ったUSB接続ができない。USBデバイスの接続については、こちらを参照。
現状、私の環境での対策としてはネットワークモードをNATに変更するしかない(2025年1月22日時点)。 ⇒再検証の結果、WSLのUbuntu 20.04の問題であることが判明した(4年ほど前に作成した「Ubuntu」でのみ発生)。新規に、「Ubuntu-20.04」を作成したら解決した(2025年3月8日追記)。
動作確認環境
- Windows 11 23H2
- WSL2
WSL バージョン: 2.4.11.0 カーネル バージョン: 5.15.167.4-1 WSLg バージョン: 1.0.65 MSRDC バージョン: 1.2.5716 Direct3D バージョン: 1.611.1-81528511 DXCore バージョン: 10.0.26100.1-240331-1435.ge-release Windows バージョン: 10.0.22631.4890
- Ubuntu 20.04
- usbip
4.4.0-128+Branch.master.Sha.d52ae5f18f93deb85cd4e1722ca5f812443a2c6d.d52ae5f18f93deb85cd4e1722ca5f812443a2c6d
エラーの確認
WSLでネットワークモードをミラーにする。WSLの設定については、こちらを参照。
[wsl2]
networkingMode=mirrored
USB IPでUSBデバイスをWSL2のUbuntu(Ubuntu 20.04)に接続(アタッチ)する際に、下記のようにファイアウォールのエラーが発生する。私の環境では、ホストのファイアウォールを無効化してもエラーは発生した。
usbipd.exe attach --wsl --busid=2-13 --auto-attach
usbipd: info: Using WSL distribution 'Ubuntu' to attach; the device will be available in all WSL 2 distributions.
usbipd: info: Using IP address 127.0.0.1 to reach the host.
usbipd: warning: A firewall appears to be blocking the connection; ensure TCP port 3240 is allowed.
usbipd: info: Starting endless attach loop; press Ctrl+C to quit.
WSL usbip: error: tcp connect
エラーの原因 (2025年3月8日追記)
私の環境で作成済みの「Ubuntu」(中身はUbuntu20.04)が原因であることが分かった。再度、WSL及びUSBIPを最新版にして、別のUbuntu 18.04/20.04/22.04では、ミラーモードでUSB接続が可能であった。
usbipd attach --busid=2-3 --wsl -a
usbipd: info: Using WSL distribution 'Ubuntu-18.04' to attach; the device will be available in all WSL 2 distributions.
usbipd: info: Detected networking mode 'mirrored'.
usbipd: info: Using IP address 127.0.0.1 to reach the host.
usbipd: info: Starting endless attach loop; press Ctrl+C to quit.
usbipd attach --busid=2-3 --wsl -a
usbipd: info: Using WSL distribution 'Ubuntu-20.04' to attach; the device will be available in all WSL 2 distributions.
usbipd: info: Detected networking mode 'mirrored'.
usbipd: info: Using IP address 127.0.0.1 to reach the host.
usbipd: info: Starting endless attach loop; press Ctrl+C to quit.
WSL Attached
usbipd attach --busid=2-3 --wsl -a
usbipd: info: Using WSL distribution 'Ubuntu-22.04' to attach; the device will be available in all WSL 2 distributions.
usbipd: info: Detected networking mode 'mirrored'.
usbipd: info: Using IP address 127.0.0.1 to reach the host.
usbipd: info: Starting endless attach loop; press Ctrl+C to quit.
WSL Attached
エラーの回避策
私の環境では、ネットワークモードをNATにすると上記エラーは解消された。ミラーモードのままでエラーを回避する方法はまだ見つけられていない。 ⇒NATモードに変更する以外に、新規にUbuntu 20.04を構築すると接続成功した(2025年3月8日追記)。
[wsl2]
networkingMode=NAT
まとめ
WSL2で、ネットワークがミラーモードだとUSB IPを使ったUSB接続ができない事象を紹介した。ネットワークモードをNATに変更することでエラーを回避できる。引き続きミラーモードでUSB接続ができる方法がないか調査を継続する。
⇒再検証の結果、WSLのUbuntu 20.04の問題であることが判明した。新規に、「Ubuntu-20.04」を作成したら解決した(2025年3月8日追記)。
参考