最好的方法是在中定义一个表并创建一个规则来阻止主机pf.conf:
table <badhosts> persist
block on fxp0 from <badhosts> to any
然后从中动态添加/删除IP地址:
$ pfctl -t badhosts -T add 1.2.3.4
$ pfctl -t badhosts -T delete 1.2.3.4
其他“表”命令包括flush(全部删除)replace和show。查看man pfctl更多。
如果您想要一个更永久的列表,可以将其保存在一个(或多个)文件中。在pf.conf:
table <badhosts> persist file "/etc/badguys1" file "/etc/badguys2"
block on fxp0 from <badhosts> to any
您也可以添加主机名而不是IP地址。见的“表”部分man pf.conf和man pfctl。
注意:上面的示例假定面向Internet的界面为fxp0,请根据您的设置进行更改。另外,请记住,其中的规则pf.conf是按顺序评估的,并且适用于该规则block或pass最后一条适用的规则。有了这个规则集
table <badhosts> persist
block on fxp0 from <badhosts> to any
pass inet tcp from 192.168.0.0/24 to any port 80
然后在badhosts表中添加1.2.3.4和192.168.0.10
$ pfctl -t badhosts -T add 1.2.3.4
$ pfctl -t badhosts -T add 192.168.0.10
来自1.2.3.4和192.168.0.10的所有流量都将被阻止,但是第二个主机将能够建立与其他计算机端口80的连接,因为该pass规则匹配并覆盖了该block规则。