Process to Change Mac's from Static I.P's to DHCP

gbunner
New Contributor III

Hi All, I've searched around here a bit, but have not yet found what I'm looking for. Some of our Mac's are already set to DHCP, but I still have a bunch that need to be converted from static I.P's over to DHCP. Using the JSS, does anyone have a good process that I can emulate to discover and change the remaining Mac's over to DHCP? Most of the ones that have been converted are new replacement computers and DHCP was set at the time of deployment.

Thanks!

8 REPLIES 8

mottertektura
Contributor

How about a policy that executes this command?

networksetup -setdhcp

[http://krypted.com/mac-security/mac-network-commands-cheat-sheet/](link URL)

You would just need to include the interface(s) that should be set to DHCP:

networksetup -setdhcp Wi-Fi

mm2270
Legendary Contributor III

To expand a little on the above from @mottertektura here are some useful commands

To get a list of all hardware ports that should be able to use DHCP, and excluding any that usually won't be able to use DHCP:

/usr/sbin/networksetup -listallhardwareports | awk -F': ' '/Hardware Port:/{print $NF}' | egrep "Ethernet|Wi-Fi|Thunderbolt"

To check on any item from the above to see if DHCP or Manual IP is configured for it

/usr/sbin/networksetup -getinfo "Port Name" | grep Configuration

The above will output either "Manual Configuration" if it's manually configured, or "DHCP Configuration" if it's using DHCP. It may also come back blank if it's not really configured at all, which may happen under some circumstances.

If you wanted to set all possible ports to use DHCP that can get an IP (except for things like Bluetooth DUN and such) you could create a script that would use the output from my first command above and loop over each one, setting them to DHCP, using the /usr/sbin/networksetup -setdhcp "Port" command like above

The only issue I see is that the Mac may temporarily lose a network connection while the newly adjusted port negotiates with the DHCP server to obtain a lease and IP address. This could cause the policy to stop running or the script to exit abnormally. You might need to do this by deploying a LaunchDaemon to machines that gets triggered after it's installed, runs, configures all available ports for DHCP, then deletes itself so it doesn't keep running on each startup.

guidotti
Contributor II

To build on what these guys said - here is another little resource about networksetup that specifically references your scenario:
https://yourmacguy.wordpress.com/2008/08/07/networksetup-command/

Want to convert from static addressing to DHCP?

mainInt=$(networksetup -listnetworkserviceorder | awk -F') ' '/(1)/ {print $2}') networksetup -setdhcp "$mainInt"

gbunner
New Contributor III

Thanks for the great responses. It looks like I will need to put one of these solutions into script form and then create a policy to execute the script, correct?

guidotti
Contributor II

You sure will.

For instance, I use networksetup to do all of our proxy and location configurations on the workstations.

mm2270
Legendary Contributor III

Yes. Here's an example, but be careful. Not tested, but this should take all ports/services that can likely use DHCP (Wi-Fi, any Ethernet or Thunderbolt ones) and configure each one for DHCP, if it's not already. This may not be what you want, but it's unclear if you're only looking to set one service/port or all of them for it.
You could adjust the first line that gets the service names by removing, say, the |Thunderbolt from the awk command, which would then only get Wi-Fi and Ethernet services, for example.

#!/bin/bash

Services=$(/usr/sbin/networksetup -listallnetworkservices | awk '/Wi-Fi|Ethernet|Thunderbolt/')

while read PORT; do
    if [[ ! $(/usr/sbin/networksetup -getinfo "$PORT" | grep Configuration) =~ "DHCP" ]]; then
        echo "$PORT is not configured for DHCP. Fixing..."
        /usr/sbin/networksetup -setdhcp "$PORT"
    else
        echo "$PORT is already configured for DHCP. Skipping..."
    fi
done < <(printf '%s
' "$Services")

As I mentioned earlier, I can't say how this will work when run on a Mac using those ports to connect to the JSS and run said policy + script. It may lose the connection and not complete or not be able to upload a log, etc. You're flipping a network connection to a different state while it's in use, so be cautious with it.

johnlp
New Contributor II

Updated ours to also include Belkin USB-C LAN

johnlp
New Contributor II
#!/bin/bash

Services=$(/usr/sbin/networksetup -listallnetworkservices | awk '/Wi-Fi|Ethernet|Belkin USB-C LAN|Thunderbolt/')

while read PORT; do
    if [[ ! $(/usr/sbin/networksetup -getinfo "$PORT" | grep Configuration) =~ "DHCP" ]]; then
        echo "$PORT is not configured for DHCP. Fixing..."
        /usr/sbin/networksetup -setdhcp "$PORT"
    else
        echo "$PORT is already configured for DHCP. Skipping..."
    fi
done < <(printf '%s
' "$Services")