Configure Linux Routing for Dual NICs: Temporary Commands & Permanent Solutions
This guide explains how to add and delete host and network routes, set default gateways, enable IP forwarding, configure permanent routes via rc.local, sysconfig files, and static‑routes, and demonstrates a dual‑NIC experiment using VMware and eNSP to achieve simultaneous internal and external network access.
Linux Routing Commands
1. Add host route
route add -host 192.168.1.11 dev eth0
route add -host 192.168.1.12 gw 192.168.1.12. Add network route
route add -net 192.168.1.11 netmask 255.255.255.0 eth0
route add -net 192.168.1.11 netmask 255.255.255.0 gw 192.168.1.1
route add -net 192.168.1.0/24 eth0
route add -net 192.168.1.0/24 gw 192.168.1.13. Add default gateway
route add default gw 192.168.1.14. Delete route
route del -host 192.168.1.11 dev eth05. Delete default route
route del default gw 192.168.1.1Ways to Configure Permanent Routes in Linux
1. Add to /etc/rc.local
route add -net 192.168.1.0/24 dev eth0
# or
route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.12. Append to /etc/sysconfig/network
GATEWAY=[gateway IP or interface name]3. Use /etc/sysconfig/static-routes
any net 192.168.1.0/24 gw 192.168.1.1
# or
any net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.14. Enable IP forwarding
1. Temporary echo "1" > /proc/sys/net/ipv4/ip_forward 2. Permanent
vim /etc/sysctl.conf
# modify or add:
net.ipv4.ip_forward=1Experiment: Configure a Dual‑NIC Host to Use Both Internal and External Networks
The experiment reproduces a real‑world scenario where a host with two network cards must access both an external ISP network (10.0.0.0/16) and an isolated internal network (172.16.0.0/16). VMware Workstation and Huawei eNSP are used to simulate the environment.
Problem Background
When the client node is the only host connecting the two networks, the internal and external networks are isolated. Placing the default gateway on the external NIC allows access to the Internet but blocks other internal subnets; placing it on the internal NIC enables full internal access but prevents Internet connectivity. The goal is to enable simultaneous access.
Experiment Environment
VMware Workstation Pro
Four minimal CentOS 7.3 virtual machines
Huawei eNSP simulator
Experiment Topology
Key nodes:
client – dual‑NIC host with IPs 10.0.0.101/16 (external) and 172.16.2.101/24 (internal)
server2 – 172.16.2.11/24
server3 – 172.16.3.11/24
server4 – 172.16.4.11/24
Network Planning
IP Allocation
client eth0 10.0.0.101 255.255.0.0 gateway 10.0.0.1 # external
client eth1 172.16.2.101 255.255.255.0 # internal
server2 eth0 172.16.2.11 255.255.255.0 gw 172.16.2.254
server3 eth0 172.16.3.11 255.255.255.0 gw 172.16.3.254
server4 eth0 172.16.4.11 255.255.255.0 gw 172.16.4.254VMware NIC Types
Operator network – Bridged – 10.0.0.0/16
VMnet2 – Host‑only – 172.16.2.0/24
VMnet3 – Host‑only – 172.16.3.0/24
VMnet4 – Host‑only – 172.16.4.0/24Configure Internal Network (eNSP Router)
Set interfaces on the Huawei router:
<huawei>system-view
[int g0/0/0]
ip address 172.16.2.254 255.255.255.0
[int g0/0/1]
ip address 172.16.3.254 255.255.255.0
[int g0/0/2]
ip address 172.16.4.254 255.255.255.0Enable IP Forwarding on the Client
Temporary: echo "1" > /proc/sys/net/ipv4/ip_forward Permanent (edit /etc/sysctl.conf):
net.ipv4.ip_forward=1Testing Connectivity
Ping external site: ping www.baidu.com -c 4 Ping each internal server:
ping 172.16.2.11 -c 4
ping 172.16.3.11 -c 4
ping 172.16.4.11 -c 4Access HTTP services hosted on the internal servers (Python SimpleHTTPServer on port 8080):
curl http://172.16.2.11:8080/index.html
curl http://172.16.3.11:8080/index.html
curl http://172.16.4.11:8080/index.htmlAdding a Persistent Route for the Whole Internal Network
On the client, add a static route covering the remaining internal subnets: route add -net 172.16.0.0/16 gw 172.16.2.254 To make it permanent, place the same command in /etc/rc.local.
Result
After adding the route, the client can reach the Internet and all internal subnets (172.16.2.0/24, 172.16.3.0/24, 172.16.4.0/24) simultaneously. The experiment demonstrates that a single static route can solve the dual‑NIC routing problem, while also illustrating the underlying routing principles.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
