macOS – How to Open Firewall from Terminal

command linefirewallmacos

I am developing a go app on a server(mac-mini) where I connect via ssh. I restart the app many times: go run main.go

Problem is, that I dont have access to the graphical ui – "Firewall Options…".

I really like to have the firewall on for security reason and poke minimal holes in it only when needed.

Is there any way to set and allow a port to be open, for incoming connections from the terminal?

Best Answer

  1. Configure the firewall to be permissible for selected applications and services.

    To turn the firewall on for specific applications/services :

    sudo defaults write /Library/Preferences/com.apple.alf globalstate -int 1
    

    https://raymii.org/s/snippets/OS_X_-_Turn_firewall_on_or_off_from_the_command_line.html

    Reboot for the change to take effect.

  2. /usr/libexec/ApplicationFirewall/socketfilterfw allows you to configure applications through the firewall. The tool provides its own help and documentation.

    For example, to grant an application incoming connections, you can use

    ./socketfilterfw -t "/Applications/Foo.app/Contents/MacOS/Foo"