LoginSignup
0
0

More than 3 years have passed since last update.

firewalldで特定ポートの通信許可(簡易版)

Posted at

目的

CentOS7のfirewalldで、特定のポート/プロトコルを通信許可する設定をすること
※「とりあえずこのポート通したい」などの状況を想定しています

firewalldでの通信制御

firewalldでは、事前に定義されたzoneに対して通信許可/遮断のルールを適用し、そのzoneを各NICに割り当てることで、制御を行います。

zone

デフォルトでpublic, dmzなど9つのゾーンが用意されています。
詳細はこちらを参照ください
firewalldの設定方法(基本設定編)

各zoneの設定は /usr/lib/firewalld/zones/配下にxmlファイルとして配置されています。

publicゾーンのxml(デフォルト設定)を確認する
[root@testvm1 ~]# cat /usr/lib/firewalld/zones/public.xml
<?xml version="1.0" encoding="utf-8"?>
<zone>
  <short>Public</short>
  <description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
  <service name="ssh"/>
  <service name="dhcpv6-client"/>
</zone>

以下のコマンドでもゾーンの設定を確認できます

[root@testvm1 ~]# firewall-cmd --list-all --zone=public
public (active)
  target: default               制御内容(許可/拒否)
※ACCEPT(ルールに適するものを無効、他を許可), DROP(ルールに適するものを許可、他を無効), REJECTがある
※defaultはzoneによって3つのうちのいずれかになるらしい
  icmp-block-inversion: no    icmp-blocksで定義したICMPタイプの許可/拒否が逆になる(デバッグ用)
  interfaces: eth0 eth1 eth2     zoneを設定するNIC
  sources:             許可/拒否する送信元IPアドレス/NW
  services: ssh dhcpv6-client   許可/拒否するサービス(port/protcol)
  ports:              許可/拒否するポート
  protocols:            許可/拒否するプロトコル
  masquerade: no          IPマスカレード(NAPT)の設定ON/OFF
  forward-ports:          ポート変換の設定
  source-ports:          許可/拒否する送信元ポート/プロトコル
  icmp-blocks:           拒否するICMPのタイプ
  rich rules:           より細かいルールの設定

特定ポートの通信を許可する

80/tcpのポートへの通信許可を設定してみます

1.firewalldを起動

[root@testvm1 ~]# systemctl start firewalld
[root@testvm1 ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: active (running) since Sat 2020-01-11 09:23:06 UTC; 6h ago
     Docs: man:firewalld(1)
 Main PID: 5742 (firewalld)
   CGroup: /system.slice/firewalld.service
           mq5742 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid

2.(デフォルトのZoneである)Publicの設定状態を表示(確認)

[root@testvm1 ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: eth0 eth1 eth2
  sources:
  services: ssh dhcpv6-client
  ports:
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:

3.publicゾーンのservicesの設定にhttpを追加

  • zoneと同様に、service一覧は/usr/lib/firewalld/services配下のxmlで確認できます
  • --permanentを指定すると恒久設定(再起動で元に戻らない)となりますが、即時反映はされないので「firewall-cmd --reload」で設定反映が必要となります
[root@testvm1 ~]# firewall-cmd --permanent --zone=public --add-service=http
success
[root@testvm1 ~]# firewall-cmd --reload
success
[root@testvm1 ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: eth0 eth1 eth2
  sources:
  services: ssh dhcpv6-client http
  ports:
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:

以上でとりあえずhttpポートへ通信できるように設定できます

おまけ

servicesの一覧/usr/lib/firewalld/servicesに無いポートを開けたい場合

(80/tcpを開ける場合)
[root@testvm1 ~]# firewall-cmd --permanent --zone=public --add-port=80/tcp
success
[root@testvm1 ~]# firewall-cmd --reload
success
[root@testvm1 ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: eth0 eth1 eth2
  sources:
  services: ssh dhcpv6-client http
  ports: 80/tcp
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:

参考

RedHatページ
  RedHatカスタマポータル「第5章 ファイアウォールの使用」

こちらも参考にさせていただきました
  【すぐわかる】CentOSのポート開放のやり方
  firewalldの設定方法(基本設定編)
  CentOS 7 firewalld よく使うコマンド

ちなみにUDPでの疎通確認はこちら
  UDPの疎通確認はtracerouteよりncが便利

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