6

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 and ipconfig /renew.

How can I release and renew network settings from the commandline interface?

4
  • Which 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
    – muru
    May 4, 2016 at 7:51
  • 2
    Did you try sudo /etc/init.d/network-manager restart?
    – migrc
    May 4, 2016 at 8:50
  • What exactly is a "modern version of Ubuntu"? You should be more specific.
    – Rosamunda
    Mar 12, 2017 at 17:30
  • 1
    A modern version of Ubuntu would be a current version. The answer could change over time.
    – David
    Mar 15, 2017 at 21:17

2 Answers 2

10

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!

2
  • For 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 use dnscrypt-proxy and I have to have systemd-resolved masked, otherwise it will interfere with it. Any other option? Thanks.
    – Shayan
    Aug 23, 2019 at 17:18
  • 1
    @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 then sudo ifconfig eth0 up. Sorry for the late response.
    – Terrance
    Apr 23, 2020 at 21:51
1

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)

1
  • So there's no equivalent to ipconfig /release /renew of Windows for Linux? But other than that, your answer works perfectly.
    – Shayan
    Aug 23, 2019 at 17:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.