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?

little-dude
  • 183
  • 1
  • 9
jonderry
  • 197
  • 4
  • 13
  • 3
    Don't use `ip addr flush` of course. – Michael Hampton Jun 09 '14 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 Jun 09 '14 at 22:13
  • Just delete the address you configured, then. Leave everything else alone. – Michael Hampton Jun 09 '14 at 22:14
  • How do I delete the address I configured? – jonderry Jun 09 '14 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 Jun 09 '14 at 22:21
  • The initial ipv6 address.. that's the [link local](http://en.wikipedia.org/wiki/Link-local_address) address. This is set up during the network initialization (read the scripts). Assuming it's CentOS/RHEL a simple `service network restart` would bring it back. That IPv6 address has nothing to do with dhcp, that's why it wasn't re-added. There's a whole lot more to setting up the network than just running dhclient. –  Jun 10 '14 at 01:08
  • OK, that makes sense, but I get `stop: Job failed while stopping` and `start: Job is already running: networking`, and this fails to reset the ipv6 address. – jonderry Jun 10 '14 at 01:13

1 Answers1

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.

little-dude
  • 183
  • 1
  • 9