How to Configure macOS Firewall Settings
The macOS Application Firewall controls which applications are allowed to accept incoming network connections. It is different from a traditional port-based firewall.
Enable the Firewall
Go to System Settings > Network > Firewall and toggle it on.
Enable Stealth Mode
Click Options and enable Enable stealth mode to prevent your Mac from responding to ping requests.
Manage App Permissions
Under Options, you can allow or block specific applications from accepting incoming connections.
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --getglobalstate