さくらのVPSの標準OSのubuntuにはファイアウォール設定が入っている

久しぶりにさくらのVPSを使うことになり、ubuntu 16.04を入れて、 apache2を入れてみたところ、ブラウザからアクセスできない。 これは変だと思って、次の手順で探ってみた。

  • apache2のログを見てみたところ、アクセスログはない。
  • tcpdump port 80で見たところ、パケットは到達している。

はて?何かデフォルトが変わったか。 ここまででネットワークかファイアウォールがあやしいと思った。 カスタムインストールを使ってubuntu16.04をインストールしてみたところ、 普通にアクセスできる。あれれれ? ここまでで、OSをなんとかすればつながるということがわかった。 というわけで元の標準OSのubuntu16.04を入れ直し、 iptables -Lを見たところ、ガッツリ入っている。これか。。。。

root@web3:~# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
f2b-sshd   tcp  --  anywhere             anywhere             multiport dports ssh
ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
ACCEPT     icmp --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh
REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Chain f2b-sshd (1 references)
target     prot opt source               destination
RETURN     all  --  anywhere             anywhere

なんでインストール直後なのにこんなに入っているの? と思い、ufwかと思ったら、ufwのChainではなかった。 次にiptables-persistentかと思ったら、インストールされていない。

grep -r iptables /etc | grep -v fail2ban

を実行してみたところ、

root@web3:~# grep -r iptables /etc | grep -v fail2ban
/etc/default/ufw:# only enable if using iptables backend
/etc/network/if-pre-up.d/iptables:iptables-restore < /etc/iptables/iptables.rules
/etc/init/ufw.conf:# The Uncomplicated Firewall is a front-end for iptables, to make managing a

むむむ。なんだこのif-pre-up.dの設定は。

dpkg -S /etc/network/if-pre-up.d/iptables

を実行しても、パッケージにないとおっしゃる。これはどこから湧いてきたのか。 さくらVPSのイメージで勝手にやっているのか??? /etc/iptables/iptables.rulesの中身は次のようになっていた。

root@web3:~# cat /etc/iptables/iptables.rules
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

とりあえず、改造しないとやっていけないので、設定を変更する。 せっかくなので削除せずに使うことにする。

-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT

この次の行に追加する。

-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT

これでapache2へのアクセスができるようになる。

sh /etc/network/if-pre-up.d/iptables

こちらのコマンドで再読込。 iptables -L で見てみると、

root@web3:~# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
ACCEPT     icmp --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:http
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:https
REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

となっていて、httpとhttpsが追加されていました。 これで良い方法なのかどうかはわかっていないのですが、とりあえずできました。