4

I'm trying to write an application that can change the IP address of the local machine. When doing some experimentation, I've noticed that if I freshly connect to a wireless network, I am assigned an IPv6 address that is a function of the MAC address. Consider the following sequence of commands:

<disconnect wireless and reconnect>
$ ifconfig 
eth0      Link encap:Ethernet  HWaddr 00:0c:29:7d:7c:42  
          inet addr:192.168.2.98  Bcast:192.168.2.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe7d:7c42/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST...
$ sudo ip addr flush dev eth0
$ ifconfig 
eth0      Link encap:Ethernet  HWaddr 00:0c:29:7d:7c:42  
          UP BROADCAST RUNNING MULTICAST...
$ sudo dhclient eth0
$ ifconfig 
eth0      Link encap:Ethernet  HWaddr 00:0c:29:7d:7c:42  
          inet addr:192.168.2.98  Bcast:192.168.2.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST

Note that after flushing, I lose both the IPv4 and IPv6 addresses, but after renewing with dhclient, I only reinitialize my IPv4 address. Why is this? What can I do to be reassigned my IPv6 address automatically?

7
  • 3
    Don't use ip addr flush of course. Commented Jun 9, 2014 at 21:54
  • What should I use instead if I am configuring a static ip and then want to wipe out my configurations and then return to dhcp?
    – jonderry
    Commented Jun 9, 2014 at 22:13
  • Just delete the address you configured, then. Leave everything else alone. Commented Jun 9, 2014 at 22:14
  • How do I delete the address I configured?
    – jonderry
    Commented Jun 9, 2014 at 22:19
  • And even if that works, I'm still curious about the questions in the OP. Where did the ipv6 address come from initially, and why wasn't it re-added when I re-ran dhclient?
    – jonderry
    Commented Jun 9, 2014 at 22:21

1 Answer 1

8

Instead of flushing all the ip addresses from an interface, you can flush only IPv4 addresses. Here is an example where I have an interface with both an IPv4 and an IPv6 address:

# ip address show qemu-br2
5: qemu-br2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group default qlen 1000
link/ether ca:b8:97:12:0a:30 brd ff:ff:ff:ff:ff:ff
inet 192.168.20.1/24 scope global qemu-br2
   valid_lft forever preferred_lft forever
inet6 fe80::c8b8:97ff:fe12:a30/64 scope link
   valid_lft forever preferred_lft forever

To flush only the IPv4:

# ip -4 address flush qemu-br2

See that the IPv6 is still there:

# ip address show qemu-br2
5: qemu-br2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group default qlen 1000
link/ether ca:b8:97:12:0a:30 brd ff:ff:ff:ff:ff:ff
inet6 fe80::c8b8:97ff:fe12:a30/64 scope link
   valid_lft forever preferred_lft forever

Generally speaking, you can add -4 or -6 to make an iproute2 command act only on IPv4 or IPv6 addresses, respectively.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .