DEV Community

Dunith Dhanushka
Dunith Dhanushka

Posted on

How To Set a Static IP Address in macOS Using Command Line

Imagine you have an Apple device like a Macmini, MacBook, or an iMac in a remote location. You only have access to it via SSH and no Desktop GUI interaction is allowed.

What if you wanted to change the IP address of the device? For example, how do you assign a static IP address instead of DHCP?

Command line is your option in that case.

Let's see how to get it done...

Make sure you are in the sudoers list

To make changes like network interface configurations, you need to be a user with super user privileges.

Follow the below commands

First, get a list of all your network services.

networksetup -listallnetworkservices
Enter fullscreen mode Exit fullscreen mode

That command will result in an output like below.

>networksetup -listallnetworkservices
An asterisk (*) denotes that a network service is disabled.
Apple USB Ethernet Adapter
USB 10/100/1000 LAN
Wi-Fi
Bluetooth PAN
Thunderbolt Bridge
Enter fullscreen mode Exit fullscreen mode

Then use the desired service name in the following command:

networksetup -setmanual SERVICE IP SUBNET ROUTER
Enter fullscreen mode Exit fullscreen mode

For example, to change the IP address of my Wi-Fi interface, I would use a command like this:

networksetup -setmanual Wi-Fi 192.168.1.2 255.255.255.0 192.168.1.1
Enter fullscreen mode Exit fullscreen mode

In the above example, 192.168.1.2 is my new IP address. 192.168.1.1 is the address of the default gateway, which is the router most of the time.

Setting it back to DHCP

To set it back to DHCP, use the following command:

networksetup -setdhcp SERVICE
Enter fullscreen mode Exit fullscreen mode

Keep DHCP with a manual IP

To keep DHCP services but only use a manually designated IP address, then use the following command:

networksetup -setmanualwithdhcprouter SERVICE IP
Enter fullscreen mode Exit fullscreen mode

Confirm settings are permanent

Restart the device sudo shutdown -r now and see if the new address persists.

If everything goes well, it should persist.

Discussion (0)