Release & Renew DHCP from the Command Line with ipconfig

Jul 30, 2015 - 3 Comments

Terminal in OS X 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

Alongside ‘Device’ you’ll find the interface, in this case it’s “en0”, which is what gets plugged into the aforementioned ipconfig command.

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

Related articles:

Posted by: Paul Horowitz in Command Line, Mac OS X, Tips & Tricks

3 Comments

» Comments RSS Feed

  1. Mr Darwin says:

    Slightly more user friendly but specific to Mac is to use networksetup, it can skip device and point at interface name instead like so for wi-fi:

    sudo networksetup -setdhcp Wi-Fi

    or for ethernet:

    networksetup -setdhcp Ethernet

    You can even set manual DHCP info this way.

    Manual IP with DHCP router (ie: manual IP, DHCP all else)

    networksetup -setmanualwithdhcprouter Wi-Fi 192.168.0.15

    Or set entirely manual DHCP info like so:

    networksetup -setmanual Wi-Fi (ip) (subnet) (router)

    Without sudo it will prompt for the admin password in OS X, of course.

  2. PaulK says:

    Here’s another way…

    sudo ifconfig en0 down; sudo ifconfig en0 up

    I have a shell script I use for this:

    #!/bin/sh

    # IPRenew.sh
    #
    #
    # Created by Paul K on 7/27/14.
    # A quick way to renew the ipaddress on an interface
    #
    echo ‘***** Renew IP Configuration *****’
    echo ‘Current IP Configuration for ‘$1
    ipconfig getpacket $1 | grep ‘yiaddr\|subnet_mask\|router\|domain_name_server\|domain_name’
    echo ‘*****’
    echo ‘Shutting Down: ‘$1
    sudo ifconfig $1 down
    echo ‘*****’
    echo ‘Bringing Up: ‘$1
    sudo ifconfig $1 up
    echo ‘*****’
    echo ‘Renewed IP Configuration for’$1
    ipconfig getpacket $1 | grep ‘yiaddr\|subnet_mask\|router\|domain_name_server\|domain_name’
    echo ‘***** Renew IP Configuration *****’

    Usage: sh IPRenew.sh en0

    Paul K

Leave a Reply

 

Shop for Apple & Mac Deals on Amazon.com

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates