Linux下选择IPv6主地址是按照一定规则来的 #RFC3484 (davidc)
- Prefer same address. (i.e. destination is local machine)
- Prefer appropriate scope. (i.e. smallest scope shared with the destination)
- Avoid deprecated addresses.
- Prefer home addresses.
- Prefer outgoing interface. (i.e. prefer an address on the interface we're sending out of)
- Prefer matching label.
- Prefer public addresses.
- Use longest matching prefix.
如果你有一个/64的段,并且为网卡分配了多个同段地址的时候,Linux会自动选择最后添加的地址作为主地址。
要设定个性化主地址,可以在添加非主地址的时候加上[lifetime]属性,将preferred_lft设为0。Linux将自动地把这些地址排除在主地址选择范围之外。
例:
1 ip addr -6 add xx:xx:xx:xx::8888/xx dev eth0 # 这个地址将包括在主地址选择中
2 ip addr -6 add xx:xx:xx:xx::A/xx dev eth0 preferred_lft 0 # 这个地址不会当做主地址
Works Cited:
1. davidc, "IPv6 Source Address Selection on Linux" May 11, 2009, Retrieved from http://www.davidc.net/networking/ipv6-source-address-selection-linux
转载于:https://www.cnblogs.com/msg7086/archive/2012/05/12/debian_set_primary_ipv6_address.html
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.
asked Mar 22 '14 at 4:16
25111 gold badge55 silver badges1616 bronze badges
1 Answer
3
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.
answered Mar 22 '14 at 11:11
2,1311111 silver badges1313 bronze badges
-
This works great! Thanks for the tip about tentative addresses, too; that would have probably taken me a while to figure out why it would work when run manually but not on system boot. – Dan Mar 22 '14 at 12:06
-
3
I just wrote a script that waits for given addresses to leave the tentative state. Might be useful in init scripts: pastebin.com/sAgRxbPY – Sander Steffann Mar 22 '14 at 17:36
-
https://askubuntu.com/questions/437614/set-primary-ipv6-address