How to configure routes for dual NICs in linux
I. Environment:
Three directly connected computer hosts
PC1 <------> PC2 <------> PC3
Two NICs eth0 and eth1 are available on PC2, which are connected to PC3 and PC1 respectively.
------ Network cable
Ii. Requirements:
Configure PC2 as vro mode to forward packets between PC1 and PC3
Iii. Method:
After connecting the three hosts to the Internet using a crossover line as required by the Environment, assign network segments for the three hosts and configure IP addresses.
CIDR blocks and IP addresses are allocated as follows:
IP gateway is allocated to the CIDR Block of the host name.
PC1: 192.168.2.0/24 192.168.2.2/24 192.168.2.1
PC2: 192.168.2.0/24 192.168.2.1/24 'configuration of the NIC connected to PC1' eth1
192.168.1.0/24 192.168.1.1/24 'configuration of the NIC connected to PC3' eth0
PC3: 192.168.1.0/24 192.168.1.2/24 192.168.1.1
Configure the Host IP addresses as shown in the preceding table. Note that to enable PC2 to have the packet forwarding function
Check whether the value in/proc/sys/net/ipv4/ip_forward is 1
Command:
# More/proc/sys/net/ipv4/ip_forward
If the value is not 1 but 0, enter the following command:
# Echo '1'>/proc/sys/net/ipv4/ip_forward
Enable IP Forwarding
Other configurations in PC2:
To add a route for PC2, run the following command:
# Route add-net 192.168.1.0 netmask 255.255.255.0 dev eth0
# Route add-net 192.168.2.0 netmask 255.255.255.0 dev eth1
Iv. Verification:
PC1:
Ping Gateway
# Ping 192.168.2.1
# Ping 192.168.1.1
Ping PC3:
# Ping 192.168.1.2
PC3:
Ping Gateway
# Ping 192.168.1.1
# Ping 192.168.2.1
Pint PC1
# Ping 192.168.2.2
V. Bug debugging:
The cause of the error mainly lies in PC2.
1. Check whether PC2 allows IP packet forwarding (/proc/sys/net/ipv4/ip_forward value is 1)
2. Check whether route settings are correct (# Press enter to view route information)
The problem may occur: (the default route changes)
On the linux host, the original Nic eth0, IP address is 172.19.74.X/24, a new Nic eth1 is added, and the IP address 10.219.17.X/24 of the other network segment is configured, however, the host in the original 172.19.74.X segment cannot access this host.
Check the route table and find that the default route goes to the new Nic eth1:
Delete the default route and add a default route to the original eth0.
Command:
Route del default gw 10.219.17.1 dev eth1
Route add default gw 172.19.74.1 dev eth0
View the route table again:
[Root @ CallCenter-SIP ~] # Netstat-r
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
172.19.74.0*255.255.255.0 U 0 0 0 eth0
10.219.20.* 255.255.255.0 U 0 0 0 eth1
Link-local * 255.255.0.0 U 0 0 0 eth1
Default 172.19.74.1 0.0.0.0 UG 0 0 0 eth0
Then, all the devices in the network are restored to normal.
Another method can be implemented through iptables port forwarding.
Sharing statement: 192.168.1.0/24 Intranet 221.235.206.51 Internet
Iptables-APOSTROUTING-tnat-s192.168.1.0/24-oeth1-jSNAT -- to-source221.235.206.51
Echo1>/proc/sys/net/ipv4/ip_forward