Linux创建虚拟网卡(单网卡双地址)

方法1:添加虚拟网卡,并配置地址
[root@vm7 ~]# ifconfig eth0:1 10.10.152.7 netmask 255.255.255.0 up    #该配置会在重启后消失

方法2:复制一份网卡文件,修改红色字体部分
cp ifcfg-eth1 ifcfg-eth1:1
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=no
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=eth0:1  
UUID=575fa71d-1ba7-468e-bd3c-2a6a5f379a78
DEVICE=eth0:1
ONBOOT=yes
IPADDR=10.10.152.7
PREFIX=24
GATEWAY=10.10.152.1
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_PRIVACY=no
[root@vm7 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.100.252  netmask 255.255.255.0  broadcast 172.16.100.255
        inet6 fe80::20c:29ff:feb6:6989  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:b6:69:89  txqueuelen 1000  (Ethernet)
        RX packets 13555483  bytes 1061275030 (1012.1 MiB)
        RX errors 0  dropped 105262  overruns 0  frame 0
        TX packets 50472  bytes 11453893 (10.9 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.10.152.7  netmask 255.255.255.0  broadcast 10.10.152.255
        ether 00:0c:29:b6:69:89  txqueuelen 1000  (Ethernet)

 

举例:虚拟网卡配置文件只保留这几项即可

TYPE=Ethernet
BOOTPROTO=static
NAME=eth0:1
DEVICE=eth0:1
ONBOOT=yes

IPADDR=10.10.4.98
NETMASK=255.255.255.0
GATEWAY=10.10.4.1
DNS1=221.11.1.67
DNS2=114.114.114.114

 

添加完虚拟网卡,可能需要指定静态路由

[root@vm7 ~]# route add -net 22.22.0.0/16 gw 10.10.152.1
[root@vm7 ~]# route add -net 10.10.4.6 netmask 255.255.255.255 gw 10.10.4.9   #或者这样
[root@vm7 ~]# route add -net 192.168.4.0 netmask 255.255.255.0 gw 10.10.4.99 dev eth0        #或者这样指定网卡
[root@vm7 ~]# route -n  #查看已配置的静态路由
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.10.4.1 0.0.0.0 UG 100 0 0 eth0
10.10.4.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0
10.10.4.6 10.10.4.9 255.255.255.255 UGH 0 0 0 eth0
10.10.4.6 10.10.4.8 255.255.255.255 UGH 0 0 0 eth0

 

同样的上面添加的静态路由会在重启后消失,若要添加永久路由,可将命令写入启动项

[root@vm7 ~]# vi /etc/rc.local 

 

或者创建static-routes 这个文件默认是不存在的,需要创建出来

[root@vm7 ~]# vi /etc/sysconfig/static-routes

#添加如下内容
any net 10.10.4.0/24 gw 192.168.1.1
any net 10.10.4.6 netmask 255.255.255.255 gw 192.168.1.1   #第二种写法

 

posted @ 2021-01-21 10:18  局域网外  阅读(2331)  评论(0编辑  收藏  举报