How to release an IP address and renew from the commandline?
Solution 1
To release and renew the IP address it is:
sudo dhclient -r eth0
sudo dhclient eth0
Or you can try a one-liner that grabs the default ethernet name from netstat
(using -v switch to show verbose):
netstat -r | awk '/default/ {print $NF}' | while read NIC; do sudo dhclient -r -v $NIC && sudo dhclient -v $NIC; done
From the dhclient
manpage:
-r Release the current lease and stop the running DHCP client as
previously recorded in the PID file. When shutdown via this
method dhclient-script will be executed with the specific reason
for calling the script set. The client normally doesn't release
the current lease as this is not required by the DHCP protocol
but some cable ISPs require their clients to notify the server
if they wish to release an assigned IP address.
Example:
terrance@terrance-ubuntu:~$ netstat -r | awk '/default/ {print $NF}' | while read NIC; do sudo dhclient -r -v $NIC && sudo dhclient -v $NIC; done
Killed old client process
Internet Systems Consortium DHCP Client 4.4.1
Copyright 2004-2018 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/
Listening on LPF/eth0/40:8d:5c:4f:12:03
Sending on LPF/eth0/40:8d:5c:4f:12:03
Sending on Socket/fallback
DHCPRELEASE of 10.0.0.100 on eth0 to 10.0.0.1 port 67 (xid=0x10ff1bda)
Internet Systems Consortium DHCP Client 4.4.1
Copyright 2004-2018 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/
Listening on LPF/eth0/40:8d:5c:4f:12:03
Sending on LPF/eth0/40:8d:5c:4f:12:03
Sending on Socket/fallback
DHCPDISCOVER on eth0 to 255.255.255.255 port 67 interval 3 (xid=0x9b730303)
DHCPOFFER of 10.0.0.100 from 10.0.0.1
DHCPREQUEST for 10.0.0.100 on eth0 to 255.255.255.255 port 67 (xid=0x303739b)
DHCPACK of 10.0.0.100 from 10.0.0.1 (xid=0x9b730303)
Timeout too large reducing to: 2147483646 (TIME_MAX - 1)
bound to 10.0.0.100 -- renewal in 2147483648 seconds.
Hope this helps!
Solution 2
One way to accomplish this is to tell network-manager to briefly disconnect the device and the connecting it again:
nmcli device disconnect wlan0; nmcli device connect wlan0
(replace wlan0
with the correct device name on your system)
Related videos on Youtube
David
Full time systems admin and network engineer, part time programmer. For fun, I swing dance.
Updated on September 18, 2022Comments
-
David over 1 year
I am using modern versions of Ubuntu that use network-manager, and I would like release and renew my network settings through the commandline.
In the olden days when Ubuntu used the
interfaces
file, I would simply do:sudo /etc/init.d/networking restart
but now that no longer works.I am looking for functionality similar to Windows'
ipconfig /release
andipconfig /renew
.How can I release and renew network settings from the commandline interface?
-
muru almost 8 yearsWhich version of Ubuntu? Network Manager's tools have evolved over time. Have a look at manpages.ubuntu.com/manpages/xenial/en/man1/nmcli.1.html
-
migrc almost 8 yearsDid you try
sudo /etc/init.d/network-manager restart
? -
Rosamunda about 7 yearsWhat exactly is a "modern version of Ubuntu"? You should be more specific.
-
David about 7 yearsA modern version of Ubuntu would be a current version. The answer could change over time.
-
-
Shayan over 4 yearsFor the second command (
sudo dhclient eth0
) I get this error:Failed to try-reload-or-restart systemd-resolved.service: Unit systemd-resolved.service is masked.
It's actually because I usednscrypt-proxy
and I have to havesystemd-resolved
masked, otherwise it will interfere with it. Any other option? Thanks. -
Shayan over 4 yearsSo there's no equivalent to
ipconfig /release /renew
of Windows for Linux? But other than that, your answer works perfectly. -
Terrance about 4 years@Shayan I really don't know about
dnscrypt-proxy
and I wouldn't know any commands to help about that. I guess you could try downing the eth0 port then bring it back up.sudo ifconfig eth0 down
thensudo ifconfig eth0 up
. Sorry for the late response.