# Script to enable/disable Firewall in macOS devices

The built-in Firewall on macOS devices prevents authorized computers and servers from connecting to your Mac. Admins users have the option to manually [configure Firewall](https://support.apple.com/en-in/guide/mac-help/mh34041/mac) settings through the *Network* panel in *System Settings*. Organizations enable the Firewall on their macOS devices to ensure network security. In some exceptional circumstances, they might also need to turn off the Firewall. This document provides you with firewall script templates that help you configure Firewall settings on macOS devices remotely. These can be executed using Hexnode’s [Execute Custom Script](https://www.hexnode.com/mobile-device-management/help/how-to-run-scripts-on-mac-using-hexnode-mdm/) feature.

 Disclaimer:The sample scripts provided below are adapted from third-party open-source sites.

 

Script to turn on Firewall on a Mac 
------------------------------------

Script to turn on Firewall on Mac

sudo defaults write /Library/Preferences/com.apple.alf globalstate -int 1 

   1

  sudo defaults write /Library/Preferences/com.apple.alf globalstate -int 1 

   

 

  

![Firewall enabled using the firewall script on a mac](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2023/06/Firewall-script-to-enable-Firewall.jpg)Script to turn off Firewall on a Mac 
-------------------------------------

Script to turn off Firewall on Mac

sudo defaults write /Library/Preferences/com.apple.alf globalstate -int 0 

   1

  sudo defaults write /Library/Preferences/com.apple.alf globalstate -int 0 

   

 

  

![Firewall disabled using the firewall script on a mac](https:2023/06/Firewall-script-to-disable-Firewall.jpg)Navigate to **System Preferences > Security & Privacy > Firewall** on the device to verify the change. Admin credentials are needed to modify the Firewall settings from the device end.

Script to check Firewall status on a Mac 
-----------------------------------------

Script to check Firewall status on a Mac

/usr/libexec/ApplicationFirewall/socketfilterfw --getglobalstate 

   1

  /usr/libexec/ApplicationFirewall/socketfilterfw --getglobalstate 

   

 

  

Executing the above command will retrieve the current firewall status of the macOS device. The script will output if the firewall is enabled (State 1) or disabled (State 0).

 Notes:- It is recommended to manually validate the script execution on a system before executing the action in bulk.
- Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.