Обновить ip адрес через командную строку linux

How to Release old IP or force Renew DHCP lease IP in Linux

How to Release old IP or force Renew DHCP lease IP in Linux 1

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

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 .

How to Release old IP or force Renew DHCP lease IP in Linux 2

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.

Please don’t ever try to release the IP over SSH connection as it will disconnect you from the host system and you may not be able to reconnect unless a new IP is assigned which is known to you.

Читайте также:  Canon mf4320d драйвер linux

How to Release old IP or force Renew DHCP lease IP in Linux 3

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.

How to Release old IP or force Renew DHCP lease IP in Linux 2

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.

How to Release old IP or force Renew DHCP lease IP in Linux 5

Sometimes even if you try to kill the dhclient process using kill -9 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.

How to Release old IP or force Renew DHCP lease IP in Linux 6

In my case process ID(PID) is 2167 , so to kill this one we are using kill -9 2167 command as shown below.

How to Release old IP or force Renew DHCP lease IP in Linux 7

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.

How to Release old IP or force Renew DHCP lease IP in Linux 8

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.

Please note if you are using RHEL/CentOS based systems, then you need to check /etc/sysconfig/network-scripts/ifcfg- configuration file.

How to Release old IP or force Renew DHCP lease IP in Linux 9

You can check the running status of Network Manager by using sudo systemctl status NetworkManager command as shown below.

How to Release old IP or force Renew DHCP lease IP in Linux 10

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.

How to Release old IP or force Renew DHCP lease IP in Linux 11

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.

Читайте также:  Moc плеер настройка linux

How to Release old IP or force Renew DHCP lease IP in Linux 12

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.

How to Release old IP or force Renew DHCP lease IP in Linux 13

Then bring up the connection using nmcli con up id «Wired connection 1» command as shown below.

How to Release old IP or force Renew DHCP lease IP in Linux 14

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.

How to Release old IP or force Renew DHCP lease IP in Linux 15

After applying above solution, you can see that you will be able to release the IP as shown below.

How to Release old IP or force Renew DHCP lease IP in Linux 16

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.

How to Release old IP or force Renew DHCP lease IP in Linux 17

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 or release a dynamic IP address in Linux

Tux the Linux penguin.

Computer Hope

The DHCP client in Linux is called dhclient. It requests dynamic IP addresses from the DHCP server, which «leases» addresses to clients for a set time. dhclient can be invoked manually to «release» the client’s currently assigned IP address, and get another address from the DHCP server.

Normally, dhclient produces no output, to see what it’s doing, we need to give it the -v (verbose) option. We need to run dhclient as root with the sudo command, because changing the system networking configuration requires escalated privileges.

Let’s start by running dhclient verbosely, without any other options. It should reach out and say «hello» to our DHCP server, which, in our example is home router.

Internet Systems Consortium DHCP Client 4.2.2 Copyright 2004-2011 Internet Systems Consortium. All rights reserved. For info, please visit https://www.isc.org/software/dhcp/ Listening on LPF/wlan0/68:a3:c4:93:47:46 Sending on LPF/wlan0/68:a3:c4:93:47:46 Sending on Socket/fallback DHCPDISCOVER on wlan0 to 255.255.255.255 port 67 interval 4 DHCPREQUEST on wlan0 to 255.255.255.255 port 67 DHCPOFFER from 192.168.2.1 DHCPACK from 192.168.2.1 RTNETLINK answers: File exists bound to 192.168.2.4 -- renewal in 42516 seconds.

The above output tells us that dhclient requested an address from the DHCP server (DHCPREQUEST). It sent this request from our wireless Internet interface (wlan0). The DHCP server responded with a simple acknowledgement of our request (DHCPACK). That’s because we already had an IP address assigned to us, so the DHCP server didn’t have anything to do. Note that the DHCP server also told us how long our dynamic IP address will last: 42516 seconds (about 12 hours from now, at which point it will be renewed automatically).

Читайте также:  Шрифт в браузере linux

So let’s manually release our address with -r. Let’s keep the -v option in there so we can see what’s going on:

Internet Systems Consortium DHCP Client 4.2.2 Copyright 2004-2011 Internet Systems Consortium. All rights reserved. For info, please visit https://www.isc.org/software/dhcp/ Listening on LPF/wlan0/68:a3:c4:93:47:46 Sending on LPF/wlan0/68:a3:c4:93:47:46 Sending on Socket/fallback DHCPRELEASE on wlan0 to 192.168.2.1 port 67

In the above example, the command tells the DHCP server to release the IP address.

On some variants of Linux or Unix, releasing your IP address this way may also inherently bring down your network interface. In this case, use your network manager to re-connect to your network. This process varies from OS to OS; if you’re unsure how to connect to your network, consult the documentation of your specific OS. If you’re using the X Window System, you can usually reconnect by clicking the network icon in your system tray, and selecting the Connect option.

  • dhclient — documentation of the dhclient command and its options.
  • ifconfig — documentation of the ifconfig command, a system administration tool used to configure network interfaces.
  • Linux help and support.

Источник

Оцените статью
Adblock
detector