How to Release old IP or force Renew DHCP lease IP in Linux
In this article, we will see how to release old IP or force renew DHCP lease IP in Linux. Dynamically allocated IP is an IP leased out from a DHCP server for a particular amount of time over a network. If you are using a lease IP then you always have to renew your ip before the lease period expires. Usually, when a lease reaches 50% of its validity time period, the DHCP client unicasts a DHCP Request message to the DHCP server to request lease renewal. The same can be forced manually through a dhcp client tool in Linux Systems.
What is DHCP
The Dynamic Host Configuration Protocol is a network management protocol used on Internet Protocol networks for automatically assigning IP addresses and other communication parameters to devices connected to the network using a client–server architecture.
How to Release old IP or force Renew DHCP Lease IP in Linux
If you have any DHCP lease IP allocated to an interface which you want to release due to certain reasons then you always have the option to release it by using DHCP client tool called dhclient. For example, in my case I have two interfaces attached to my system — enp0s3 and enp0s8 .
To release the IP attached to both the above interfaces, we need to use sudo dhclient -r -v command. But sometimes what happens is that the IP does not get released as you can see below.
If we try to check again the IP got released or not using ip addr sh command then we can notice that nothing happened. The assigned IP did not got released.
If you try to release IP from specific interface by choosing the interface using sudo dhclient -r -v enp0s3 command then also you can notice that the IP did not got released. This is a very common scenario faced by lot of folks hence I purposefully chosen this one while trying to explain how to release the DHCP lease IP so that you will understand what needs to be done here.
Sometimes even if you try to kill the dhclient process using kill -9 <PID> command then you will notice that it is getting started every time you kill this process and the IP is not getting changed. To check the process ID of currently running dhclient program, you can use ps -ef | grep dhclient command as shown below.
In my case process ID(PID) is 2167 , so to kill this one we are using kill -9 2167 command as shown below.
Now if I try to check the process again, I can see that a new dhclient command is launched with process ID(PID) 2169 . So now you must be wondering how did this happen.
Well, this happened because the network interfaces are currently controlled by Network Manager. You can verify this on Debian/Ubuntu systems by checking one of the below netplan configuration file depending on Ubuntu installation you are currently having.
- 01-netcfg.yaml
- 01-network-manager-all.yaml
- 50-cloud-init.yaml
You can check the running status of Network Manager by using sudo systemctl status NetworkManager command as shown below.
If you are also facing problems like above, then you can try below solutions that works for you.
Solution 1: Restart Network Manager
First solution you can try is that you can restart Network Manager by running sudo systemctl restart NetworkManager command as shown below.
Solution 2: Using nmcli command
If the first solution does not work then next you can try restarting your connection using nmcli command. Here you need to first check the current connection using nmcli con command as shown below.
Then first take down the connection for the interface where you need to renew the IP. In our case, let’s say we want to renew the IP of enp0s3 interface, then we need to run nmcli con down id «Wired Connection 1» command after releasing the IP as shown below.
Then bring up the connection using nmcli con up id «Wired connection 1» command as shown below.
Solution 3: Restart systemd-networkd
If second solution also did not worked out, then you can try restarting systemd-networkd once by using sudo systemctl restart systemd-networkd command as shown below.
After applying above solution, you can see that you will be able to release the IP as shown below.
You can verify the same by checking the status of ip assigned to network interfaces using ip addr sh command as shown below. From the output, you can see that none of the IP is assigned to both the interfaces — enp0s3 and enp0s8 . This confirms that IP release was successful.
Once the IP is released, you will be able to get the new ip by running sudo dhclient -v command as shown below.
How to renew dhcp ip address in ubuntu?
This is a simple question in ubuntu server How do I renew a dhcp assigned IP address?
3 Answers 3
Actually, there are (somewhat unusual) situations in which
is not sufficient.
If the client thinks it already has a valid lease, it will use it, even if the DHCP server would have given it a different address. This can be confusing.
For instance, if you go from a dynamically allocated IP address to a static (and different) IP address for a given client, then (at least on Ubuntu 10.04, and possibly generally) $dhclient -r and $dhclient isn’t sufficient. Because the old lease is still valid, the client will just use that.
This can lead to your DHCP server thinking the IP address for your host should be one thing, and your host thinking a different thing. Chaos reigns.
How do I renew my DHCP lease?
Short of rebooting, how can I release and renew my DHCP lease? It would be useful to know a GUI and terminal method.
I’d especially like to know if there is a means to do this without requiring admin privileges.
10 Answers 10
To renew your dhcp lease at the terminal:
In my case I had to specify which card to renew:
Note: you might want to join the two on one line, as otherwise you might lose the remote connection you’re on after the first!
First release the current IP address:
then renew the IP address
.. or do them together to avoid losing connection:
In the network drop-down selector of the system tray you can press the network you are already connected to. This will make NetworkManager ask for a new lease from a DHCP server.
This also works for wired networks, but I don’t think it works for PPP connections (mobile broadband).
If you’re on a systemd based-version of Ubuntu without a GUI (i.e. server version) then this is the way to renew the DHCP lease:
Note: If you’re connected via the interface whose DHCP lease you’re renewing you may get disconnected, unless you’re connected via a statically configured interface or the console.
This works on Ubuntu 12.04 LTS:
When I do that, Network Manager asks for a new DHCP lease.
I prefer this to manually (re)starting dhclient because I’ve had problems by stale dhclient processes (ones not managed by Network Manager) spontaneously and incorrectly reconfiguring my network settings at random points when it’s least convenient.
Maverick comes with nmcli , a command line interface to the network manager.
I can’t see a simple way of telling it to renew a lease, but running:
Gets you a connection list, and running:
Takes the connection down and back up. It may be possible to do something similar with the device instead.
The connection list seems to include all connections, so this will probably work with PPTP and VPN connections too.
Having just upgraded to Maverick Meerkat Beta 1, something broke in my standard, vanilla eth0 configuration which I’ve not debugged yet. The quick and dirty workaround has been
which notices that there was a (possibly dead) client already and obtains a new lease:
This isn’t a fix, just a hack. I’ll follow-up when I figure what went bad.
Lacking this specific capability via nmcli , I believe the best CLI solution is to release and kill the dhclient instance managed by NetworkManager, rather than starting a new one. NM will then bring it back up again automatically, avoiding having to restart the entire NM infrastructure.
- -v — make verbose
- -r — release and shut down
- -pf — pid file of NM’s instance
How you renew the DHCP lease depends on the DHCP client the system is using.
Since 16.04, Ubuntu has used Netplan- a high-level abstraction- to set the networking gears in the renderer: directive in the config file /etc/netplan/fileName.yaml .
Where renderer: NetworkManager , restart NetworkManager to renew the lease
Where renderer: networkd , then dhclient is used to renew the lease
As a general rule at the time of this writing:
Server versions of Ubuntu use systemd-networkd to control the networking in netplan
Desktop versions of Ubuntu use NetworkManager.
Obviously if you’re a SysAdmin handling systems others have implemented, they could have changed the value of renderer to change the default, so worth checking the netplan config file to validate which system is authoritative.
Networking → How to force release DHCP IP address from Ubuntu 16.04.3 LTS and set a static IP
So I don’t know if you are like me and always seems to forget the little things in life, but if you are, have no fear! I wrote this down so you and I could remember the simple things that make developers look cool in the DevOps world.
Problem
No matter how many times I do:
I still get the same IP address from the previous time.
Solution
- Go to Terminal.
First thing you want to do is to have the dhclient force release the IP address:
Then you can go into your network interfaces file and create your static ip changes
You’ll see something similar to this
Now we want to add our static ip information. Since I am doing this on a dev box, its nothing fancy, if you need more information included in your configuration, I would check out Ubuntu ManPage on Network Interfaces