CentOS 7 firewalld メモ

Contents


はじめに

CentOS 7 を始めたばかりのときiptablesからfirewalldに置き換わり、コマンド体系も変わるっていうので、とりあえず良く使いそうなコマンドをRHELの公式ドキュメント読んでメモったしりたときの備忘録。説明とかかなり省略。

 

 

firewalldの自動起動

サービスを表す.serviceは省略できます。tab補完ですぐ表示できるので個人的には結構表示させてる。

確認

# systemctl is-enabled firewalld.service
enabled

有効化

# systemctl enable firewalld.service

無効化

# systemctl disable firewalld.service

 

 

firewalldの起動

起動

# systemctl start firewalld.service

確認

# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
 Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
 Active: active (running) since Sat 2018-04-07 06:26:02 JST; 3 days ago
 Docs: man:firewalld(1)
 Main PID: 14679 (firewalld)
 CGroup: /system.slice/firewalld.service
 └─14679 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid

Apr 07 06:26:02 ***** systemd[1]: Starting firewalld - dynamic firewall daemon...
Apr 07 06:26:02 ***** systemd[1]: Started firewalld - dynamic firewall daemon.

 

 

設定ファイル

フォールバック設定

/usr/lib/firewalld/

システム固有の設定

/etc/firewalld/

内容は後述

 

 

対象サービス

利用可能なサービス

# firewall-cmd --get-services
RH-Satellite-6 amanda-client amanda-k5-client bacula bacula-client bitcoin bitcoin-rpc bitcoin-testnet bitcoin-testnet-rpc ceph ceph-mon cfengine condor-collector ctdb dhcp dhcpv6 dhcpv6-client dns docker-registry dropbox-lansync elasticsearch freeipa-ldap freeipa-ldaps freeipa-replication freeipa-trust ftp ganglia-client ganglia-master high-availability http https imap imaps ipp ipp-client ipsec iscsi-target kadmin kerberos kibana klogin kpasswd kshell ldap ldaps libvirt libvirt-tls managesieve mdns mosh mountd ms-wbt mssql mysql nfs nrpe ntp openvpn ovirt-imageio ovirt-storageconsole ovirt-vmconsole pmcd pmproxy pmwebapi pmwebapis pop3 pop3s postgresql privoxy proxy-dhcp ptp pulseaudio puppetmaster quassel radius rpc-bind rsh rsyncd samba samba-client sane sip sips smtp smtp-submission smtps snmp snmptrap spideroak-lansync squid ssh synergy syslog syslog-tls telnet tftp tftp-client tinc tor-socks transmission-client vdsm vnc-server wbem-https xmpp-bosh xmpp-client xmpp-local xmpp-server

 

サービスの設定情報取得

# firewall-cmd --info-service=http
http
 ports: 80/tcp
 protocols:
 source-ports:
 modules:
 destination:

 

サービスの追加・削除

デフォルトテンプレートは以下のディレクトリ配下に保存されている。このファイルは弄らないこと。

/usr/lib/firewalld/services/

サービスの追加変更を行った場合、以下のディレクトリ配下に保存される

/etc/firewalld/services/

 

 

firewall-cmd

  • 設定は即時反映される
  • 設定の永続化は--permanentオプションを追加する(反映にはreloadが必要)

アクティブゾーンと割り当てられているインターフェースの一覧

# firewall-cmd --get-active-zones
public
 interfaces: eth0

 

publicゾーンの全設定を確認

# firewall-cmd --zone=public --list-all
public (active)
 target: default
 icmp-block-inversion: no
 interfaces: eth0
 sources:
 services: ssh dhcpv6-client
 ports:
 protocols:
 masquerade: no
 forward-ports:
 source-ports:
 icmp-blocks:
 rich rules:

 

現在読み込まれているサービス一覧

# firewall-cmd --get-services

 

カスタム作成されまだ読み込まれていないサービスも含む一覧

# firewall-cmd --permanent --get-services

 

パニックモード

すべての送受信パケットの遮断

# firewall-cmd --panic-on

 

すべての送受信パケットの受け渡しを再開

# firewall-cmd --panic-off

 

パニックモードの有効・無効の確認

# firewall-cmd --query-panic
no

 

リロード

ユーザ接続状態を切断せず(状態情報を失わずに) リロード

# firewall-cmd --reload
success

ユーザー接続を切断し、状態情報を破棄してファイアーウォールをリロードする(rootで実行)

# firewall-cmd --complete-reload
success

リロードに関してはRHEL公式に以下のように書かれています。

ファイアーウォールをリロードするには、すべての設定ファイルをリロードし、ファイアーウォール設定全体を再作成する必要があります。リロード中に、安全上の理由により組み込みチェーンのポリシーは DROP に設定され、最終的に ACCEPT に再設定されます。したがって、リロード中にサービスが破棄されることがあります。

コマンドを永続化してすぐに実行する場合はコマンドを--permanentありとなし計2回実行する。(この場合reloadは使わない)

 

ランタイムルールをパーマネントルールに保存

# firewall-cmd --runtime-to-permanent

 

インターフェースをゾーンに追加

# firewall-cmd --permanent --zone=public --add-interface=eth0

 

デフォルトゾーンの変更

オンライン

# firewall-cmd --get-default-zone
public

# firewall-cmd --set-default-zone=public

設定ファイル

# vi /etc/firewalld/firewalld.conf
DefaultZone=public

# firewall-cmd --reload

 

ゾーンにサービスを追加・削除

# firewall-cmd --zone=public --add-service=smtp

# firewall-cmd --zone=public --remove-service=smtp

 

ゾーンで開放されているサービスの一覧表示

# firewall-cmd --list-services
http https

 

ゾーンに対してポートを追加

# firewall-cmd --zone=dmz --add-port=8080/tcp
# firewall-cmd --zone=public --add-port=5060-5061/udp

 

ゾーンで開放されている全ポートの一覧表示

# firewall-cmd --zone=public --list-ports

 

アクセス拒否

dropゾーンに追加するのがセオリーみたい

# firewall-cmd --zone=drop --add-source=10.10.xxx.xxx/32
# firewall-cmd --zone=drop --permanent --add-source=10.10.xxx.xxx/32

 

XMLファイル

XMLファイルを編集してゾーンにサービスを追加・削除する場合、/usr/lib/firewalld/zones/配下のファイルはデフォルトで使用されるため編集しないこと。

/etc/firewalld/zones/配下にXMLファイルをコピーして編集する

# cp /usr/lib/firewalld/zones/test.xml /etc/firewalld/zones/

 

リッチ言語構文

リッチ言語を使うことで複雑なファイアウォールのルールを設定できる

リッチ言語コマンドの追加、削除、確認

firewall-cmd [--zone=zone] --add-rich-rule='rule' [--timeout=timeval]
firewall-cmd [--zone=zone] --remove-rich-rule='rule'
firewall-cmd [--zone=zone] --query-rich-rule='rule'

 

リッチルールコマンドの形式

rule [family="rule family"]
 [ source address="address" [invert="True"] ]
 [ destination address="address" [invert="True"] ]
 [ element ]
 [ log [prefix="prefix text"] [level="log level"] [limit value="rate/duration"] ]
 [ audit ]
 [ action ]

 

例)

# firewall-cmd --permanent --zone=public --add-rich-rule='rule family=ipv4 source address="192.168.xxx.xxx/24" service name="http" log prefix="firewalld" level="notice" limit value="24/h" accept'

# firewall-cmd --permanent --zone=public --add-rich-rule='rule family=ipv4 source address="192.168.xxx.xxx/24" service name="https" log prefix="firewalld" level="notice" limit value="24/h" accept'

# firewall-cmd --reload
success

# firewall-cmd --zone=public --list-all
public (active)
 target: default
 icmp-block-inversion: no
 interfaces: eth0
 sources:
 services:
 ports:
 protocols:
 masquerade: no
 forward-ports:
 source-ports:
 icmp-blocks:
 rich rules:
 rule family="ipv4" source address="192.168.xxx.xxx/24" service name="http" log prefix="firewalld" level="notice" limit value="24/h" accept
 rule family="ipv4" source address="192.168.xxx.xxx/24" service name="https" log prefix="firewalld" level="notice" limit value="24/h" accept

 

ゾーンに設定されているリッチルールの表示

(ランタイム)
# firewall-cmd --list-rich-rules

(パーマネント)
# firewall-cmd --permanent --list-rich-rules

 

ICMP

ICMP一覧表示

# firewall-cmd --get-icmptypes
address-unreachable bad-header communication-prohibited destination-unreachable echo-reply echo-request fragmentation-needed host-precedence-violation host-prohibited host-redirect host-unknown host-unreachable ip-header-bad neighbour-advertisement neighbour-solicitation network-prohibited network-redirect network-unknown network-unreachable no-route packet-too-big parameter-problem port-unreachable precedence-cutoff protocol-unreachable redirect required-option-missing router-advertisement router-solicitation source-quench source-route-failed time-exceeded timestamp-reply timestamp-request tos-host-redirect tos-host-unreachable tos-network-redirect tos-network-unreachable ttl-zero-during-reassembly ttl-zero-during-transit unknown-header-type unknown-option

 

拒否設定

firewall-cmd --zone=public --permanent ¥
--add-icmp-block=destination-unreachable ¥
--add-icmp-block=source-quench ¥
--add-icmp-block=redirect ¥
--add-icmp-block=time-exceeded ¥
--add-icmp-block=parameter-problem ¥
--add-icmp-block=timestamp-request ¥
--add-icmp-block=timestamp-reply ¥
--add-icmp-block=router-advertisement ¥
--add-icmp-block=router-solicitation

確認

# firewall-cmd --list-icmp-blocks --zone=public
または
# firewall-cmd --permanent --list-icmp-blocks --zone=public

ゾーンの適用確認

# firewall-cmd --zone=public --list-all
public (active)
 target: default
 icmp-block-inversion: no
 interfaces: eth0
 sources:
 services: dhcpv6-client ssh
 ports:
 protocols:
 masquerade: no
 forward-ports:
 sourceports:
 icmp-blocks: destination-unreachable parameter-problem redirect router-advertisement router-solicitation source-quench time-exceeded timestamp-reply timestamp-request

 

参考

https://access.redhat.com/documentation/ja-jp/red_hat_enterprise_linux/7/html/security_guide/sec-using_firewalls#sec-Configuring_firewalld
https://nullpopopo.blogcube.info/2015/01/firewalld-deny-tips.html