LoginSignup
0
0

firewalldの使い方

Last updated at Posted at 2023-12-09

はじめに

OCI で提供されるoracle linux はデフォルトでfirewalldが有効になっており、最初に必ず躓く(躓いた)。
firewalldの使い方を記載。以下、特に記載がなければrootユーザーで実施。

firewalld サービス 起動・停止

# 状態確認
systemctl status firewalld

# 起動
systemctl start firewalld

# 停止
systemctl stop firewalld

firewall-cmd の使い方

・http の service (80/tcp)
・http用の別ポート(8080/tcp)
を許可する手順の例を記載。

http の service (80/tcp) を許可

# 状態確認
[root@my-instance ~]# firewall-cmd --state
running

# デフォルトのzone (以下ではpublic)と、その設定を表示
# 現状sshと dhcpv6-client だけが許可されている
[root@my-instance ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens3
  sources:
  services: dhcpv6-client ssh
  ports:
  protocols:
  forward: no
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:

# アクティブなzoneは以下で確認可能。publicというzoneがデフォルトかつアクティブな状態。
[root@my-instance ~]# firewall-cmd --get-active-zones
public
  interfaces: ens3

# http serviceの概要を確認。これを有効にすると(tcp/80)が許可される
[root@my-instance ~]# firewall-cmd --info-service=http
http
  ports: 80/tcp
  protocols:
  source-ports:
  modules:
  destination:
  includes:
  helpers:

# http serviceをpublic zoneに追加。永続化のためにpermanentオプションが必須。
[root@my-instance ~]# firewall-cmd --add-service=http --zone=public --permanent
success

# reloadして設定を反映
[root@my-instance ~]# firewall-cmd --reload
success

# public zone にhttp serviceが追加された。
[root@my-instance ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens3
  sources:
  services: dhcpv6-client http ssh
  ports:
  protocols:
  forward: no
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:

http用の別ポート(8080/tcp) を許可

# 続いて、個別に8080/tcpをポート指定して追加
[root@my-instance ~]# firewall-cmd --add-port=8080/tcp --zone=public --permanent
success

# reloadして設定を反映
[root@my-instance ~]# firewall-cmd --reload
success

# public zone に8080/tcp portが追加された。
[root@my-instance ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens3
  sources:
  services: dhcpv6-client http ssh
  ports: 8080/tcp
  protocols:
  forward: no
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:

ログの有効化

デフォルトではログ出力はoffになっている。以下で有効化する。

# 
[root@my-instance ~]# firewall-cmd --get-log-denied
off
[root@my-instance ~]# firewall-cmd --set-log-denied=all
success
[root@my-instance ~]# firewall-cmd --get-log-denied
all
[root@my-instance ~]# firewall-cmd --reload
success

ログは/var/log/messagesに表示される。
試しにポートが開いていないtcp/81にcurl してみると、REJECTのログが出るようになる。

[root@my-instance ~]# tail -f 
Dec  9 12:32:51 my-instance kernel: FINAL_REJECT: IN=ens3 OUT= MAC=xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx SRC=xxx.xxx.xxx.xxx DST=10.0.0.130 LEN=52 TOS=0x00 PREC=0x00 TTL=117 ID=65233 PROTO=TCP SPT=35624 DPT=81 WINDOW=64240 RES=0x00 SYN URGP=0

firewalldのログだけ個別に振り分けておくと便利。
メッセージにFINAL_REJECTの文字が含まれていた場合、非同期に(ハイフン-)、/var/log/firewalld.logに書き込み、このログについては以降の振り分けをしない(&stop)。

vi /etc/rsyslog.conf
()
#### RULES ####

# Log all kernel messages to the console.
# Logging much else clutters up the screen.
#kern.*                                                 /dev/console

# add filter of firewalld deniy log   ★
:msg, contains, "FINAL_REJECT:"         -/var/log/firewalld.log
&stop

# Log anything (except mail) of level info or higher.
# Don't log private authentication messages!
*.info;mail.none;authpriv.none;cron.none                /var/log/messages
[root@my-instance ~]# tail -f /var/log/firewalld.log
Dec  9 12:47:47 my-instance kernel: FINAL_REJECT: IN=ens3 OUT= MAC=xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx SRC=106.131.196.247 DST=10.0.0.130 LEN=52 TOS=0x00 PREC=0x00 TTL=117 ID=837 PROTO=TCP SPT=37649 DPT=81 WINDOW=64240 RES=0x00 SYN URGP=0

さいごに

まあfirewalld自体止めたほうが早いという話もある。

0
0
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
0
0