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?

ポートフォワーディングとファイアウォールを設定したのにWSL2で稼働中のサーバにアクセスできなかった話

Last updated at Posted at 2025-08-10

はじめに

  • WSL2でNode.jsのExpressサーバーを立ててローカルで動作確認OK
  • 同一ネットワーク内のスマホからアクセスしようとしたが繋がらない問題に遭遇

環境情報

  • Windows 11
  • WSL2(Ubuntu)
  • Node.jsのExpressサーバーを3000番ポートで起動
  • 同一Wi-Fi内のスマホからアクセス予定

問題の詳細

  • Windows側でファイアウォールでポート3000を許可済み

  • netsh interface portproxy でポートフォワーディング設定済み

  • それでもWindowsからWSLのサーバに接続できない(Test-NetConnectionで失敗)

  • WSLのIPにpingは通るがTCP接続はできない状態

原因

  • WSL側のUbuntuでufwが有効でポート3000を許可していなかった

  • WSL2のネットワークはWindowsホストから仮想的に分離されているためファイアウォール設定が必要

解決手順

今回はWindowsのファイアウォール設定およびポートフォワーディングは完了している前提(参考記事)で話を進めます。

  1. WSL内のufwの状態を確認します。

    sudo ufw status
    
  2. ポート3000が許可されていなければ、許可します。

    sudo ufw allow 3000
    
  3. WSLのIPアドレスを確認します。

    ip addr show eth0 | grep inet
    
  4. 接続をテストします。

    • Windows PowerShellで以下を実行して接続を確認。
    Test-NetConnection -ComputerName <WindowsIPアドレス> -Port 3000
    
  5. スマホなどから

    http://<WindowsのIPアドレス>:3000/
    

    にアクセスし動作確認します。

まとめ

  • WSL2ではWindows側とWSL側でそれぞれファイアウォールの設定が必要

  • ポートフォワーディング設定も忘れずに

  • これらが揃うと、スマホなどからもアクセス可能になる

参考リンク

こちらの記事を参考にWSLのポートフォワーディングなどを行いました。
https://qiita.com/y-hys/items/4ddb95e68ba55a31080e

UFWについてはこちらで詳しく解説されています。
https://qiita.com/010Ri/items/f37f7f5fb35265823c7c

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?