Friday, July 31, 2015

Release & Renew DHCP from the Command Line with ipconfig

If you need to release and renew DHCP from the command line on a Mac, the helpful ipconfig utility is able to do so quickly. Do keep in mind that for most OS X users, renewing a DHCP lease from the Mac System Preferences is the best approach because it’s user friendly and just as effective, but the Terminal approach has benefits to advanced users and is able to be run through ssh and single user mode, making this a worthwhile bit of knowledge to share.

The basic syntax to renew DHCP lease from the command line with ipconfig is as follows:



sudo ipconfig set (DEVICEINTERFACE) DHCP

If you know the device interface (en0, en1, en2, etc), just run that command to release and then renew DHCP for the determined device. Let’s say it’s en0, standard for modern Macs with wi-fi only.

sudo ipconfig set en0 DHCP

Once the command is run you can check if DHCP has set properly by determining DHCP info with the same ipconfig command with ‘getpacket’ like so:

ipconfig getpacket en0

Assuming the prior ‘set’ command was successful, getpacket will return the DHCP assigned IP, DNS server, subnet mask, router / gateway, and the lease time. If the DHCP info returns blank, then either the interface queried was wrong, or the DHCP lease did not renew or distribute properly.

Another crude option would be to run ipconfig for all available device interfaces on the Mac by stringing the syntax together like so:

sudo ipconfig set en0 DHCP && sudo ipconfig set en1 DHCP

It’s best to set DHCP for the specific interface, however.

If you don’t know the interface, then the first step is to determine the computers hardware device interface used for the particular networking port you’re using. For most modern Macs, we’re looking for wi-fi which is typically on en0, but many Mac users use ethernet, an iPhone personal hotspot, a tethered Android phone, or an external NIC card as well, each of which may have a different device interface depending on the hardware. You can easily determine what the device interface is by running networksetup -listallhardwareports like so:

networksetup -listallhardwareports

Scroll through the output to find the interface you want to set and renew DHCP for, let’s assume you’re looking for “Wi-Fi” which may look like this:

Hardware Port: Wi-Fi
Device: en0
Ethernet Address: b1:3f:22:dd:ab:19


No comments:

Post a Comment