Sub topic 11
Verifying your settings from Terminal
System Settings shows each security feature as a switch. This chapter shows how to read the same state from Terminal. You can check a Mac in seconds, paste the output into a support ticket, or run the same check on several Macs. You can also confirm that a setting stayed on after a macOS update.
This chapter assumes you can open Terminal and run a command, and that you know what sudo does. It also assumes you have already met FileVault, the application firewall, and Find My in earlier chapters. The checks here are read-only and need no administrator rights, except where noted.
What Terminal can and cannot tell you
Two of the three features have a command-line tool for reading their state.
| Feature | Terminal check | Needs `sudo` to read? |
|---|---|---|
| FileVault | `fdesetup status` | No |
| Application firewall | `socketfilterfw` with `--get...` options | No |
| Find My Mac | No documented command | Not applicable |
Changing a setting is different from reading it. Turning the firewall on, for example, needs administrator rights. Reading the state does not.
Find My has no documented command-line check. Confirm it in System Settings, under your Apple Account, then iCloud. A script can report FileVault and the firewall. It cannot report Find My, so a clean script result does not prove the Mac is recoverable.
Checking FileVault with fdesetup
fdesetup is the command-line tool for managing FileVault. Its status subcommand prints the current state.
fdesetup statusThe common outputs are:
FileVault is On.The startup disk is encrypted, or encryption has been enabled.FileVault is Off.The disk is not protected by FileVault.Encryption in progress: Percent completed = Nappears together with the On line while the disk is still being encrypted. Your data is not yet fully protected.Decryption in progress: ...means FileVault was turned off and the disk is being decrypted.
The "On" line can appear while encryption is still running. After you enable FileVault, the Mac encrypts in the background and you can keep working. Seeing FileVault is On. right after you enable it does not mean the job is finished, so read the whole output and look for a progress line.
A Mac with several volumes may print details that differ from this list. The first line is the one to rely on.
Checking the firewall with socketfilterfw
The application firewall is managed by a tool at /usr/libexec/ApplicationFirewall/socketfilterfw. That directory is not on your default PATH, so you type the full path. Each setting has a --get option to read it and a --set option to change it.
The master switch comes first:
/usr/libexec/ApplicationFirewall/socketfilterfw --getglobalstateTypical output is Firewall is enabled. (State = 1) or Firewall is disabled. (State = 0). The wording can vary between macOS versions. The number after State = is easier to match than the sentence.
Stealth mode and the block-all-incoming option each have their own check:
/usr/libexec/ApplicationFirewall/socketfilterfw --getstealthmode
/usr/libexec/ApplicationFirewall/socketfilterfw --getblockallThese print a short sentence stating whether each option is on. Run them once on your own Mac and note the exact wording before you write a script that matches it. Different macOS releases word these lines differently, and a script that matches the wrong phrase can report the opposite of the real state.
Per-app rules are also visible from Terminal. The tool has options to list the apps it knows about, and you can see the same list in System Settings under Network, then Firewall, then Options. Run the tool with --help to see the options your version supports.
If the global state is off, you can turn it on. This is the one command in the chapter that changes something, so it needs administrator rights:
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --setglobalstate onTerminal asks for your password. It prints nothing as you type, which is normal. Afterward, run --getglobalstate again to confirm the change took effect.
Combining the checks into one script
Running three commands by hand is fine once. If you check often, put them in a script. This is the minimal version, and it prints the raw status of each feature:
#!/bin/bash
# Print a quick security status summary for this Mac.
echo "FileVault: $(fdesetup status)"
echo "Firewall: $(/usr/libexec/ApplicationFirewall/socketfilterfw --getglobalstate)"
echo "Stealth: $(/usr/libexec/ApplicationFirewall/socketfilterfw --getstealthmode)"Save it as secstatus.sh, then make it executable and run it:
chmod +x secstatus.sh
./secstatus.shIf FileVault is encrypting, fdesetup status prints more than one line. The extra line appears inside the $(...) substitution, so the progress line is printed too.
Making the script report pass or fail
A summary you read by eye is good. A script that exits with a nonzero code when something is wrong is better, because other tools can act on it. The version below checks FileVault and the firewall master switch. It reports the stealth line for information only, because its wording varies the most.
#!/bin/bash
# Check FileVault and the application firewall.
# Exit 0 if both are on, 1 otherwise.
SOCKETFILTERFW=/usr/libexec/ApplicationFirewall/socketfilterfw
problems=0
filevault=$(fdesetup status)
case "$filevault" in
*"FileVault is On"*)
echo "OK: FileVault is on."
case "$filevault" in
*"Encryption in progress"*)
echo "NOTE: Encryption is still running; the disk is not fully protected yet."
;;
esac
;;
*)
echo "FAIL: FileVault is not on."
problems=1
;;
esac
firewall=$("$SOCKETFILTERFW" --getglobalstate)
case "$firewall" in
*"enabled"*)
echo "OK: Firewall is on."
;;
*)
echo "FAIL: Firewall is not on."
problems=1
;;
esac
echo "INFO: $("$SOCKETFILTERFW" --getstealthmode)"
echo "INFO: Find My cannot be checked here. Confirm it in System Settings."
exit "$problems"The script matches the word enabled, which does not appear inside disabled, so a disabled firewall falls through to the failure branch. It treats anything it does not recognize as a failure. That is the safe default: if a future macOS release changes the wording, the script reports a problem instead of a false pass.
After running it, check the exit code with echo $?. A 0 means both checks passed.
Common errors and gotchas
command not foundforsocketfilterfw. You typed only the tool name. The directory is not on yourPATH, so use the full path, or store it in a variable as the script does.- A
--setoption fails or does nothing. Setters needsudo. A getter run withoutsudoworks, so a read that succeeds does not mean a write will. - The script reports FileVault as on during encryption.
fdesetup statussays On as soon as encryption is enabled. Look for theEncryption in progressline, as the second script does. - The firewall reads as on, but an app still accepts connections. The firewall acts on incoming connections to apps. A per-app rule can allow an app through, and some signed system software is allowed automatically. Read the app list before you conclude the Mac is closed.
- Stealth mode reads as off. Stealth mode is a separate option from the master switch. The firewall can be on with stealth mode off. Each option reports separately.
- A clean result on a Mac you do not control. If you run the script over a remote session on someone else's Mac, you are checking their settings with your account. Make sure you have permission before you do that.
When to use Terminal and when to use System Settings
Use System Settings when you are changing a setting once and want the interface to explain the options. Use Terminal when you need to check many Macs, repeat the check, or record output. A shell script suits routine checks. Large organizations manage these settings with device management tools instead, which is beyond this tutorial.
Exercise
You run fdesetup status right after turning on FileVault and see FileVault is On. followed by Encryption in progress: Percent completed = 8. Is the disk fully protected? What should you do?
Show answer
Not yet. The On line appears as soon as encryption is enabled, and the progress line shows the disk is still being encrypted at 8 percent. Keep the Mac powered (plugged in if it is a laptop) and run fdesetup status again later. When the progress line is gone and the output reads FileVault is On., encryption is complete.
Exercise
The pass/fail script prints FAIL: Firewall is not on. Which command turns the firewall on, why does it need sudo, and how do you confirm the change?
Show answer
Run sudo /usr/libexec/ApplicationFirewall/socketfilterfw --setglobalstate on. Changing the firewall state is a system-wide change, so it needs administrator rights, while reading the state does not. Confirm it by running /usr/libexec/ApplicationFirewall/socketfilterfw --getglobalstate and checking that it reports the firewall as enabled.
Exercise
Why can't the status script prove that Find My Mac is on, and how do you check it instead?
Show answer
Find My has no documented command-line check, so there is nothing reliable for a script to read. Confirm it in System Settings under your Apple Account, then iCloud. The script prints a reminder for this reason.