Linuxでkai(準備編) > ネットワーク設定 > iptables設定例

Total: - (Today: - Yesterday: - )
最終更新日 2010/08/20 00:17

  • ここではiptablesによるファイアウォール例をまとめる
  • ファイアウォール設定は 自己責任でお願いします。


1. ルールの設定例

  • ここではiptablesによる設定例を載せる。
  • Kaiの他にPing、SSHを許可する例のつもりだが、設定内容は間違っている箇所があるかもしれない。

1.1. iptablesのルール定義の例

  • ルールを適用する定義ファイルを作成する。
  • このサンプルにはUPnP、30000/udp、34525/udp、34523/udpを定義してあるが、有効になっているのはUPnPのみなことに注意。
  • 試用するときは環境に応じて調整してほしい。
$ touch iptables-up
$ chmod 700 iptables-up
$ vi iptables-up
+ ubuntu / debian / fedora
ubuntu / debian / fedora
#!/bin/sh
# Enter the NETWORK address the Internal Interface is on
INTNET='192.168.100.0/24'
# Enter the iptables command location
IPTABLES=/sbin/iptables # ubuntu / debian / fedora
# Any address
UNIVERSE='0.0.0.0/0'
# Clear any existing rules
$IPTABLES -F # Flush all chains
$IPTABLES -X # Delete all user specified chains
$IPTABLES -Z # Reset all counters
# Set default policies
$IPTABLES -P INPUT DROP
#$IPTABLES -P OUTPUT DROP
$IPTABLES -P OUTPUT ACCEPT
$IPTABLES -P FORWARD DROP
# Allow unlimited traffic on the loopback interface
$IPTABLES -A INPUT -i lo -j ACCEPT
$IPTABLES -A OUTPUT -o lo -j ACCEPT
# Previously initiated and accepted exchanges bypass rule checking
# Allow unlimited outbound traffic
$IPTABLES -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# icmp
$IPTABLES -A INPUT -p icmp --icmp-type echo-request -s $INTNET -j ACCEPT
$IPTABLES -A INPUT -p icmp --icmp-type echo-reply -s $INTNET -j ACCEPT
# XLinkKai 7.4.18/kaiWebUIPort 34522/tcp
$IPTABLES -A INPUT -m tcp -s $INTNET --dport 34522 -p tcp -j ACCEPT
# UPnP 1900/udp
$IPTABLES -A INPUT -m state --state NEW -m udp -s $INTNET --sport 1900 -p udp -j ACCEPT
# XLinkKai 7.4.18/kaiPort 30000/udp
#$IPTABLES -A INPUT -m udp -s $UNIVERSE --dport 30000 -p udp -j ACCEPT
# XLinkKai 7.0.0.7/kaiPort 34525/udp
#$IPTABLES -A INPUT -m udp -s $UNIVERSE --dport 34525 -p udp -j ACCEPT
# XLinkKai 7.0.0.7/kaiDeepPort 34523/udp
#$IPTABLES -A INPUT -m udp -s $UNIVERSE --dport 34523 -p udp -j ACCEPT
# ssh 22/tcp
$IPTABLES -A INPUT -m state --state NEW,ESTABLISHED -m tcp -s $INTNET --dport 22 -p tcp -j ACCEPT
# Drop all other traffic
$IPTABLES -A INPUT -j DROP
$IPTABLES -A FORWARD -j DROP
+ slackware
slackware
#!/bin/sh
# Enter the NETWORK address the Internal Interface is on
INTNET='192.168.100.0/24'
# Enter the iptables command location
IPTABLES=/usr/sbin/iptables # slackware
# Any address
UNIVERSE='0.0.0.0/0'
# Clear any existing rules
$IPTABLES -F # Flush all chains
$IPTABLES -X # Delete all user specified chains
$IPTABLES -Z # Reset all counters
# Set default policies
$IPTABLES -P INPUT DROP
#$IPTABLES -P OUTPUT DROP
$IPTABLES -P OUTPUT ACCEPT
$IPTABLES -P FORWARD DROP
# Allow unlimited traffic on the loopback interface
$IPTABLES -A INPUT -i lo -j ACCEPT
$IPTABLES -A OUTPUT -o lo -j ACCEPT
# Previously initiated and accepted exchanges bypass rule checking
# Allow unlimited outbound traffic
$IPTABLES -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# icmp
$IPTABLES -A INPUT -p icmp --icmp-type echo-request -s $INTNET -j ACCEPT
$IPTABLES -A INPUT -p icmp --icmp-type echo-reply -s $INTNET -j ACCEPT
# XLinkKai 7.4.18/kaiWebUIPort 34522/tcp
$IPTABLES -A INPUT -m tcp -s $INTNET --dport 34522 -p tcp -j ACCEPT
# UPnP 1900/udp
$IPTABLES -A INPUT -m state --state NEW -m udp -s $INTNET --sport 1900 -p udp -j ACCEPT
# XLinkKai 7.4.18/kaiPort 30000/udp
#$IPTABLES -A INPUT -m udp -s $UNIVERSE --dport 30000 -p udp -j ACCEPT
# XLinkKai 7.0.0.7/kaiPort 34525/udp
#$IPTABLES -A INPUT -m udp -s $UNIVERSE --dport 34525 -p udp -j ACCEPT
# XLinkKai 7.0.0.7/kaiDeepPort 34523/udp
#$IPTABLES -A INPUT -m udp -s $UNIVERSE --dport 34523 -p udp -j ACCEPT
# ssh 22/tcp
$IPTABLES -A INPUT -m state --state NEW,ESTABLISHED -m tcp -s $INTNET --dport 22 -p tcp -j ACCEPT
# Drop all other traffic
$IPTABLES -A INPUT -j DROP
$IPTABLES -A FORWARD -j DROP

1.2.ルールのインストール

  • インストール用のスクリプトを作成しておく。
  • ディストリビューションによってスクリプトの内容が異なるので注意。
$ touch iptables_install.sh
$ chmod 700 iptables_install.sh
$ vi iptables_install.sh
+ ubuntu / debian
ubuntu / debian
#!/bin/sh
test -f iptables-up || exit 1
cp iptables-up /etc/network/if-pre-up.d/iptables-up
chown root.root /etc/network/if-pre-up.d/iptables-up
test -f iptables-down || exit 1
cp iptables-down /etc/network/if-post-down.d/iptables-down
chown root.root /etc/network/if-post-down.d/iptables-down
+ fedora
fedora
#!/bin/sh
test -f iptables-up || exit 1
/etc/rc.d/init.d/iptables stop
. ./iptables-up
/etc/rc.d/init.d/iptables save
/etc/rc.d/init.d/iptables start
/sbin/chkconfig iptables on
+ slackware
slackware
#!/bin/sh
test -f iptables-up || exit 1
. iptables-up
/usr/sbin/iptables-save > /etc/iptables.rules
  • ルール定義ファイルをインストールする。
$ sudo ./iptables_install.sh

1.3.その他

  • slackwareの場合、OS起動時にルールを有効化するには/etc/rc.d/rc.localも修正する必要がある
$ sudo vi /etc/rc.d/rc.local
if [ -f /etc/iptables.rules ]; then
    echo "Starting IPTABLES: /etc/iptables.rules"
    /usr/sbin/iptables-restore < /etc/iptables.rules
fi



2.ルールの解除

  • すでに自前のルール定義がある場合、この方法を実行してはいけない。
  • 実行すると通信をすべて許可する状態になるので、注意すること。

2.1. iptablesのルール定義解除の例

  • ルールを解除する定義ファイルを作成しておく。
$ touch iptables-down
$ chmod 700 iptables-down
$ vi iptables-down
+ ubuntu / debian / fedora
ubuntu / debian / fedora
#!/bin/sh
# Enter the iptables command location
IPTABLES=/sbin/iptables # ubuntu / debian / fedora
# Clear any existing rules
$IPTABLES -F # Flush all chains
$IPTABLES -X # Delete all user specified chains
$IPTABLES -Z # Reset all counters
# Set default policies
$IPTABLES -P INPUT ACCEPT
$IPTABLES -P OUTPUT ACCEPT
$IPTABLES -P FORWARD ACCEPT
+ slackware
slackware
#!/bin/sh
# Enter the iptables command location
IPTABLES=/usr/sbin/iptables # slackware
# Clear any existing rules
$IPTABLES -F # Flush all chains
$IPTABLES -X # Delete all user specified chains
$IPTABLES -Z # Reset all counters
# Set default policies
$IPTABLES -P INPUT ACCEPT
$IPTABLES -P OUTPUT ACCEPT
$IPTABLES -P FORWARD ACCEPT

2.2.ルールのアンインストール

  • アンインストール用のスクリプトを作成しておく。
  • ディストリビューションによってスクリプトの内容が異なるので注意。
$ touch iptables_uninstall.sh
$ chmod 700 iptables_uninstall.sh
$ vi iptables_uninstall.sh
+ ubuntu / debian
ubuntu / debian
#!/bin/sh
/etc/network/if-post-down.d/iptables-down
rm /etc/network/if-pre-up.d/iptables-up
rm /etc/network/if-post-down.d/iptables-down
+ fedora
fedora
#!/bin/sh
test -f iptables-down || exit 1
/etc/rc.d/init.d/iptables stop
. ./iptables-down
/etc/rc.d/init.d/iptables save
/sbin/chkconfig iptables off
+ slackware
slackware
#!/bin/sh
test -f iptables-down || exit 1
. iptables-down
/usr/sbin/iptables-save > /etc/iptables.rules
  • ルール定義ファイルをアンインストールする
$ sudo ./iptables_uninstall.sh



3. iptablesの設定確認

  • ルールを変更したら、iptablesの設定を確認しておくこと
  • OS再起動したときにも定義が有効(または無効)になっているかも確認しておくといいだろう。
$ sudo iptables -nvL

以上でiptables設定例は終わり。


コメント

名前:
コメント:
最終更新:2010年08月20日 00:17
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。