I met the same issue on both CentOS 7.9 & Oracle Linux 7.9 in Oracle Cloud. And found the root cause is the OS doesn't set the default IPv6 gateway based on the router advertisement. The issue is disappeared after we set the default gateway properly.
Here is the script to get the IPv6 address and set the default gateway, say names configure_ipv6.sh
:
#!/bin/bash # 检查是否具有 root 权限 if [[ $EUID -ne 0 ]]; then echo "Error: This script must be run as root" 1>&2 exit 1 fi # 指定要使用的网络接口。Please change to your interface name INTERFACE="ens3" # 使用 dhclient 获取 IPv6 配置信息 dhclient -6 $INTERFACE # 使用 rdisc6 获取路由通告信息 output=$(rdisc6 $INTERFACE 2>/dev/null) # 提取 fe80 开头的链路本地地址作为默认网关 ipv6_gateway=$(echo "$output" | grep -oP 'fe80::[0-9a-f:]{1,39}') # 检查是否成功获取到网关地址 if [[ -z "$ipv6_gateway" ]]; then echo "Error: Could not find IPv6 gateway address from rdisc6 output." exit 1 fi echo "Found IPv6 gateway: $ipv6_gateway" # 添加默认 IPv6 路由 ip -6 route add default via "$ipv6_gateway" dev "$INTERFACE" # 检查是否成功添加路由 if [[ $? -eq 0 ]]; then echo "Successfully added default IPv6 route via $ipv6_gateway on $INTERFACE." else echo "Error: Failed to add default IPv6 route." fi
Meanwhile, to run above script automatically in boot-up time, you might want to create a file named /etc/systemd/system/configure-ipv6.service
with below content:
[Unit] Description=Configure IPv6 address and Add IPv6 Default Route via Script After=network-online.target Wants=network-online.target [Service] Type=oneshot ExecStart=/PATH/TO/YOUR/SCRIPT/configure_ipv6.sh # Check the answer for the content RemainAfterExit=true [Install] WantedBy=multi-user.target
and then run below CLI to make sure it would be ran in boot-up time:
sudo systemctl daemon-reload sudo systemctl enable configure-ipv6
Check the status:
sudo systemctl status configure-ipv6