操作系统:Linux CentOS7

网络环境:Linux虚拟机,一块ens32内网网卡,需要配置一块可以连公网的ens33网卡。
ens32:192.168.107.150 netmask:255.255.255.0 gw:192.168.107.1

通过vm新增一块网卡,并启动激活该网卡:
ens33:192.168.81.145  netmask:255.255.255.0 gw:192.168.81.1

方法:
1、查看服务器接入网卡数和接入的网卡设备信息
    1)服务器上网卡数
    [root@test ~]#  lspci | grep -i Ethernet
    02:00.0 Ethernet controller: Intel Corporation 82545EM Gigabit Ethernet Controller (Copper) (rev 01)
    02:01.0 Ethernet controller: Intel Corporation 82545EM Gigabit Ethernet Controller (Copper) (rev 01)
    2)设备对应信息
    [root@test ~]# nmcli con show
    NAME    UUID                                  TYPE      DEVICE 
    ens32   1b521a6d-933c-4fa3-8b67-cd50560bbcd7  ethernet  ens32  
    ens33   7fa2f659-8709-4ba9-bd19-a2eb9f05b6d8  ethernet  ens33  
    virbr0  dbe9c632-d1b7-42d2-81f8-dec29131d675  bridge    virbr0 
2、ip addr查看网卡信息
[root@localhost ~]# ip addr
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: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:bc:30:b0 brd ff:ff:ff:ff:ff:ff
    inet 192.168.107.150/24 brd 192.168.107.255 scope global noprefixroute ens32
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:febc:30b0/64 scope link 
       valid_lft forever preferred_lft forever
3: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:bc:30:ba brd ff:ff:ff:ff:ff:ff
    inet 192.168.81.227/24 brd 192.168.81.255 scope global noprefixroute dynamic ens33
       valid_lft 6920sec preferred_lft 6920sec
    inet6 fe80::9131:7a96:77fd:665/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

3、[root@localhost ~]# vi /etc/iproute2/rt_tables
在最后添加两行
107 net_107
81 net_81

4、执行如下语句
ip route flush table net_107
ip route add default via 192.168.107.1 dev ens32 src 192.168.107.150 table net_107
ip rule add from 192.168.107.150 table net_107
ip route flush table net_81
ip route add default via 192.168.81.1 dev ens33 src 192.168.81.227 table net_81
ip rule add from 192.168.81.227 table net_81
route add default gw 192.168.81.1

操作系统我是最小化安装,没有route命令,最后一个命令执行不了,暂时不管它。
5、上述语句添加至/etc/rc.d/init.d/network中esac和exit $rc之间,保证机器重启开机自动运行。
[root@localhost ~]# vi /etc/rc.d/init.d/network 
.....
esac
ip route flush table net_107
ip route add default via 192.168.107.1 dev ens32 src 192.168.107.150 table net_107
ip rule add from 192.168.107.150 table net_107
ip route flush table net_81
ip route add default via 192.168.81.1 dev ens33 src 192.168.81.227 table net_81
ip rule add from 192.168.81.227 table net_81
route add default gw 192.168.81.1
exit $rc

5、编辑ens32内网网卡,屏蔽网关
[root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens32
#GATEWAY=192.168.107.

6、ip route查看路由信息,并删除内网路由
[root@localhost ~]# ip route
default via 192.168.107.1 dev ens32 proto static metric 100 
default via 192.168.81.1 dev ens33 proto dhcp metric 101 
192.168.81.0/24 dev ens33 proto kernel scope link src 192.168.81.227 metric 101 
192.168.107.0/24 dev ens32 proto kernel scope link src 192.168.107.150 metric 100

[root@localhost ~]# ip route del default via 192.168.107.1 dev ens32 proto static metric 100

8、测试连公网成功
[root@localhost ~]# ping www.baidu.com
PING www.a.shifen.com (182.61.200.7) 56(84) bytes of data.
64 bytes from 182.61.200.7 (182.61.200.7): icmp_seq=1 ttl=52 time=3.11 ms
64 bytes from 182.61.200.7 (182.61.200.7): icmp_seq=2 ttl=52 time=4.11 ms

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐