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でLXDのネットワークを設定する。

Last updated at Posted at 2025-04-09

LXDでポートフォワードを設定して、外部からアクセスできるようにしたときのメモ

ホストのipアドレスとデバイスを調べる

$ ip route show
default via 172.31.160.1 dev eth0 proto kernel
10.215.99.0/24 dev lxdbr0 proto kernel scope link src 10.215.99.1
172.31.160.0/20 dev eth0 proto kernel scope link src 172.31.166.99

windows上でのWSLのipアドレスが172.31.166.99でデバイスがeth0であることがわかる。

ゲストのipアドレスを調べる

$ lxc list
+-------+---------+----------------------+-----------------------------------------------+-----------+-----------+
| NAME  |  STATE  |         IPV4         |                     IPV6                      |   TYPE    | SNAPSHOTS |
+-------+---------+----------------------+-----------------------------------------------+-----------+-----------+
| ehime | RUNNING | 10.215.99.253 (eth0) | fd42:a480:4d2e:6fce:216:3eff:fe6a:5671 (eth0) | CONTAINER | 0         |
+-------+---------+----------------------+-----------------------------------------------+-----------+-----------+

lxdのゲストマシンのipアドレスが10.215.99.253であることがわかる。

ポートフォワードを設定

$ sudo iptables -t nat -A PREROUTING -p tcp -d  172.31.166.99  -i eth0 --dport 80 -j DNAT --to-destination 10.215.99.253:80

以上でWINDOWSのブラウザから172.31.166.99にアクセスすることにより画面の確認ができる。

起動時の設定

iptablesの設定は、再起動すると消えてしまうので、起動時に自動設定するようにする。

設定のエクスポート

$ sudo iptables-save > iptables_setting

起動時のスクリプトの作成

startup.sh
#!/bin/bash

iptables-restore < /root/iptables_setting

実行できるようにする。

$ chmod +x startup.sh

systemdの設定

/etc/systemd/systemにsystemdのファイルを作成する

startup.service
[Unit]
Description=My Custom Start up Service
After=network.target

[Service]
ExecStart=/root/startup.sh
Restart=no

[Install]
WantedBy=multi-user.target

サービスを登録して実行してみてエラーがなけれは登録完了

# systemctl enable startup.service
# systemctl start startup.service
# systemctl status startup.service
0
0
1

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?