Reset PF Firewall Automatically While Testing Configuration With Remote Server Over SSH Session

by Vivek Gite on May 13, 2009 · 3 comments

I'd like to tell my BSD PF firewall to flush out the current configuration every 2 minutes. This will help when I'm testing a new rules and configuration options. Some time I find myself locked out of my own remote server. How do I reset PF firewall automatically without issuing hard reboot?

There is no need to write a shell script and call it from cron. You can load the rules from the /etc/pf.conf and sleepf or 120 seconds then disable pf:
#/sbin/pfctl -f /etc/pf.conf && sleep 120 && /sbin/pfctl -d
Where,

  • -f /etc/pf.conf - Load the rules contained in /etc/pf.conf.
  • -d - Disable the packet filter.
  • sleep 200: The sleep command suspends execution for a minimum of 200 seconds before calling the next command.

You can also test pf.conf for syntax errors:
# /sbin/pfctl -nf /etc/pf.conf
Finally, && (AND list) shell control operator is to used run next command only if, first command returns an exit status of zero. So each command in list must be successful in order to run next command.
# /sbin/pfctl -nf /etc/pf.conf && /sbin/pfctl -f /etc/pf.conf && sleep 120 && /sbin/pfctl -d
See - How To Reset Linux Firewall Automatically While Testing Configuration With Remote Server Over SSH Session

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

{ 3 comments… read them below or add one }

1 gregf May 13, 2009

This is great, although you might already have a working rule set. In which case you wouldn’t want to take down the whole firewall over a mistake. My working method has been about the same as above, but I keep new rules in there own file pf.testing. Then you can do the 120 second delay to load the original set if things went wrong. This way there is no open hole(s) in your firewall while your testing. Obviously it’s highly unlikely you’ll have an attack the moment you bring the firewall down. I just think it’s better practice if there is a choice.

Reply

2 eigenheit June 23, 2009

Thinking about it the command -nf is very useful in testing new rules, you are always kept behind your trustful gateway.

Reply

3 Nilesh January 17, 2011

Saviour!

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 9 + 10 ?
Please leave these two fields as-is:
Solve the simple math so we know that you are a human and not a bot.




Previous post:

Next post: