How can I open a port (not application) in the macOS Sierra firewall? I am trying to follow up on this thread and to update it for macOS Sierra (and probably all versions since Yosemite). 

In that thread, a way to open a single port in the OS's firewall was suggested: 

sudo ipfw add 40000 allow tcp from any to any dst-port portnum


However, since Yosemite, ipfw was replaced with pf and, unless I am mistaken, pfctl should therefore be used. 

What would the above command translate to?
To open a port you would use the following line(s):

pass in proto tcp from any to any port 40000
pass in proto tcp from any to any port 80
pass in proto {tcp, udp} from any to any port 53
pass in from any to any port 3601


To quick-start pf and open a port with one of the lines above use e.g.:

echo "pass in proto tcp from any to any port 40000" | sudo pfctl -ef -


To test the rule(s) use: sudo pfctl -s rules

After a reboot you would have to enter the command again, because pf is not started automatically.

By default the pf.conf file has no blocking all rule. So even after starting pf with sudo pfctl -e nothing is blocked.

If you don't want to dive deeply into pf/pf.conf/pfctl, I recommend to use a pfctl GUI like the free Murus Lite to configure pf.