# 郷に入っては郷に従え ## 問題文 | host | user | pass | updated_at | | -- | -- | -- | -- | | `153.127.196.222` | `ubuntu` | `SABJSeLyiBkgSE` | `2019-08-29T22:36:06+09:00` | WEBサーバを運用する部署に所属することになったあなたは、前任者が構築したサーバを引き継ぐことになりました。現在の状態、発生した問題などについて、前任者の報告がありました。 LXCを使用してWEBサーバを構築しました。コンテナではapacheが1234/tcpで起動しています。 LXCホストのiptablesでホスト80/tcpへの通信をコンテナ1234/tcpへ転送するDNATの設定をしました。VNCサーバからLXCホストの80/tcpへアクセスすると無事にWEBサーバの起動を確認できました。 しかし、システムのアップデートをしようと思ったらコンテナからaptコマンドを使用できなくなりました。 どうやら、担当者は少々無理をしてコンテナのWEBサーバを外部に公開したようです。原因の究明と適切な設定を施してほしいです。設定は永続化し、再起動などが行えるようにしてください。 ### ゴール - 外部(VNCサーバ)からLXCホスト80番でWEBページが確認できる(開始時点で閲覧可能だが、トラブルシューティング後も見られるようにすること) - コンテナ内で apt update ができる - 設定の永続化が行われている ### 情報 - 問題サーバー - IPアドレス: `192.168.0.1` - ユーザー名: `admin` - パスワード: `3tRC7llZ` - サービス名:`dnat.service` - コンテナ名:`ubuntu-dnat` - scriptの場所:`/home/admin/dnat.sh` ## 回答 お疲れさまです。大橋です。 問題「郷に入っては郷に従え」の回答をお送りいたします。 ### 原因究明 `sudo iptables -t nat -L --line-numbers`でNATルールを見たところ、以下のように「ホストに到達した80番ポート向けTCPパケットは、すべて10.176.76.106:1234へ宛先変更する」というルールが設定されていました。 ``` Chain PREROUTING (policy ACCEPT) num target prot opt source destination 1 DNAT tcp -- anywhere anywhere tcp dpt:http to:10.176.76.106:1234 Chain INPUT (policy ACCEPT) num target prot opt source destination Chain OUTPUT (policy ACCEPT) num target prot opt source destination Chain POSTROUTING (policy ACCEPT) num target prot opt source destination 1 MASQUERADE all -- 10.176.76.0/24 !10.176.76.0/24 /* generated for LXD network lxdbr0 */ ``` したがって、コンテナ内から80番ポート向けTCPパケットを送信すると、次にホストに到達するため、そこで10.176.76.106:1234へ宛先変更されてしまい、正しい宛先に届かなくなります。 以上により、内部処理でHTTP通信を行う`apt update`がコンテナ内で失敗するようになったと考えられます。 ### 解決 今回の問題は以下のような操作を行い解決いたしました。 - 元のiptablesのルールを削除する - 正しくポートフォワーディングされるようiptablesのルールを追加する - 「ホストに到達した192.168.0.1:80行きTCPパケットは、10.176.76.106:1234へ宛先変更する」というルールを追加します - iptables-persistentを利用し、設定の永続化を行う 以下にそれを実行するコマンドをお送りいたします。 #### コマンド ``` admin@lxc:~$ sudo iptables -t nat -L --line-numbers Chain PREROUTING (policy ACCEPT) num target prot opt source destination 1 DNAT tcp -- anywhere anywhere tcp dpt:http to:10.176.76.106:1234 Chain INPUT (policy ACCEPT) num target prot opt source destination Chain OUTPUT (policy ACCEPT) num target prot opt source destination Chain POSTROUTING (policy ACCEPT) num target prot opt source destination 1 MASQUERADE all -- 10.176.76.0/24 !10.176.76.0/24 /* generated for LXD network lxdbr0 */ admin@lxc:~$ sudo iptables -t nat -D PREROUTING 1 admin@lxc:~$ sudo iptables -t nat -L --line-numbers Chain PREROUTING (policy ACCEPT) num target prot opt source destination Chain INPUT (policy ACCEPT) num target prot opt source destination Chain OUTPUT (policy ACCEPT) num target prot opt source destination Chain POSTROUTING (policy ACCEPT) num target prot opt source destination 1 MASQUERADE all -- 10.176.76.0/24 !10.176.76.0/24 /* generated for LXD network lxdbr0 */ admin@lxc:~$ sudo iptables -t nat -A PREROUTING -d 192.168.0.1/32 -p tcp -m tcp --dport 80 -j DNAT --to-destination 10.176.76.106:1234 admin@lxc:~$ sudo apt-get install iptables-persistent admin@lxc:~$ sudo /etc/init.d/netfilter-persistent save ``` ### 動作の確認 踏み台サーバー・コンテナ内で以下のコマンドを実行し、正しく動作していることを確認しました。 #### 踏み台サーバー ``` ubuntu@ubuntu:~$ curl 192.168.0.1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <!-- Modified from the Debian original for Ubuntu Last updated: 2016-11-16 See: https://launchpad.net/bugs/1288690 --> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Apache2 Ubuntu Default Page: It works</title> <style type="text/css" media="screen"> * { margin: 0px 0px 0px 0px; padding: 0px 0px 0px 0px; } body, html { padding: 3px 3px 3px 3px; ︙ ``` #### コンテナ内 ``` root@ubuntu-dnat:~# sudo apt update Get:1 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB] Hit:2 http://archive.ubuntu.com/ubuntu bionic InRelease Get:3 http://archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB] Get:4 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages [489 kB] Get:5 http://archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB] Get:6 http://security.ubuntu.com/ubuntu bionic-security/main Translation-en [166 kB] Get:7 http://security.ubuntu.com/ubuntu bionic-security/restricted amd64 Packages [4976 B] Get:8 http://security.ubuntu.com/ubuntu bionic-security/restricted Translation-en [2476 B] Get:9 http://security.ubuntu.com/ubuntu bionic-security/universe amd64 Packages [600 kB] Get:10 http://security.ubuntu.com/ubuntu bionic-security/universe Translation-en [200 kB] Get:11 http://security.ubuntu.com/ubuntu bionic-security/multiverse amd64 Packages [4004 B] Get:12 http://archive.ubuntu.com/ubuntu bionic/universe amd64 Packages [8570 kB] Get:13 http://security.ubuntu.com/ubuntu bionic-security/multiverse Translation-en [2060 B] Get:14 http://archive.ubuntu.com/ubuntu bionic/universe Translation-en [4941 kB] Get:15 http://archive.ubuntu.com/ubuntu bionic/multiverse amd64 Packages [151 kB] Get:16 http://archive.ubuntu.com/ubuntu bionic/multiverse Translation-en [108 kB] Get:17 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages [713 kB] Get:18 http://archive.ubuntu.com/ubuntu bionic-updates/main Translation-en [260 kB] Get:19 http://archive.ubuntu.com/ubuntu bionic-updates/restricted amd64 Packages [11.9 kB] Get:20 http://archive.ubuntu.com/ubuntu bionic-updates/restricted Translation-en [4156 B] Get:21 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 Packages [1000 kB] Get:22 http://archive.ubuntu.com/ubuntu bionic-updates/universe Translation-en [307 kB] Get:23 http://archive.ubuntu.com/ubuntu bionic-updates/multiverse amd64 Packages [6636 B] Get:24 http://archive.ubuntu.com/ubuntu bionic-updates/multiverse Translation-en [3556 B] Get:25 http://archive.ubuntu.com/ubuntu bionic-backports/main amd64 Packages [2512 B] Get:26 http://archive.ubuntu.com/ubuntu bionic-backports/main Translation-en [1644 B] Get:27 http://archive.ubuntu.com/ubuntu bionic-backports/universe amd64 Packages [4000 B] Get:28 http://archive.ubuntu.com/ubuntu bionic-backports/universe Translation-en [1856 B] Fetched 17.8 MB in 8s (2321 kB/s) Reading package lists... Done Building dependency tree Reading state information... Done 52 packages can be upgraded. Run 'apt list --upgradable' to see them. ```