How to open firewall from terminal
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.

/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 &quot;/Applications/Foo.app/Contents/MacOS/Foo&quot;