Networking

預設情況下,preferred_lft 如何為 IPv6 設置?

  • August 9, 2019

在帶有 Debian 和 Ubuntu 的 Hetzner Cloud 上,我注意到預設 IPv6 的 preferred_lft 為 0 秒。因此它被“棄用”,並且在添加另一個 inet6 時會導致問題,因為優先考慮未棄用的 ipv6。

root@debian-2gb-nbg1-1:~# ip -6 a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 state UNKNOWN qlen 1000
   inet6 ::1/128 scope host
      valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 state UP qlen 1000
   inet6 2a01:4f8:c2c:6daa::1/64 scope global deprecated
      valid_lft forever preferred_lft 0sec
   inet6 fe80::9400:ff:fe2d:6848/64 scope link
      valid_lft forever preferred_lft forever

這是配置:

root@debian-2gb-nbg1-1:~# cat /etc/network/interfaces.d/50-cloud-init.cfg
# This file is generated from information provided by
# the datasource.  Changes to it will not persist across an instance.
# To disable cloud-init's network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp
   dns-nameservers 213.133.99.99 213.133.98.98 213.133.100.100

auto eth0:0
iface eth0:0 inet6 static
   address 2a01:4f8:c2c:6daa::1/64
   gateway fe80::1
   post-up route add -net :: netmask 0 gw fe80::1%eth0 || true
   pre-down route del -net :: netmask 0 gw fe80::1%eth0 || true

0sec是從哪裡來的?

解決方案是不為 IPv6 使用虛擬介面:

root@debian-2gb-nbg1-1:~# cat /etc/network/interfaces.d/50-cloud-init.cfg
# This file is generated from information provided by
# the datasource.  Changes to it will not persist across an instance.
# To disable cloud-init's network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp
   dns-nameservers 213.133.99.99 213.133.98.98 213.133.100.100
iface eth0 inet6 static
   address 2a01:4f8:c2c:6daa::1/64
   gateway fe80::1
   post-up route add -net :: netmask 0 gw fe80::1%eth0 || true
   pre-down route del -net :: netmask 0 gw fe80::1%eth0 || true

這樣就不會出現preferred_lft 問題,IPv6 也不會被標記為已棄用。

root@debian-2gb-nbg1-1:~# ip -6 a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 state UNKNOWN qlen 1000
   inet6 ::1/128 scope host
      valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 state UP qlen 1000
   inet6 2a01:4f8:c2c:6daa::1/64 scope global
      valid_lft forever preferred_lft forever
   inet6 fe80::9400:ff:fe2d:6848/64 scope link
      valid_lft forever preferred_lft forever

引用自:https://serverfault.com/questions/978664