Ubuntu – Set primary IPv6 address

12.04ipv6networking

I've got a /64 of IPv6 addresses available to one of my servers (Ubuntu 12.04). I'm binding them like this:

auto eth0
    iface eth0 inet static
    address xxx.xxx.xxx.82
    netmask 255.255.255.248
    network xxx.xxx.xxx.80
    broadcast xxx.xxx.xxx.255
    gateway xxx.xxx.xxx.81

    iface eth0 inet6 static
    address xxxx::2
    netmask 64
    gateway xxxx::1

    pre-up echo 0 > /proc/sys/net/ipv6/conf/eth0/autoconf
    pre-up echo 0 > /proc/sys/net/ipv6/conf/eth0/accept_ra
    pre-up echo 0 > /proc/sys/net/ipv6/conf/eth0/accept_ra_defrtr
    pre-up echo 0 > /proc/sys/net/ipv6/conf/eth0/accept_ra_pinfo
    pre-up echo 0 > /proc/sys/net/ipv6/conf/eth0/accept_ra_rtr_pref

    up /sbin/ifconfig eth0 inet6 add xxxx::3/64
    # ... snip ...
    up /sbin/ifconfig eth0 inet6 add xxxx::25/64

This works, but applications are all using xxxx::25 for their outgoing requests unless specifically told otherwise. If I add xxxx::26 in my /etc/network/interfaces, they all start using that. It seems like they just use whatever the highest number is.

How do I specify a certain address to be used as the default? In my case, I happen to want to use the lowest address (xxxx::2), but I'd really like to know how to specify a particular one, should the need arise in the future.

Best Answer

You can solve this by modifying the default route. Every route in Linux has the option to specify the default source address. If you specify the xxxx::3 address in your route then that one will be used by default:

/sbin/ip -6 route del default
/sbin/ip -6 route add default via xxxx::1 src xxxx::3

If you show the routing table you will see the result:

/sbin/ip -6 route
default via xxxx::1 dev eth0  src xxxx::222  metric 1024

One thing to watch out for is that IPv6 addresses are in a tentative state until duplicate address detection has been performed. Linux will refuse to use a tentative address as the default source address. This means that you might need to add a short sleep before adding this route so that the address has time to come out of the tentative state and become usable.