Sub topic 6
Enabling the macOS firewall
This chapter assumes you can open System Settings, run commands with sudo in Terminal, and have administrator access to your Mac. You should also know the basic idea from the previous chapter: a firewall controls incoming network connections, not everything a computer does on the network.
By the end you will be able to turn the firewall on in System Settings and from Terminal. You will also know what the macOS application firewall filters, and how to check that it is running.
What the macOS firewall actually is
The built-in firewall is an application firewall. Most network firewalls decide by port number and address, for example "block TCP port 22." The macOS firewall decides by program. When another computer tries to connect to your Mac, the firewall asks which application would receive the connection and whether that application is allowed to accept it.
Three facts follow from this design:
- It filters incoming connections only. Your Mac can still start connections to any server. A malicious app that calls out to the internet is not stopped by this firewall.
- It works per application, not per port. You allow or block
Example.app, not "port 8080." - It is off by default on a fresh macOS install. You have to turn it on.
macOS also contains a lower-level packet filter called pf. That is a separate system and is not what the Firewall settings pane controls. This chapter covers only the application firewall.
Why bother, if most Macs sit behind a router
A home router usually performs network address translation (NAT), which blocks unsolicited connections from the internet. That covers one situation. It does not help on a café, hotel, airport, or campus network, where other devices share your local network and can reach services your Mac is running. Those services include file sharing, screen sharing, a development web server, or an app that listens for connections without you noticing.
The firewall limits which programs on your Mac can accept those connections. It cannot protect you from phishing, malicious downloads, or a program you chose to run.
Turning the firewall on in System Settings
The location depends on your macOS version. These steps match macOS Ventura and later:
- Open System Settings.
- Choose Network in the sidebar.
- Select Firewall.
- Switch the Firewall toggle on. You may be asked for your administrator password or Touch ID.
On macOS Monterey and earlier, the setting is in System Preferences under Security & Privacy, on the Firewall tab, after clicking the lock to make changes.
After you enable it, the Options button becomes available. Options holds the settings covered in the next sections.
The Options panel
Click Options in the Firewall pane. You will see a list of applications and several switches.
| Setting | What it does | Typical use |
|---|---|---|
| Block all incoming connections | Refuses all incoming connections except those needed for basic internet services such as DHCP, Bonjour, and IPsec. Sharing services stop working. | Untrusted public networks |
| Automatically allow built-in software to receive incoming connections | Lets Apple-signed system software accept connections without prompting. | Leave on for most users |
| Automatically allow downloaded signed software to receive incoming connections | Lets apps signed with a valid developer certificate accept connections without prompting. | Leave on unless you want to approve every app |
| Enable stealth mode | Makes the Mac ignore probing requests such as ICMP ping, and not answer connection attempts on closed ports. | Public networks, when you do not need to be discoverable |
Stealth mode and per-app rules are covered in detail in the next chapter. This chapter focuses on turning the firewall on and confirming it works.
How the firewall decides
The following diagram shows the decision for one incoming connection once the firewall is on.
The prompt in the last branch is the dialog you see when an app asks to accept incoming network connections. Choosing Allow adds the app to the list with "Allow incoming connections." Choosing Deny adds it with "Block incoming connections."
Enabling the firewall from Terminal
The firewall command-line tool is socketfilterfw. Apple does not document it extensively, but it is stable and widely used in management scripts. First check the current state, which needs no administrator rights:
/usr/libexec/ApplicationFirewall/socketfilterfw --getglobalstateThe output is a single line. On current macOS versions it reads like Firewall is disabled. (State = 0) or Firewall is enabled. (State = 1). The exact wording can differ slightly between releases, so scripts should match on the words "enabled" or "disabled" rather than comparing the whole string.
To turn the firewall on, run:
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --setglobalstate onThe command prints a confirmation, and the System Settings toggle changes to match. Use off in place of on to reverse it. The change takes effect immediately and persists across restarts. macOS stores the configuration in /Library/Preferences/com.apple.alf.plist, but you should change it through socketfilterfw or System Settings, not by editing the file.
A script that enables the firewall only when needed
When you manage several Macs, or just want a repeatable setup, a script that checks first avoids unnecessary changes. This one must run as root, so it checks for that before doing anything.
#!/bin/bash
# Enable the macOS application firewall if it is not already on.
set -euo pipefail
SFW=/usr/libexec/ApplicationFirewall/socketfilterfw
if [ "$(id -u)" -ne 0 ]; then
echo "Run this script with sudo." >&2
exit 1
fi
state="$("$SFW" --getglobalstate)"
case "$state" in
*enabled*)
echo "Firewall already enabled."
;;
*)
"$SFW" --setglobalstate on
echo "Firewall enabled."
;;
esac
"$SFW" --getglobalstateSave it as enable-firewall.sh, make it executable with chmod +x enable-firewall.sh, and run it with sudo ./enable-firewall.sh. The final line prints the state again, so you see the result of the change and not only your intent.
Working with per-app rules from Terminal
You can inspect and edit the application list the same way. This lists every app the firewall has a rule for:
/usr/libexec/ApplicationFirewall/socketfilterfw --listappsTo add an app and allow it to accept incoming connections, use --add and then --unblockapp. To block an app, use --blockapp. The path must be the full path to the application bundle. Replace /Applications/Example.app with a real app on your Mac:
APP="/Applications/Example.app"
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --add "$APP"
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --unblockapp "$APP"APP="/Applications/Example.app"
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --blockapp "$APP"Quote the path, because many application names contain spaces. If --add reports that the path does not exist, check the spelling. If you edit rules for an app and later update or reinstall it, the app's code signature may change, and the firewall may ask you again.
What happens when the firewall is on
After you enable it, you will notice a few behaviors:
- An app that opens a listening socket for the first time may trigger a prompt: "Do you want the application to accept incoming network connections?" This is normal. Read the app name before you click Allow.
- Services you turned on, such as File Sharing, Screen Sharing, or Remote Login, keep working because they are built-in or signed software. Turning on "Block all incoming connections" will break them.
- Your own outgoing connections, such as browsing, email, and software updates, are unaffected.
Developers hit one common case. If you run a local server, such as python3 -m http.server, other devices on your network may be unable to reach it while the prompt is pending or after you click Deny. Reach it from the same Mac using localhost, which the firewall does not block, or allow the interpreter when prompted.
Verifying and testing
Checking the setting is not the same as testing the behavior. To confirm the firewall works end to end, take these steps:
- Run
--getglobalstateand confirm it reports enabled. - Run
--listappsand look for apps you expect to see rules for. - From another device on the same network, try to connect to a service on your Mac that you have blocked, and confirm it fails.
For step 3, a quick check is to start a test listener and try to reach it from another computer using the Mac's local IP address. Remember to remove any test allow rule afterward.
Common errors and gotchas
- Assuming it is on. The firewall ships disabled. A Mac that has never been configured is unprotected at this layer.
- Confusing it with outbound filtering. The firewall does not stop an app from sending data out. Tools that filter outgoing traffic are third-party products.
- Allowing too much. If you click Allow on every prompt, the firewall list becomes a record of permissive rules. Review the list occasionally in Options and remove apps you no longer use.
- Running
--setglobalstatewithoutsudo. The command fails with a permission message. Reads work withoutsudo. Writes need it. - Trusting "signed" too much. Automatic allowing of signed software means any app with a valid developer certificate can accept connections without a prompt. A signature shows who published the app. It does not show that the app is safe.
Exercise
You enable the firewall, then open a local development server on port 8000. A colleague on the same Wi-Fi cannot reach it, but you can reach it from your own Mac at localhost. Name two firewall settings that could explain this, and give the Terminal command that lists the per-app rules so you can check.
Show answer
Either "Block all incoming connections" is on, or the program running the server has a rule set to "Block incoming connections" (for example, you clicked Deny at the prompt). If the server's program is unsigned and no rule exists, a pending prompt can also stop connections. Run /usr/libexec/ApplicationFirewall/socketfilterfw --listapps to see the per-app rules, and --getblockall to check the block-all setting.
Exercise
Why does the firewall script in this chapter check for root and read the current state before calling --setglobalstate on?
Show answer
--setglobalstate needs administrator rights, so checking with id -u gives a clear error instead of a confusing permission failure. Reading the state first makes the script safe to run repeatedly: it reports "already enabled" instead of changing anything, and it prints the final state so you can see the result.