centos7 双网卡同网段双网关配置

需求:

#1、服务器为双网卡;
#2、网卡1为互联网 
172.16.137.99/24/254
#3、网卡2为旅游专网
172.16.137.97/24/1
#4、互联网路由器为172.16.137.254
#5、旅游网需访问网关 172.16.134.1、135.1、136.1 。。。
#6、实现服务器被互联网访问,也能被旅游网访问;

1、配置网卡

网卡1

[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-eno1
PROXY_METHOD="none"
BOOTPROTO="static"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
NAME="eno1"
DEVICE="eno1"
ONBOOT="yes"
IPADDR="172.16.137.99"
PREFIX="24"
GATEWAY="172.16.137.254"
DNS1="60.255.80.18"
DNS2="60.255.80.19"
[root@localhost ~]#

  

网卡2(不要设置GATEWAY,不要设置GATEWAY,不要设置GATEWAY,重要的事情说三遍!)

[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-eno2
PROXY_METHOD="none"
BOOTPROTO="static"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
NAME="eno2"
DEVICE="eno2"
ONBOOT="yes"
IPADDR="172.16.137.97"
PREFIX="24"
GATEWAY="172.16.137.1"
DNS1="60.255.80.18"
DNS2="60.255.80.19"
[root@localhost ~]#

2、查看ip地址

[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether ec:aa:a0:19:85:98 brd ff:ff:ff:ff:ff:ff
    inet 172.16.137.99/24 brd 172.16.137.255 scope global noprefixroute eno1
       valid_lft forever preferred_lft forever
    inet6 fe80::eeaa:a0ff:fe19:8598/64 scope link
       valid_lft forever preferred_lft forever
3: eno2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether ec:aa:a0:19:85:99 brd ff:ff:ff:ff:ff:ff
    inet 172.16.137.97/24 brd 172.16.137.255 scope global noprefixroute eno2
       valid_lft forever preferred_lft forever
    inet6 fe80::eeaa:a0ff:fe19:8599/64 scope link
       valid_lft forever preferred_lft forever
4: enp0s26u1u2u5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether ee:aa:a0:19:85:9f brd ff:ff:ff:ff:ff:ff
[root@localhost ~]#

ip配置生效,测试网关只有网卡1通,网卡2不通。

3、添加路由

查看路由

[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.16.137.254  0.0.0.0         UG    100    0        0 eno1
0.0.0.0         172.16.137.1    0.0.0.0         UG    101    0        0 eno2172.16.137.0    0.0.0.0         255.255.255.0   U     100    0        0 eno1
172.16.137.0    0.0.0.0         255.255.255.0   U     101    0        0 eno2
[root@localhost ~]#

添加临时路由,既然需要通不同旅游专网网段,我添加目标路由172.16.0.0 16位子网掩码到172.16.137.1出

route add -net 172.16.0.0 netmask 255.255.0.0 gw 172.16.137.1

再查看路由

[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.16.137.254  0.0.0.0         UG    100    0        0 eno1
0.0.0.0         172.16.137.1    0.0.0.0         UG    101    0        0 eno2
172.16.0.0      172.16.137.1    255.255.0.0     UG    101    0        0 eno2
172.16.137.0    0.0.0.0         255.255.255.0   U     100    0        0 eno1
172.16.137.0    0.0.0.0         255.255.255.0   U     101    0        0 eno2
[root@localhost ~]#

其他旅游专线专线,能ping通,也能被其他旅游网专线ping通。至此路由配置已通。

4、添加永久路由

#1、添加route文件,文件名route + 网卡名
[root@localhost ~]# vim /etc/sysconfig/network-scripts/route-eno2
#2、添加路由
172.16.0.0/16 via 172.16.137.1 dev eno2
#3、重启网络
systemctl restart network

 

posted @ 2022-03-29 10:27  QiuPing  阅读(2943)  评论(0编辑  收藏  举报