3

How to filter all ports except 80, 443, 22, 3306 ports using firewalld on CentOS 7?

I searched unix.stackexchange.com, find this about iptables.

but how to block every port except special ports by firewalld?

5
  • What OS are you running? Jun 22, 2020 at 3:54
  • unixmen.com/iptables-vs-firewalld
    – jsotola
    Jun 22, 2020 at 4:02
  • Updated my Post
    – 244boy
    Jun 22, 2020 at 4:08
  • @jsotola your link do not provide the way to realize it.
    – 244boy
    Jun 22, 2020 at 4:12
  • 1/2 way down the page ... look for Adding Port in Public Zone
    – jsotola
    Jun 22, 2020 at 4:16

1 Answer 1

3

CentOS 7 uses firewalld by default.

If firewalld is enabled and running, then all ports are blocked by default unless they were enable at install (which is usually done with ssh which is port 22 unless it's set to run on another port in /etc/ssh/sshd_config) or enabled by the person managing the system.

Start and enable the service:

systemctl start firewalld
systemctl enable firewalld

Open the ports:

firewall-cmd --permanent --zone=public --add-port=80/tcp
firewall-cmd --permanent --zone=public --add-port=443/tcp
firewall-cmd --permanent --zone=public --add-port=22/tcp
firewall-cmd --permanent --zone=public --add-port=3306/tcp

Reload firewalld

firewall-cmd --reload

To show the open ports:

firewall-cmd --list-ports
1
  • firewall-cmd --permanent ---zone=public --add-port=80/tcp : The zone argument has 3 "-". It must be --zone=public instead of ---zone=public
    – soung
    Oct 23, 2021 at 20:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .