Manually Configuring IPv4 and IPv6 Policy-based Routes for a Linux ECS with Multiple Network Interfaces (Debian)
Scenarios
This section describes how to configure policy-based routes for an ECS with two network interfaces running Debian 12.0.0 64bit.
- IPv4: If IPv4 communication between ECSs with multiple network interfaces is required, you need to configure IPv4 routes by referring to Configuring Policy-based Routes for Linux ECSs Using IPv4 Addresses (Debian).
- IPv6: If IPv6 communication between ECSs with multiple network interfaces is required, you need to configure IPv6 routes by referring to Configuring Policy-based Routes for Linux ECSs Using IPv6 Addresses (Debian).
- IPv4/IPv6 dual stack: If both IPv4 and IPv6 communications between ECSs with multiple network interfaces are required, you need to configure both IPv4 and IPv6 routes by referring to Configuring Policy-based Routes for Linux ECSs Using IPv4 Addresses (Debian) and Configuring Policy-based Routes for Linux ECSs Using IPv6 Addresses (Debian).
For details about the background knowledge and networking of an ECS with two network interfaces, see Overview.
Configuring Policy-based Routes for Linux ECSs Using IPv4 Addresses (Debian)
- Collect the ECS network interface information required for configuring policy-based routes.
For details, see Collecting ECS Network Information.
In this example, the network information of the ECS is shown in Table 1.
Table 1 Linux ECSs using IPv4 addresses (Debian) Type
Primary Network Interface
Extended Network Interface
Source
- Network interface IPv4 address: 10.0.0.138
- Subnet IPv4 CIDR block: 10.0.0.0/24
- Subnet IPv4 gateway: 10.0.0.1
- Network interface IPv4 address: 10.0.1.29
- Subnet IPv4 CIDR block: 10.0.1.0/24
- Subnet IPv4 gateway: 10.0.1.1
Destination
Network interface IPv4 address: 10.0.2.179
N/A
- Log in to the source ECS.
For details, see How Do I Log In to My ECS?
- Check whether the source ECS can use its primary network interface to communicate with the destination ECS:
Before configuring policy-based routes, ensure that the source ECS can use its primary network interface to communicate with the destination ECS.
ping -I <IP-address-of-the-primary-network-interface-on-the-source-ECS> <IP-address-of-the-destination-ECS>
Example command:
ping -I 10.0.0.138 10.0.2.179
If information similar to the following is displayed, the source ECS can use its primary network interface to communicate with the destination ECS.root@ecs-s:~# ping -I 10.0.0.138 10.0.2.179 PING 10.0.2.179 (10.0.2.179) from 10.0.0.138 : 56(84) bytes of data. 64 bytes from 10.0.2.179: icmp_seq=1 ttl=64 time=0.220 ms 64 bytes from 10.0.2.179: icmp_seq=2 ttl=64 time=0.166 ms 64 bytes from 10.0.2.179: icmp_seq=3 ttl=64 time=0.127 ms ^C -- 10.0.2.179 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2043ms rtt min/avg/max/mdev = 0.127/0.171/0.220/0.038 ms
- Query the network interface names of the source ECS: Search for the network interface names based on IP addresses.
- The IP address of the primary network interface is 10.0.0.138, and its name is eth0.
- The IP address of the extended network interface is 10.0.1.29, and its name is eth1.
root@ecs-s:~# ip addr ... 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 gdisc mq state UP group default qlen 1000 link/ether fa:16:3e:30:30:ba brd ff:ff:ff:ff:ff:ff inet 10.0.0.138/24 brd 10.0.0.255 scope global dynamic noprefixroute eth0 valid lft 315359713sec preferred _lft 315359713sec inet6 fe80::c624:9977:1f61:cf54/64 scope link noprefixroute valid_lft forever preferred_lft forever 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 gdisc mq State UP group default qlen 1000 link/ether fa:16:3e:30:31:4d brd ff:ff:ff:ff:ff:ff inet 10.0.1.29/24 brd 10.0.1.255 scope global dynamic noprefixroute eth1 valid_lft 315359713sec preferred_lft 315359713sec inet6 fe80::eda5:599b:e006:d2d3/64 scope link noprefixroute valid_lft forever preferred_lft forever - Configure temporary routes for the source ECS.
- Configure policy-based routes for both the primary and extended network interfaces.
- Primary network interface
ip route add default via <subnet-gateway> dev <network-interface-name> table <route-table-name>
ip route add <subnet-CIDR-block> dev <network-interface-name> table <route-table-name>
ip rule add from <network-interface-address> table <route-table-name>
- Extended network interface
ip route add default via <subnet-gateway> dev <network-interface-name> table <route-table-name>
ip route add <subnet-CIDR-block> dev <network-interface-name> table <route-table-name>
ip rule add from <network-interface-address> table <route-table-name>
Configure the parameters as follows:Example commands:
- Primary network interface
ip route add default via 10.0.0.1 dev eth0 table 10
ip route add 10.0.0.0/24 dev eth0 table 10
ip rule add from 10.0.0.138 table 10
- Extended network interface
ip route add default via 10.0.1.1 dev eth1 table 20
ip route add 10.0.1.0/24 dev eth1 table 20
ip rule add from 10.0.1.29 table 20
If the ECS has multiple network interfaces, configure policy-based routes for all network interfaces one by one.
- Primary network interface
- Check whether the policy-based routes are added.
ip route show table <route-table-name-of-the-primary-network-interface>
ip route show table <route-table-name-of-the-extended-network-interface>
The route table name is the one configured in 5.a.
Example commands:
ip rule
ip route show table 10
ip route show table 20
If information similar to the following is displayed, the policy-based routes have been added.root@ecs-s:~# ip rule 0: from all lookup local 32764: from 10.0.1.29 lookup 20 32765: from 10.0.0.138 lookup 10 32766: from all lookup main 32767: from all lookup default root@ecs-s:~# ip route show table 10 default via 10.0.0.1 dev eth0 10.0.0.0/24 dev eth0 scope link root@ecs-s:~# ip route show table 20 default via 10.0.1.1 dev eth1 10.0.1.0/24 dev eth1 scope link
- Check whether the source and destination ECSs can communicate with each other.
ping -I <IP-address-of-the-primary-network-interface-on-the-source-ECS> <IP-address-of-the-destination-ECS>
ping -I <IP-address-of-the-extended-network-interface-on-the-source-ECS> <IP-address-of-the-destination-ECS>
Example commands:
ping -I 10.0.0.138 10.0.2.179
ping -I 10.0.1.29 10.0.2.179
If information similar to the following is displayed, both the network interfaces of the source ECS can communicate with the destination ECS.root@ecs-s:~# ping -I 10.0.0.138 10.0.2.179 PING 10.0.2.179 (10.0.2.179) from 10.0.0.138 : 56(84) bytes of data. 64 bytes from 10.0.2.179: icmp_seq=1 ttl=64 time=0.182 ms 64 bytes from 10.0.2.179: icmp_seq=2 ttl=64 time=0.156 ms 64 bytes from 10.0.2.179: icmp_seq=3 ttl=64 time=0.118 ms ^C --- 10.0.2.179 ping statistics -- 3 packets transmitted, 3 received, 0% packet loss, time 2046ms rtt min/avg/max/mdev = 0.118/0.152/0.182/0.026 ms root@ecs-s:~# ping -I 10.0.1.29 10.0.2.179 PING 10.0.2.179 (10.0.2.179) from 10.0.1.29 : 56(84) bytes of data. 64 bytes from 10.0.2.179: icmp_seq=1 ttl=64 time=1.13 ms 64 bytes from 10.0.2.179: icmp_seq=2 ttl=64 time=0.177 ms 64 bytes from 10.0.2.179: icmp_seq=3 ttl=64 time=0.123 ms ^C --- 10.0.2.179 ping statistics -- 3 packets transmitted, 3 received, 0% packet loss, time 2022ms rtt min/avg/max/mdev = 0.123/0.476/1.130/0.462 ms
- Configure policy-based routes for both the primary and extended network interfaces.
- Configure permanent routes for the source ECS.
- Run the following command to add the network-routes.service file to the systemd service:
vi /etc/systemd/system/network-routes.service
- Press i to enter the editing mode.
- Add the following content to the end of the file:
[Unit] Description=Network Routes Configuration After=network.target [Service] Type=oneshot RemainAfterExit=yes ExecStart=/bin/bash -c 'for((x=0; x<10; x++)); do [[ $(ip addr show eth0 | grep -w 10.0.0.138 >/dev/null 2>&1 && echo 1) ]] && break; sleep 1; done; ip route flush table 10; ip route add default via 10.0.0.1 dev eth0 table 10; ip route add 10.0.0.0/24 dev eth0 table 10; ip rule add from 10.0.0.138 table 10; for((x=0; x<10; x++)); do [[ $(ip addr show eth1 | grep -w 10.0.1.29 >/dev/null 2>&1 && echo 1) ]] && break; sleep 1; done; ip route flush table 20; ip route add default via 10.0.1.1 dev eth1 table 20; ip route add 10.0.1.0/24 dev eth1 table 20; ip rule add from 10.0.1.29 table 20; ip rule add to 169.254.169.254 table main' [Install] WantedBy=multi-user.target
The parameters are as follows:
- for loop: Check whether eth0 or eth1 has obtained an IPv4 address (eth0: 10.0.0.138; eth1: 10.0.1.29) every second up to 10 times.
- ip route flush table route-table-name: Running this command will delete existing routes from the specified route table. This prevents new routes from being affected.
- Policy-based routes of the primary network interface: Set it to the same value as that in 5.a.
- Policy-based routes of the extended network interface: Set it to the same value as that in 5.a.
- ip rule add to 169.254.169.254 table main: Set the Cloud-Init address to the same value as that in this example.
- Press ESC to exit and enter :wq! to save the configuration.
- Run the following commands to reload the systemd configuration and start the service:
systemctl enable network-routes.service
If information similar to the following is displayed, the service is started:root@ecs-s:~# systemctl daemon-reload root@ecs-s:~# systemctl enable network-routes.service Created symlink /etc/systemd/system/multi-user.target.wants/network-routes.service → /etc/systemd/system/network-routes.service.
- Run the following command to restart the source ECS:
Policy-based routes added to the network-routes.service file only work after the source ECS is restarted. Ensure that workloads on the ECS will not be affected before restarting the ECS.
- Repeat 5.b to 5.c to check whether the policy-based routes are added and whether the source ECS and the destination ECS can communicate with each other.
- Run the following command to add the network-routes.service file to the systemd service:
Configuring Policy-based Routes for Linux ECSs Using IPv6 Addresses (Debian)
- Collect the ECS network interface information required for configuring policy-based routes.
For details, see Collecting ECS Network Information.
In this example, the network information of the ECS is shown in Table 2.
Table 2 Linux ECSs using IPv6 addresses (Debian) Type
Primary Network Interface
Extended Network Interface
Source
- Network interface IPv4 address: 10.0.0.138
- Network interface IPv6 address: 2407:c080:1880:9ba:57d9:40d3:f968:81bb
- Subnet IPv6 CIDR block: 2407:c080:1880:9ba::/64
- Subnet IPv6 gateway: 2407:c080:1880:9ba::1
- Network interface IPv4 address: 10.0.1.29
- Network interface IPv6 address: 2407:c080:1880:9bb:70e0:4aa8:f6c3:5504
- Subnet IPv6 CIDR block: 2407:c080:1880:9bb::/64
- Subnet IPv6 gateway: 2407:c080:1880:9bb::1
Destination
- Network interface IPv4 address: 10.0.2.179
- Network interface IPv6 address: 2407:c080:1880:9bc:2fba:3a48:9262:2483
N/A
- Log in to the source ECS.
For details, see How Do I Log In to My ECS?
- Check whether the ECSs have IPv6 enabled and have IPv6 addresses.
Perform this step for both the source and destination ECSs to ensure that the ECSs have IPv6 addresses. Otherwise, the ECSs cannot communicate with each other using IPv6 addresses.
ECSs in this example run Debian 12.0.0 (64-bit). For details about how to assign IPv6 addresses for ECSs running other OSs, see Linux (Manually Enabling Dynamic Assignment of IPv6 Addresses).
- Run the following command to check whether the source ECS has IPv6 addresses: Information similar to the following is displayed. eth0 and eth1 are the network interfaces of the ECS. There is an inet6 entry starting with fe80, which indicates that the ECS has IPv6 enabled. There is also an inet6 entry starting with 2407, which indicates that the ECS has obtained an IPv6 address automatically. In this example, no additional configuration is required for the image.
root@ecs-s:~# ip addr ... 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisC mq state UP group default qlen 1000 link/ether fa:16:3e:30:30:ba brd ff:ff:ff:ff:ff:ff altname enpos3 altname ens3 inet 10.0.0.138/24 brd 10.0.0.255 scope global dynamic noprefixroute eth0 valid_lft 315359957sec preferred_lft 315359957sec inet6 2407:c080:1880:9ba:57d9:40d3:f968:81bb/128 scope global dynamic noprefixroute valid_lft 7158sec preferred_lft 7158sec inet6 fe80::19ba:54cd:a7f6:615e/64 scope link noprefixroute valid_lft forever preferred_lft forever 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 gdisc mq state UP group default qlen 1000 link/ether fa:16:3e:30:31:4d brd ff:ff:ff:ff:ff:ff altname enp4s4 inet 10.0.1.29/24 brd 10.0.1.255 scope global dynamic noprefixroute eth1 valid_lft 315359957sec preferred_lft 315359957sec inet6 2407:c080:1880:9bb:70e0:4aa8:f6c3:5504/128 scope global dynamic noprefixroute valid_lft 7158sec preferred_lft 7158sec inet6 fe80::9dd7:9fd6:611d:9f43/64 scope link noprefixroute valid_lft forever preferred_lft forever - Locate the network interface names based on their IP addresses in the previous command output.
- Run the following command to check whether the source ECS has IPv6 addresses:
- Log in to the source ECS and check whether it can communicate with the destination ECS.
Before configuring policy-based route for an ECS with multiple network interfaces, ensure that at least one of the primary or extended network interface of the source ECS can communicate with the destination ECS.
ping6 -I <IP-address-of-a-network-interface-on-the-source-ECS> <IP-address-of-the-destination-ECS>
Example command:
ping6 -I 2407:c080:1880:9ba:57d9:40d3:f968:81bb 2407:c080:1880:9bc:2fba:3a48:9262:2483
If information similar to the following is displayed, the source ECS can communicate with the destination ECS.root@ecs-s:~# ping6 -I 2407:c080:1880:9ba:57d9:40d3:f968:81bb 2407:c080:1880:9bc:2fba:3a48:9262:2483 PING 2407:c080:1880:9bc:2fba:3a48:9262:2483(2407:c080:1880:9bc:2fba:3a48:9262:2483) from 2407:c080:1880:9ba:57d9:40d3:f968:81bb : 56databytes 64 bytes from 2407:c080:1880:9bc:2fba:3a48:9262:2483: icmp-seg=1 ttl=64 time=0.512 ms 64 bytes from 2407:c080:1880:9bc:2fba:3a48:9262:2483: icmp_seq=2 ttl=64 time=0.478 ms 64 bytes from 2407:c080:1880:9bc:2fba:3a48:9262:2483: icmp_seq=3 ttl=64 time=0.461 ms ^C -- 2407:c080:1880:9bc:2fba:3a48:9262:2483 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2039ms rtt min/avg/max/mdev = 0.461/0.483/0.512/0.021 ms
- Log in to the source ECS and configure temporary routes for the ECS.
- Configure policy-based routes for both the primary and extended network interfaces.
- Primary network interface
ip -6 route add default via <subnet-gateway> dev <network-interface-name> table <route-table-name>
ip -6 route add <subnet-CIDR-block> dev <network-interface-name> table <route-table-name>
ip -6 rule add from <network-interface-address> table <route-table-name>
- Extended network interface
ip -6 route add default via <subnet-gateway> dev <network-interface-name> table <route-table-name>
ip -6 route add <subnet-CIDR-block> dev <network-interface-name> table <route-table-name>
ip -6 rule add from <network-interface-address> table <route-table-name>
Configure the parameters as follows:Example commands:
- Primary network interface
ip -6 route add default via 2407:c080:1880:9ba::1 dev eth0 table 10
ip -6 route add 2407:c080:1880:9ba::/64 dev eth0 table 10
ip -6 rule add from 2407:c080:1880:9ba:57d9:40d3:f968:81bb table 10
- Extended network interface
ip -6 route add default via 2407:c080:1880:9bb::1 dev eth1 table 20
ip -6 route add 2407:c080:1880:9bb::/64 dev eth1 table 20
ip -6 rule add from 2407:c080:1880:9bb:70e0:4aa8:f6c3:5504 table 20
If the ECS has multiple network interfaces, configure policy-based routes for all network interfaces one by one.
- Primary network interface
- Check whether the policy-based routes are added.
ip -6 route show table <route-table-name-of-the-primary-network-interface>
ip -6 route show table <route-table-name-of-the-extended-network-interface>
The route table name is the one configured in 5.a.
Example commands:
ip -6 rule
ip -6 route show table 10
ip -6 route show table 20
If information similar to the following is displayed, the policy-based routes have been added.root@ecs-s:~# ip -6 rule 0: from all lookup local 32764: from 2407:c080:1880:9bb:70e0:4aa8:f6c3:5504 lookup 20 32765: from 2407:c080:1880:9ba:57d9:40d3:f968:81bb lookup 10 32766: from all lookup main root@ecs-s:~# ip -6 route show table 10 2407:c080:1880:9ba::/64 dev eth0 metric 1024 pref medium default via 2407:c080:1880:9ba::1 dev eth0 metric 1024 pref medium root@ecs-s:~# ip -6 route show table 20 2407:c080:1880:9bb::/64 dev eth1 metric 1024 pref medium default via 2407:c080:1880:9bb::1 dev eth1 metric 1024 pref medium
- Check whether the source and destination ECSs can communicate with each other.
ping6 -I <IP-address-of-the-primary-network-interface-on-the-source-ECS> <IP-address-of-the-destination-ECS>
ping6 -I <IP-address-of-the-extended-network-interface-on-the-source-ECS> <IP-address-of-the-destination-ECS>
Example commands:
ping6 -I 2407:c080:1880:9ba:57d9:40d3:f968:81bb 2407:c080:1880:9bc:2fba:3a48:9262:2483
ping6 -I 2407:c080:1880:9bb:70e0:4aa8:f6c3:5504 2407:c080:1880:9bc:2fba:3a48:9262:2483
If information similar to the following is displayed, both the network interfaces of the source ECS can communicate with the destination ECS.root@ecs-s:~# ping6 -I 2407:c080:1880:9ba:57d9:40d3:f968:81bb 2407:c080:1880:9bc:2fba:3a48:9262:2483 PING 2407:c080:1880:9bc:2fba:3a48:9262:2483(2407:c080:1880:9bc:2fba:3a48:9262:2483) from 2407:c080:1880:9ba:57d9:40d3:f968:81bb : 56 data bytes 64 bytes from 2407:c080:1880:9bc:2fba:3a48:9262:2483: icmp_seq=1 ttl=64 time=0.451 ms 64 bytes from 2407:c080:1880:9bc:2fba:3a48:9262:2483: icmp_seq=2 ttl=64 time=0.385 ms 64 bytes from 2407:c080:1880:9bc:2fba:3a48:9262:2483: icmp_seq=3 ttl=64 time=0.352 ms ^C --- 2407:c080:1880:9bc:2fba:3a48:9262:2483 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2035ms rtt min/avg/max/mdev = 0.352/0.396/0.451/0.041 ms root@ecs-s:~# ping6 -I 2407:c080:1880:9bb:70e0:4aa8:f6c3:5504 2407:c080:1880:9bc:2fba:3a48:9262:2483 PING 2407:c080:1880:9bc:2fba:3a48:9262:2483(2407:c080:1880:9bc:2fba:3a48:9262:2483) from 2407:c080:1880:9bb:70e0:4aa8:f6c3:5504 : 56 data bytes 64 bytes from 2407:c080:1880:9bc:2fba:3a48:9262:2483: icmp_seq=1 ttl=64 time=0.418 ms 64 bytes from 2407:c080:1880:9bc:2fba:3a48:9262:2483: icmp_seq=2 ttl=64 time=0.370 ms 64 bytes from 2407:c080:1880:9bc:2fba:3a48:9262:2483: icmp_seq=3 ttl=64 time=0.347 ms 64 bytes from 2407:c080:1880:9bc:2fba:3a48:9262:2483: icmp_seq=4 ttl=64 time=0.357 ms ^C -- 2407:c080:1880:9bc:2fba:3a48:9262:2483 ping statistics_--- 4 packets transmitted, 4 received, 0% packet loss, time 3072ms rtt min/avg/max/mdev = 0.347/0.373/0.418/0.027 ms
- Configure policy-based routes for both the primary and extended network interfaces.
- Configure permanent routes for the source ECS.
- Run the following command to create the network-routes6.service file for the systemd service:
vi /etc/systemd/system/network-routes6.service
- Press i to enter the editing mode.
- Add the following content to the end of the file:
[Unit] Description=Network Routes Configuration After=network.target [Service] Type=oneshot RemainAfterExit=yes ExecStart=/bin/bash -c 'for((x=0; x<10; x++)); do [[ $(ip addr show eth0 | grep -w 2407:c080:1880:9ba:57d9:40d3:f968:81bb >/dev/null 2>&1 && echo 1) ]] && break; sleep 1; done; ip route flush table 10; ip -6 route add default via 2407:c080:1880:9ba::1 dev eth0 table 10; ip -6 route add 2407:c080:1880:9ba::/64 dev eth0 table 10; ip -6 rule add from 2407:c080:1880:9ba:57d9:40d3:f968:81bb table 10; for((x=0; x<10; x++)); do [[ $(ip addr show eth1 | grep -w 2407:c080:1880:9bb:70e0:4aa8:f6c3:5504 >/dev/null 2>&1 && echo 1) ]] && break; sleep 1; done; ip route flush table 20; ip -6 route add default via 2407:c080:1880:9bb::1 dev eth1 table 20; ip -6 route add 2407:c080:1880:9bb::/64 dev eth1 table 20; ip -6 rule add from 2407:c080:1880:9bb:70e0:4aa8:f6c3:5504 table 20' [Install] WantedBy=multi-user.target
The parameters are as follows:
- for loop: Check whether eth0 or eth1 has obtained an IPv6 address (eth0: 2407:c080:1880:9ba:57d9:40d3:f968:81bb; eth1: 2407:c080:1880:9bb:70e0:4aa8:f6c3:5504) every second up to 10 times.
- ip route flush table route-table-name: Running this command will delete existing routes from the specified route table. This prevents new routes from being affected.
- Policy-based routes of the primary network interface: Set it to the same value as that in 5.a.
- Policy-based routes of the extended network interface: Set it to the same value as that in 5.a.
- Press ESC to exit and enter :wq! to save the configuration.
- Run the following commands to reload the systemd configuration and start the service:
systemctl enable network-routes6.service
If information similar to the following is displayed, the service is started:root@ecs-s:/etc/netplan# systemctl daemon-reload root@ecs-s:/etc/netplan# systemctl enable network-routes6.service Created symlink /etc/systemd/system/multi-user.target.wants/network-routes6.service → /etc/systemd/system/network-routes6.service.
- Run the following command to restart the source ECS:
Policy-based routes added to the network-routes6.service file only work after the source ECS is restarted. Ensure that workloads on the ECS will not be affected before restarting the ECS.
- Repeat 5.b to 5.c to check whether the policy-based routes are added and whether the source ECS and the destination ECS can communicate with each other.
- Run the following command to create the network-routes6.service file for the systemd service:
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot