105

What is the Linux command to clear IP addresses of an interface without bringing it down and/or restarting network services?

Seems strange that ifconfig is able to change IP addresses but has no option to remove them, or am I wrong?

jackhab
  • 2,970
  • 1
    @Andy: I think you misunderstood the question. @jackhab wants to unassign an address from the interface, not set it to 0.0.0.0 -- it's just how it is done with ifconfig. – u1686_grawity Jun 17 '10 at 10:36
  • @grawity Cheers. Unassigning an address is switching the NIC off to all intents and purposes? – Andy Jun 17 '10 at 11:03
  • @Andy: Not necessarily. One could still watch incoming packets. Also, a NIC can have multiple addresses (though it doesn't apply in this case). – u1686_grawity Jun 17 '10 at 21:33
  • See also https://serverfault.com/questions/407676/deleting-all-ips-on-an-interface-with-iproute2 – pevik Jul 17 '17 at 09:02
  • To clear the IPs of all interfaces that are up you can use ip addr flush up – ws6079 May 19 '20 at 09:19

4 Answers4

186

Use ip from iproute2. (You need to also specify the prefix length though.)

ip addr del 10.22.30.44/16 dev eth0

To remove all addresses (in case you have multiple):

ip addr flush dev eth0
u1686_grawity
  • 452,512
  • Works, but when I do want to a non-dev permanent change, I get an error saying "eth0 is garbage" (lol). i guess i need to manually edit that file, just can't remember the name right now – The Onin Apr 11 '17 at 02:22
  • When setting up a kubernetes cluster I accidently ran a command that created an interface and used an IP address that was being used so I needed to delete that IP address but leave the interface around. This command fixed my problem. I used sudo ip addr del 192.168.0.1/24 dev weave. See https://unix.stackexchange.com/questions/646326/how-do-i-remove-an-ip-address-from-an-interface – PatS Apr 23 '21 at 02:45
30

As simple as ifconfig eth0 0.0.0.0. They should have put it in the manual.

Giacomo1968
  • 55,001
jackhab
  • 2,970
18

To remove all adreses from all interfaces i used for loop:

for i in $(ls /sys/class/net/) ; do
    /usr/sbin/ip addr flush $i &
done
eri
  • 369
2

Perhaps you are just looking to get a new IP from the [DHCP-enabled] router? In this case call

dhclient eth0

CodyBugstein
  • 1,575
  • THAT worked, whereas assigning an IP didn't somehow get the network working. Thank you. – Rich_F Sep 25 '19 at 11:56
  • 1
    The only problem here is that now you coulld have two ips assigned as dhclient just asks for one more IP not checking if one is already there nor deleting the old ips. So delete the unused IP first before you call dhclient – sebisnow Jul 01 '20 at 08:08