Ubuntu双网卡通过配置静态路由同时接入内外网

Ubuntu双网卡网卡配置文件如下:

/etc/network/interfaces

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet dhcp

auto eth1
iface eth1 inet static
address 192.168.39.121
netmask 255.255.255.0

auto th0 说明eth0接口会在系统启动时被自动配置

第一张网卡DHCP自动获得外网IP

auto eth0
iface eth0 inet dhcp

第二张网卡静态IP连接单位内网

auto ethe1
iface eth1 inet static
address 192.168.39.121
netmask 255.255.255.0

双网卡IP配置完成后重启网络

/etc/init.d/networking restart

查看双网卡IP配置是否正确

root@Frpc-Server:/home/hxs# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0c:29:df:a6:79
inet addr:192.168.1.241 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fd98:55c0:7a4f:0:20c:29ff:fedf:a679/64 Scope:Global
inet6 addr: fda2:66a5:7416:0:20c:29ff:fedf:a679/64 Scope:Global
inet6 addr: fe80::20c:29ff:fedf:a679/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:10315 errors:0 dropped:0 overruns:0 frame:0
TX packets:523 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:752450 (752.4 KB) TX bytes:108400 (108.4 KB)

eth1 Link encap:Ethernet HWaddr 00:0c:29:df:a6:83
inet addr:192.168.39.121 Bcast:192.168.39.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fedf:a683/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:20 errors:0 dropped:0 overruns:0 frame:0
TX packets:20 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1484 (1.4 KB) TX bytes:1718 (1.7 KB)

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:16 errors:0 dropped:0 overruns:0 frame:0
TX packets:16 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1
RX bytes:1296 (1.2 KB) TX bytes:1296 (1.2 KB)

root@Frpc-Server:/home/hxs#

添加静态路由指定单位内网通过eth1连接

ip route add 192.168.39.0/24 via 192.168.39.1
ip route add 192.168.0.0/16 via 192.168.39.1
ip route add 172.19.0.0/16 via 192.168.39.1

或者如下命令

up route add -net 202.14.88.0 netmask 255.255.255.0 gw 221.5.43.1 eth0

删除静态路由的命令:

ip route del 192.168.39.0/24 via 192.168.39.1

删除路由

route del -net 172.16.100.0/24

ubunu可以在/etc/network/interfaces文件末尾末尾加入如下代码以添加永久静态路由

up route add -net 192.168.0.0 netmask 255.255.0.0 gw 192.168.60.1 eth1
up route add -net 172.19.0.0 netmask 255.255.0.0 gw 192.168.60.1 eth1
up route add -net 150.102.0.0 netmask 255.255.0.0 gw 192.168.60.1 eth1
up route add -net 10.130.0.0 netmask 255.255.0.0 gw 192.168.60.1 eth1
up route add -net 10.36.0.0 netmask 255.255.0.0 gw 192.168.60.1 eth1

route查看静态路由配置是否生效

root@Frpc-Server:/home/hxs# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default LEDE-X64.lan 0.0.0.0 UG 0 0 0 eth0
192.168.1.0 * 255.255.255.0 U 0 0 0 eth0
192.168.39.0 * 255.255.255.0 U 0 0 0 eth1
root@Frpc-Server:/home/hxs#

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注