Apple

macOS Command Line Basics – Part 7: Network Commands

Managing network settings and configurations on macOS can be a straightforward process with the right commands. Here’s a detailed guide on essential network commands that can help troubleshoot, configure, and…

4 min read TexArxs

Managing network settings and configurations on macOS can be a straightforward process with the right commands. Here’s a detailed guide on essential network commands that can help troubleshoot, configure, and optimize your macOS network settings.


networksetup:

The networksetup command is a powerful command-line utility on macOS that allows users to manage and configure network settings. It provides control over network interfaces, locations, and configurations, making it a valuable tool for system administrators and advanced users who need to script or automate network configuration tasks. Visit man networksetup will page for all the attributes for the command.

List Network Services:

Display all available network services on the system (e.g., Wi-Fi, Ethernet, Thunderbolt).


$ networksetup -listallnetworkservices

Output:

USB 10/100/1000 LAN

Thunderbolt Bridge

Wi-Fi


Configure Manual IP Address:

Set a static IP address, subnet mask, and router for a specific network service.

$ networksetup -setmanual “Wi-Fi” 192.168.1.154 255.255.255.0 192.168.1.1


Configure DHCP:

Set a static IP address, subnet mask, and router for a specific network service.

$ networksetup -setdhcp “Wi-Fi”


Set DNS Servers

Specify DNS servers for a network service.

$ networksetup -setdnsservers “Wi-Fi” 8.8.8.8 8.8.4.4


Get MAC Address

Displays ethernet (or Wi-Fi) address for hardware port or device specified.

$ networksetup -getmacaddress “Wi-Fi”


Connect to a Wi-Fi Network:

Join a Wi-Fi network with a specified SSID and password.

$ networksetup -setairportnetwork en0 “NetworkName” “Password”


Enable or Disable Network Services:

Turn a network service on or off.

$ networksetup -setnetworkserviceenabled “Wi-Fi” on

$ networksetup -setnetworkserviceenabled “Wi-Fi” off


Notes:

Administrative Privileges: Many networksetup commands require administrative privileges, so you may need to use sudo.


Scripting: The command is highly useful for scripting network configurations, especially in environments where network settings change frequently or need to be managed across multiple devices.


The networksetup utility is an essential tool for detailed network management on macOS, offering more granular control than the graphical interface in System Preferences.


Get Active Interface Name

activeInterface=$(route get default | grep interface | awk ‘{print $2}’)

echo $activeInterface

Output: (Depends on your active interface)

en0


ifconfig:

The ifconfig (interface configuration) command is a network utility in Unix-like operating systems, including macOS, used for configuring, managing, and querying network interfaces. It provides a range of functionalities that help administrators and users control network connections and troubleshoot network issues.


Get information about the interface

$ ifconfig en0  #Use variable to pass Active Interface Name (Ex: ifconfig $activeInterface)

Output:

en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500

               options=6460<TSO4,TSO6,CHANNEL_IO,PARTIAL_CSUM,ZEROINVERT_CSUM>

               ether 01:b1:f4:c1:70:e4

               inet6 fe22::ce6:f8b3:e818:1234%en0 prefixlen 64 secured scopeid 0xe

               inet 192.168.4.32 netmask 0xfffff800 broadcast 255.255.0.0

               nd6 options=201<PERFORMNUD,DAD>

               media: autoselect

               status: active


Enable or Disable an interface:

$ sudo ifconfig en0 up  

$ sudo ifconfig en0 down


ipconfig:

The ipconfig command in macOS is used to manage and retrieve network configuration details for various network interfaces. It is similar in purpose to ifconfig but provides different functionality, particularly around dynamic IP addressing and DHCP information.


Get the IP Address of an Interface:

To display the current IP address assigned to a specific interface (e.g., en0):

$ ipconfig getifaddr en0


Get the Subnet Mask:

To retrieve the subnet mask for an interface:

$ ipconfig getoption en0 subnet_mask


View DHCP Lease Information:

To get detailed information about the DHCP lease for an interface:

$ ipconfig getpacket en0


ping:

The ping command in macOS is a network utility used to test the connectivity between your machine and a specified host. It helps in diagnosing network issues by sending a series of ICMP echo requests to the target and waiting for responses.


Continuous ping for a host

$ ping google.com


Ping a host with a delay. (default is 1 sec)

$ ping -i 5 google.com


Ping a host a specific number of times:

$ ping -c 5 google.com


Follow TexArxs more insights on macOS management, security, and more!

Follow TexArxs on LinkedIn
Need Apple IT support for your team?
TexArxs handles MDM deployment, Mac support, and Apple IT management for startups and SMEs across India.
Talk to us →

We use cookies to improve your experience and analyse site usage. By continuing, you agree to our use of cookies. Read our Privacy Policy.