IP别名技术

| Tags Linux  Testing 

如果要为eth0网卡设置两个ip (192.168.0.10 & 192.168.0.11), 如下表所示:

网卡 IP地址 类型
——– ————– ———
eth0 192.168.0.9 Primary
eth0:0 192.168.0.10 Alias 1
eth0:1 192.168.0.11 Alias 2

临时方案

$ sudo ifconfigeth0:0 192.168.0.10 up
$ sudo ifconfigeth0:1 192.168.0.11 up

注意:以上的修改在系统或网络重启后会失效!

永久方案

RedHad/Centos 系统

  • 复制网卡配置文件
$ cd /etc/sysconfig/network-scripts/
$ cp -v ifcfg-eth0 ifcfg-eth0:0
$ cp -v ifcfg-eth0 ifcfg-eth0:1
  • 修改 DEVICEIPADDR 参数
$ cat /etc/sysconfig/network-scripts/ifcfg-eth0:0
DEVICE="eth0:0"
BOOTPROTO=static
ONBOOT=yes
TYPE="Ethernet"
IPADDR=192.168.0.10
NETMASK=255.255.255.0
GATEWAY=192.168.0.1
HWADDR=00:0B:42:28:AB:4B

$ cat /etc/sysconfig/network-scripts/ifcfg-eth0:1
DEVICE="eth0:1"
BOOTPROTO=static
ONBOOT=yes
TYPE="Ethernet"
IPADDR=192.168.0.11
NETMASK=255.255.255.0
GATEWAY=192.168.0.1
HWADDR=00:0B:42:28:AB:4B

Debian/Ubuntu 系统

  • 打开 /etc/network/interfaces
$ cat /etc/network/interfaces
# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
allow-hotplug eth0
#iface eth0 inet dhcp
iface eth0 inet static
	address  192.168.0.9
	netmask	 255.255.255.0
	gateway  192.168.0.1
	dns-nameservers 8.8.8.8 8.8.4.4
  • 添加下面的内容设置虚拟网卡 (eth0:0 & eth0:1)
# Virtual Aliases
auto eth0:0
iface eth0:0 inet static
        address 192.168.0.10
        netmask 255.255.255.0

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

注意:修改完配置并保存后,需要重启网络服务使修改生效。

确认修改是否生效

ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0B:42:28:AB:4B
          inet addr:192.168.0.9  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::96de:80ff:fe63:ab6a/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:198507112 errors:0 dropped:0 overruns:0 frame:0
          TX packets:187207576 errors:0 dropped:0 overruns:0 carrier:1
          collisions:0 txqueuelen:1000
          RX bytes:96441149361 (89.8 GiB)  TX bytes:59909542838 (55.7 GiB)
          Interrupt:18

eth0:0    Link encap:Ethernet  HWaddr 00:0B:42:28:AB:4B
          inet addr:192.168.0.10  Bcast:192.168.0.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:18 Base address:0x2000

eth0:1    Link encap:Ethernet  HWaddr 00:0B:42:28:AB:4B
          inet addr:192.168.0.11  Bcast:192.168.0.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:18 Base address:0x2000

https://miteshshah.github.io/sysadmin/assign-multiple-ip-address/


Previous     Next