Debian GNU/Linux 7.1.0 サーバでファイアウォール iptables を設定する方法についての記録です。
以前古いバージョンでの作業時に残していた記録を参考に作業を行い、今回更新した作業記録を貼付する形式の内容となっています。
0. 前提条件
- Debian GNU/Linux 7.1.0 での作業を想定。
- 接続元のマシンは Linux Mint 14(64bit) を想定。
- IPv4 のみついて対応する。(IPv6 は無効化している)
(「Debian 7 Wheezy - サーバ初期設定!」参照) - ファイアウォールのルールは、取り急ぎ最低限の設定のみ。(必要になった際に追加する)
- iptables をデーモンとして自動起動するツール iptables-persistent を使用する。
(iptables-persistent を使用しない場合は、別途起動スクリプトを用意する必要がある)
1. iptables インストール
iptables と、iptables をデーモンとして自動起動させるツール iptables-persistent を以下のようにしてインストールする。
1
|
|
iptables-persistent のインストールで、「現在の IPv4 ルールを保存しますか?」と問われたら<はい>を選択する。
同様に「現在の IPv6 ルールを保存しますか?」と問われるが、今回は使用しないので<いいえ>を選択する。
2. 既存設定確認
最初に既存の設定を確認してみる。
1 2 3 4 5 6 7 8 9 |
|
3. ルールファイル編集
ルールファイルを以下のように編集する。
SSH のポートは SSH の設定("/etc/ssh/sshd_config")に合わせること。
File: /etc/iptables/rules.v4
1 *filter
2
3 # Allows all loopback (lo0) traffic and drop all traffic to 127/8 that doesn't use lo0
4 -A INPUT -i lo -j ACCEPT
5 -A INPUT ! -i lo -d 127.0.0.0/8 -j REJECT
6
7 # Accepts all established inbound connections
8 -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
9
10 # Allows all outbound traffic
11 # You could modify this to only allow certain traffic
12 -A OUTPUT -j ACCEPT
13
14 # Allows HTTP and HTTPS connections from anywhere (the normal ports for websites)
15 -A INPUT -p tcp --dport 80 -j ACCEPT
16 -A INPUT -p tcp --dport 443 -j ACCEPT
17
18 # Allows SSH connections
19 # THE -dport NUMBER IS THE SAME ONE YOU SET UP IN THE SSHD_CONFIG FILE
20 -A INPUT -p tcp -m state --state NEW --dport 9999 -j ACCEPT
21
22 # Now you should read up on iptables rules and consider whether ssh access
23 # for everyone is really desired. Most likely you will only allow access from certain IPs.
24
25 # Allow ping
26 -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
27
28 # log iptables denied calls (access via 'dmesg' command)
29 -A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7
30
31 # Reject all other inbound - default deny unless explicitly allowed policy:
32 -A INPUT -j REJECT
33 -A FORWARD -j REJECT
34
35 COMMIT
4. iptables-persistent 再起動
設定を反映させるために、iptables-persistent を再起動する。
IPv6 は無効にしているのでスキップされる。
1 2 |
|
5. 設定確認
設定した内容が反映されているか確認してみる。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
参考サイト
以上。