系统相关
首页 > 系统相关> > 11,如何利用Linux双网卡连接两个网络

11,如何利用Linux双网卡连接两个网络

作者:互联网

我们有时候会遇到两个网络的情况,一个是内部私有的办公网络,一个是对外的网络,为了安全两个网络不能互相通讯,而在两个网络间要架一台双网卡的linux服务器,
通过在内网PC上访问服务器,而服务器将具体数据与公网进行交换,达到内部用户不用切换网络,就可以方便、安全的访问服务器,具体的网络拓扑如下图 具体的网卡配置如下 一号网卡,此网卡接外网 [root@MasServer network-scripts]# cat ifcfg-eth0 DEVICE=eth0 BOOTPROTO=static IPADDR=192.168.0.2 NETMASK=255.255.255.0 GATEWAY=192.168.0.1 ONBOOT=yes TYPE=Ethernet 二号网卡,此网卡接内网 [root@MasServer network-scripts]# cat ifcfg-eth1 DEVICE=eth1 BOOTPROTO=static IPADDR=192.168.254.2 NETMASK=255.255.255.0 ONBOOT=yes TYPE=Ethernet 如此配置后,linux系统的路由表如下 [root@MasServer ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 192.168.254.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1 169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth1 0.0.0.0 192.168.0.1 0.0.0.0 UG 0 0 0 eth0 但是如此配置并不能达到我们想要的效果,我们可以通过linux系统ping一下内网网关测试 [root@MasServer root]# ping 192.168.254.1 PING 202.112.14.152 (192.168.251.1) 56(84) bytes of data. --- 192.168.251.1 ping statistics --- 9 packets transmitted, 0 received, 100% packet loss, time 8016ms 这是因为存在一条默认路由(红色标记),linux会把所有从私网上传输过来的数据,通过eth0传输出去,而不会通过eth1返回到私网上。为此 我们要添加一条静态路由,告诉系统,将私网上的数据强行通过eth1口返回。我们可以用以下命令 [root@MasServer ~]# route add -host 192.168.254.1 dev eth1 添加私网上的网关地址,强行制定内网的数据从eth1口返回。添加以后,查看Linux系统的路由表 [root@MasServer ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.254.1 0.0.0.0 255.255.255.255 UH 0 0 0 eth1 192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 192.168.254.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1 169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth1 0.0.0.0 192.168.0.1 0.0.0.0 UG 0 0 0 eth0 如此就可以ping通内网网关192.168.254.1和192.168.254.3主机了 这样添加的路由信息,在系统重启以后将会失效,为此我们需要将此命令添加到/etc/rc.d/rc.local启动项里.如下 [root@MasServer rc.d]# cat rc.local #!/bin/sh # # This script will be executed *after* all the other init scripts. # You can put your own initialization stuff in here if you don't # want to do the full Sys V style init stuff. touch /var/lock/subsys/local export LC_ALL="zh_CN.GB18030" export JAVA_OPTS=-Xmx256M export JAVA_HOME=/usr/local/jdk export PATH=$PATH:$JAVA_HOME/bin /usr/local/tomcat/bin/startup.sh route add -host 192.168.254.1 dev eth1 注意事项 1、网卡2的配置理由不可以再加GATEWAY网关项,否则有不可预知的问题。可能外网通内网不通,可能内网通外网不通,也可能内外网都不通。因为配置两个网关,系统就不知道数据到底应该是从eth0口出去,还是从eth1口出去。 2、本来应该是用添加网段的命令 route add -net 192.168.254.0/24 gw 192.168.254.1 dev eth1 但是经过实验,此命令无法达到效果,具体原因不清楚,所以只好用添加主机的命令直接添加网关地址,如果划分了多个vlan,就需要添加多个网关 3、添加好路由后,ping内网地址,需要用service iptables stop命令将防火墙关闭,否则无法ping通。如果是内网访问服务器,则不需要关闭防火墙 4、添加到rc.local启动项时,有可能因为未指定route命令路径,导致添加不成功,为此可以在route前指定route命令路径 /sbin/route add -host 192.168.254.1 dev eth1

 

标签:11,0.0,route,192.168,255.255,添加,Linux,双网卡,eth1
来源: https://www.cnblogs.com/k8s-pod/p/13757329.html