CentOS 7.x 系でiptablesを利用する

2023-03-06

CentOS 7 の「iptables」について

CentOS 7系では、F/Wは「firewalld」に変わっています。CentOS 6系利用した「iptables」を利用したい場合は、firewalld を停止してiptables をインストールして利用します。

「firewalld」と「iptables」の主な違いは以下のような違いがあります

  • iptablesでは、設定を反映させるために、サービスの再起動が非通用でしたが、firewalld では停止することなく設定を反映できます
  • iptablesでは、TCP/IPの仕組みを理解しないと設定がややこしが、firewalld では比較的にTCP/IP の理解がなくても設定が可能となっています
  • firewalldでは、一時的なルールと永続的なルールを管理できて一時的なルールのみを有効にできますが、iptables ではそのようなルールはありません

CentOS 7 の「iptables」のインストールについて

まずは、CentOS 7のインストールされている「firewalld」を停止して、自動起動を停止します

1.firewalld を停止します

$ sudo systemctl stop firewalld

2.firewalld の自動起動を停止します

$ sudo systemctl disable firewalld

3.firewalld を停止したら、iptables をインストールします

$ sudo yum install iptables-services

4.iptables を起動します

$ sudo systemctl start iptables

5.iptables を自動起動するように設定します

$ sudo systemctl enable iptables

6.iptablesの設定を入れていきます。
以下では、iptabelsの設定を初期化してから、サーバーから送信する通信を許可し、特定の以下のポートのみ受信可能としています

  • ping(icmp)
  • http(80)
  • https(443)
  • ftp(21)
  • ssh(22)
# iptables 初期化
$ sudo /sbin/iptables -F INPUT
$ sudo /sbin/iptables -F OUTPUT
$ sudo /sbin/iptables -F FORWARD

# 受信を破棄 / 送信を許可 / 通過を拒否
$ sudo /sbin/iptables -P INPUT DROP
$ sudo /sbin/iptables -P OUTPUT ACCEPT
$ sudo /sbin/iptables -P FORWARD DROP

# ローカルの通信を全て許可
$ sudo /sbin/iptables -A INPUT -i lo -j ACCEPT
$ sudo /sbin/iptables -A OUTPUT -o lo -j ACCEPT

# 内部から行ったアクセスに対する外部からの返答アクセスを許可
$ sudo /sbin/iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# ping(icmp)許可
$ sudo /sbin/iptables -I INPUT -p icmp --icmp-type 0 -j ACCEPT
$ sudo iptables -I INPUT -p icmp --icmp-type 8 -j ACCEPT

# ftp許可
$ sudo /sbin/iptables -A INPUT -p tcp --dport 21 -j ACCEPT
# ftp許可
$ sudo /sbin/iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# HTTPポート(80)を許可
$ sudo /sbin/iptables -A INPUT -p tcp --dport 80 -j ACCEPT
# HTTPSポート(443)を許可
$ sudo /sbin/iptables -A INPUT -p tcp --dport 443 -j ACCEPT

7.設定をへんこうしたら設定内容を確認して、設定内容に間違いがなければ「iptabels save」してからiptables を再起動します

# 設定内容を確認します
$ sudo iptables -nL
Chain INPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           icmp type 8 
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           icmp type 0 
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:21 
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:22 
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:80 
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:443 
DROP       all  --  0.0.0.0/0            0.0.0.0/0           

Chain FORWARD (policy DROP)
target     prot opt source               destination         
DROP       all  --  0.0.0.0/0            0.0.0.0/0           

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0   

# 変更した内容を保存します
$ sudo service iptabels save

# iptables を再起動します
$ sudo systemctl start iptables

 

スポンサーリンク

0
0

LinuxCentOS 7

Posted by admin