Firewall configuration in CentOS7
1. Basic usage of firewall
1. Start the firewall
[root@localhost ~]# systemctl start firewalld.service
2. Turn off the firewall
[root@localhost ~]# systemctl stop firewalld.service
3. Restart the firewall
[root@localhost ~]# systemctl restart firewalld.service
4. Reload the firewall
[root@localhost ~]# systemctl reload firewalld.service
5. View firewalld service status
[root@localhost ~]# 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 Fri 2020-08-07 04:48:14 EDT; 1min 19s ago
Docs: man:firewalld(1)
Process: 1860 ExecReload=/bin/kill -HUP $MAINPID (code=exited, status=0/SUCCESS)
Main PID: 1764 (firewalld)
CGroup: /system.slice/firewalld.service
└─1764 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid
Aug 07 04:48:13 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon...
Aug 07 04:48:14 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.
Aug 07 04:48:14 localhost.localdomain firewalld[1764]: WARNING: AllowZoneDrifting is enabled. This is considered an inse... now.
Aug 07 04:48:48 localhost.localdomain systemd[1]: Reloading firewalld - dynamic firewall daemon.
Aug 07 04:48:48 localhost.localdomain systemd[1]: Reloaded firewalld - dynamic firewall daemon.
Aug 07 04:48:48 localhost.localdomain firewalld[1764]: WARNING: AllowZoneDrifting is enabled. This is considered an inse... now.
Hint: Some lines were ellipsized, use -l to show in full.
6. Disable the firewall at boot
[root@localhost ~]# systemctl disable firewalld.service
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
7. Enable firewall at boot
[root@localhost ~]# systemctl enable firewalld.service
Created symlink from /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service to /usr/lib/systemd/system/firewalld.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/firewalld.service to /usr/lib/systemd/system/firewalld.service.
Two, configure firewall-cmd
1. View help
[root@localhost ~]# firewall-cmd --help
Usage: firewall-cmd [OPTIONS...]
General Options
-h, --help Prints a short help text and exists
-V, --version Print the version string of firewalld
-q, --quiet Do not print status messages
Status Options
--state Return and print firewalld state
--reload Reload firewall and keep state information
--complete-reload Reload firewall and lose state information
--runtime-to-permanent
Create permanent from runtime configuration
--check-config Check permanent configuration for errors
............
2. Check the version
[root@localhost ~]# firewall-cmd --version
0.6.3
3. Display status
[root@localhost ~]# firewall-cmd --state
running
4. Show all open ports
[root@localhost ~]# firewall-cmd --list-port
3306/tcp
5. Update firewall rules
[root@localhost ~]# firewall-cmd --reload
success
6. View firewall rules
[root@localhost ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: dhcpv6-client ssh
ports: 3306/tcp
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
7. Open port 3306
[root@localhost ~]# firewall-cmd --permanent --add-port=3306/tcp
success
[root@localhost ~]# firewall-cmd --reload
success
[root@localhost ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: dhcpv6-client ssh
ports: 8080/tcp 3306/tcp
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
Description:
(1) –permanent: means the setting is persistent;
(2) reload: reload the firewall.
8. Remove port 3306
[root@localhost ~]# firewall-cmd --permanent --remove-port 3306/tcp
success
[root@localhost ~]# firewall-cmd --reload
success
[root@localhost ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: dhcpv6-client ssh
ports: 8080/tcp
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
Description:
(1) –permanent: means the setting is persistent;
(2) reload: reload the firewall.