linux单网卡设置双IP

前言

最近在折腾一个网关,系统是国产龙芯系统,但是也是基于linux系统的,最终使用时需要设置双IP,说实话还真是头一次听说单网卡能设置双IP,是我孤陋寡闻了,下面具体记录一下吧

过程

一开始用的ifconfig命令配置的多IP,命令如下

1
ifconfig eth0:0 192.168.1.1 netmask 255.255.255.0 # eth:0 可以单网卡配置多IP

但是这个命令在系统重启后就会失效,所以这个命令临时测试用一下可以,上生产不太行,所以继续研究下改配置文件

配置文件在这vim /etc/network/interfaces,具体命令如下

1
2
3
4
5
6
7
8
9
auto eth0:1
iface eth0:1 inet static
address 192.168.7.20
netmask 255.255.255.0

auto eth0:2
iface eth0:2 inet static
address 10.10.15.1
netmask 255.255.255.0

更多的IP的话就是类似的在后面加,完整的配置文件如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
source /etc/network/interfaces.d/*
# Network is managed by Network manager
auto eth0

#iface eth0 inet dhcp

iface eth0 inet static
address 192.168.6.22
netmask 255.255.255.0

auto eth0:1
iface eth0:1 inet static
address 192.168.7.20
netmask 255.255.255.0

auto eth0:2
iface eth0:2 inet static
address 10.10.15.1
netmask 255.255.255.0


auto lo
iface lo inet loopback

配置完成后,重启reboot使其生效