LoginSignup
19
22

More than 5 years have passed since last update.

iptablesセットアップ用のスクリプトを作成する

Last updated at Posted at 2013-01-08

毎回オプションを綺麗に忘れるのでメモ。

iptables.sh
#!/bin/bash

# チートシート
# -F, --flush [chain]: 選択したチェーンのルールを消去する
# -P, --policy chain target: 選択したチェーンのポリシーを指定する
# -A, --append chain rule-specification: 選択したチェーンにルールを追加する
#
# -j, --jump target: ターゲットを指定する
# -p, --protocol protocol: プロトコルを指定する
# -i, --in-interface name: 入力インターフェースを指定する
# -o, --out-interface name: 出力インターフェースを指定する
# -m matchname
# --dport, --destination-port port[:port]: 送信先ポートを指定する
# --sport, --source-port port[:port]: 送信元ポートを指定する

# ルールを初期化する
iptables -F

# 外部からのアクセスは拒否, 自分からのアクセスは許可する
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT

# ループバックインターフェースは許可する
iptables -A INPUT -i lo -j ACCEPT

# セッション確立後のアクセスは許可
iptables -A INPUT -m state --state ESTABLISHED,RELATED --jump ACCEPT


# サービス用のポートを開放する
iptables -A INPUT -p tcp --dport 22 -j ACCEPT   # SSH
iptables -A INPUT -p tcp --dport 25 -j ACCEPT   # SMTP
iptables -A INPUT -p tcp --dport 80 -j ACCEPT   # HTTP
iptables -A INPUT -p tcp --dport 110 -j ACCEPT  # POP3
iptables -A INPUT -p tcp --dport 143 -j ACCEPT  # IMAP
iptables -A INPUT -p tcp --dport 443 -j ACCEPT  # HTTPS
iptables -A INPUT -p tcp --dport 1194 -j ACCEPT # OpenVPN
iptables -A INPUT -p tcp --dport 3306 -j ACCEPT # MySQL
iptables -A INPUT -p tcp --dport 8080 -j ACCEPT # HTTP Alternate

# pingを許可する
iptables -I INPUT -p icmp --icmp-type 0 -j ACCEPT
iptables -I INPUT -p icmp --icmp-type 8 -j ACCEPT
19
22
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
22