pfctl

PF firewall configuration commands

Supplementary instructions

pfctl command is the configuration command of PF firewall. PF firewall (full name: Packet Filter) is a software system for TCP/ip traffic filtering and network address translation on UNIX LIKE systems. PF can also provide TCP/IP traffic shaping and control, and provides bandwidth control and packet priority set control. PF was originally developed by Daniel Hartmeier, and is now developed and maintained by Daniel and other members of the openBSD team.

PF firewall has many functions. This site only lists some basic configurations.

activation

To activate pf and have it call the configuration file at startup, edit the /etc/rc.conf file and modify the line that configures pf:

pf=yes

Restart the operating system for the configuration to take effect.

PF can also be started and stopped through the pfctl program:

pfctl -e
pfctl -d

Note that this only starts and shuts down PF. It does not actually load the rule set. The rule set is either loaded when the system starts or is loaded separately through a command after PF is started.

Configuration

When the system boots to run PF in the rc script file, PF loads the configuration rules from the /etc/pf.conf file. Note that when the /etc/pf.conf file is the default configuration file, when the system calls the rc script file, it is only loaded as a text file by pfctl and interpreted and inserted into pf. For some applications, additional rule sets can be loaded from other files after the system boots. PF provides enough flexibility for some very well designed Unix programs.

The pf.conf file has 7 sections:

Except for macros and tables, other sections should also appear in this order in the configuration file, although not all sections are necessary for some specific applications.

Empty lines are ignored and lines starting with # are considered comments.

control

After booting, PF can be operated through the pfctl program. Here are some examples:

pfctl -f /etc/pf.conf # Load pf.conf file
pfctl -nf /etc/pf.conf # Parse the file, but do not load it
pfctl -Nf /etc/pf.conf #Load only the NAT rules in the file
pfctl -Rf /etc/pf.conf #Load only the filtering rules in the file
pfctl -sn # Display current NAT rules
pfctl -sr # Display the current filtering rules
pfctl -ss # Display the current status table
pfctl -si # Display filter status and count
pfctl -sa # Display any displayable

For a complete list of commands, see the pfctl man page.