Keep Coding!

A fearless adventure in knowing what to do when no one’s there telling you what to do.

ServersMan@VPS(Ubuntu)に初期設定をした話。(2)

初期設定の続きです。今回は、iptablesの設定を行いました。

参考にしたのは

ServersMan@VPS ファイアウォール設定

[SERVERSMAN@VPS]エントリープランからサーバーを構築する(3)~IPTABLES(ファイヤーウォール)設定~

Ubuntuでiptablesを設定する

iptablesの設定

1.設定の初期化

$ iptables -F

2.自分自身からの入力とPingの受け取り許可

$ iptables -A INPUT -p icmp -j ACCEPT
$ iptables -A INPUT -i lo -j ACCEPT

3.許可するものをそれぞれ入力

$ iptables -A INPUT -p tcp --dport 80 -j ACCEPT
$ iptables -A INPUT -p tcp --dport 25 -j ACCEPT
$ iptables -A INPUT -p tcp --dport 587 -j ACCEPT
$ iptables -A INPUT -p tcp --dport 110 -j ACCEPT
$ iptables -A INPUT -p tcp --dport 443 -j ACCEPT
$ iptables -A INPUT -p tcp --dport 22 -j ACCEPT
$ iptables -A INPUT -p tcp --dport 21 -j ACCEPT
$ iptables -A INPUT -p tcp --dport 53 -j ACCEPT
$ iptables -A INPUT -p tcp --dport  3843 -j ACCEPT
$ iptables -A INPUT -p udp -m udp --dport 60000:61000 -j ACCEPT

4.データの送信に対する応答受信の許可

$ iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

5.デフォルトルールの設定(受信:破棄 送信:許可 通過:破棄)

$ iptables -P INPUT ACCEPT
$ iptables -P OUTPUT ACCEPT
$ iptables -P FORWARD ACCEPT

これで設定は大丈夫、、、のはず。

iptablesの設定の保存

再起動したりすると設定がリセットされてしまうので、起動時に読み込むよう設定する。

1.設定を保存

$ mkdir /etc/iptables
$ iptables-save > /etc/iptables/iptables.rules

2.起動時有効化

/etc/network/if-pre-up.d/iptablesload
を作成

以下をiptablesloadに設定

#!/bin/sh
iptables-restore < /etc/iptables/iptables.rules
exit 0
/etc/network/if-post-down.d/iptablessave を作成

以下をiptablessaveに設定

#!/bin/sh
iptables-save -c < /etc/iptables/iptables.rules
exit 0

3.それぞれに実行権限を付与

sudo chmod +x /etc/network/if-post-down.d/iptablessave
sudo chmod +x /etc/network/if-pre-up.d/iptablesload

おわり

これで基本的には大丈夫そう。