Skip to content
Menu

Turn on the Mac Firewall and Stealth Mode

Turns on the macOS application firewall and, by default, stealth mode. It saves the settings file if present before changing either setting.

Turn on the Mac firewall and stealth mode is free for members.

Enter your email to create your free account and get the file.

We'll email you the script and the weekly MSP Outpost email. Unsubscribe anytime. Privacy policy.

Already a member? Sign in.

Be the first to review Turn on the Mac firewall and stealth mode. Tell other MSPs how it worked for you.

Write a Review

LanguageBash
Runs onmacOS 13+; runs as root
Needsbash 3.2, root, socketfilterfw
Changes thingsChanges what it runs on

What It Does

The script reads the application firewall with socketfilterfw. When it is off, the script turns it on. When ENABLE_STEALTH_MODE is true, it also turns stealth mode on if that is off. The global state stays as it is when the firewall is already on or blocking all incoming connections. Stealth mode can still change. Per-app rules, the block-all setting and pf rules stay as they are.

Before a real change it copies the firewall settings file (/Library/Preferences/com.apple.alf.plist), if present, to /Library/Application Support/MSPOutpost/Backups/enable-mac-firewall/. After the change it reads the state again and ends in ERROR if it did not take, which is what you see when a configuration profile manages the firewall and puts it back.

The script leaves existing app rules and signed-app allowances as they are. Check that any server app can still receive the connections it needs. When the requested settings are already on, the script reports that there is nothing to do.

Before You Run It

  • Run it with DRY_RUN=true first and read what it would change.
  • Check that no unsigned server app on the Mac needs to receive connections, or be ready to allow it.
  • Stealth mode makes the Mac ignore probes such as ping. If a monitor pings the Mac, set ENABLE_STEALTH_MODE to false.
  • If it ends in ERROR, look for a configuration profile or MDM policy that manages the firewall.

Type and Risk

  • Type: Action. It makes its change, checks the change worked and says what changed. It exits 0, or 2 when it could not do the job.
  • Risk: Low. Set DRY_RUN=true to list every change without making any.
  • What it changes: Turns on the application firewall and (optionally) stealth mode.
  • Restart: It never restarts the machine.
  • Typical run: Under 5 seconds.
  • Example answer: "Turned on: Firewall, Stealth mode"
  • Tested before listing on macOS 27 (Apple silicon VM) with /bin/bash 3.2, run as root, against its own test cases.

Inputs

Pass an input as an argument (--input-name value), as an environment variable named as listed, which is how most RMMs pass script variables, or change its default in the DEFAULT_ lines near the top of the script. An argument wins over an environment variable, which wins over the default.

  • ENABLE_STEALTH_MODE: Also turn stealth mode on. Default true.

Every script also takes DRY_RUN, OUTPUT_FORMAT (Text, Answer, Json or Csv), EXPORT_PATH and RESULT_FIELD, which writes the answer to an RMM custom field, unless the list above words one differently.

License

Provided "as is" under the MIT License, without warranty of any kind. Test before production use. MSP Outpost and User First Digital Inc. accept no liability for any damage or loss.

The Script

enable-mac-firewall.sh · 569 lines · 25.3 KB

Free for Members

Create a free account or sign in to read, copy and download this script.

Welcome Back

Sign in to your MSP Outpost account.