网卡多 IP 配置
概述
网卡多 IP 是指在同一个网络接口上配置和绑定多个 IP 地址。
引进网卡多 IP 的目的主要有以下几个:
- 提供服务高可用性。通过在同一接口绑定多个 IP 地址,然后在服务端使用这些 IP 地址启动多个服务实例。这样在任意一 IP 出现问题时,可以通过其他 IP 继续访问服务,提高服务的高可用性。
- 特殊用途 IP。我们可以在一个接口上配置公网 IP、内网 IP 和特殊用途 IP,用于不同用途。多 IP 可以满足一块接口绑定不同类型 IP 的需求。
所以,引进网卡多 IP,主要是为了更灵活和更加充分的利用网络接口提供的网络能力。
实现方式
在 SylixOS 系统中,使用 ifmip
命令来给网卡添加多个 IP 地址。在Shell 界面通过 help ifmip
可以查看到该命令的实际使用方式。
[root@sylixos:/root]# help ifmip
net interface add / delete secondary IPv4 address.
NOTICE: a fake interface 'mi*' will be create / delete.
ifmip [ifname] [{add | del}] [ipv4 address [netmask]]
网卡添加第二个 IP
在 en1 网卡上添加一个新的 IP 地址 172.168.0.126,子网掩码是 255.255.255.0 。
[root@sylixos:/root]# ifmip en1 add 172.168.0.126 255.255.255.0
使用
ifconfig
命令查看新添加的 IP, mi3 网卡的地址表示 en1 网卡新添加的 IP。[root@sylixos:/root]# ifconfig mi3 Link encap: Ethernet HWaddr: 00:11:22:33:44:98 Mif: en1 Ifidx: 4 DHCP: D4 D6 Spd: 1000 Mbps inet addr: 172.168.0.126 netmask: 255.255.255.0 gateway: 0.0.0.0 broadcast: 172.168.0.255 UP BROADCAST RUNNING MTU:1500 Metric:1 en2 Link encap: Ethernet HWaddr: be:7a:3c:c9:9a:e5 Dev: dw_1 Ifidx: 3 DHCP: D4 D6 Spd: N/A inet addr: 10.4.130.210 netmask: 255.255.255.0 gateway: 10.4.0.1 broadcast: 10.4.130.255 inet6 addr: fe80::bc7a:3cff:fec9:9ae5 Scope:Link<T0> UP BROADCAST MULTICAST MTU:1500 Metric:1 collisions:0 txqueue:0 tcpaf:2 tcpwnd:65535 RX ucast packets:0 nucast packets:0 dropped:0 TX ucast packets:0 nucast packets:0 dropped:0 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B) en1 Link encap: Ethernet HWaddr: 00:11:22:33:44:98 Dev: dw_0 Ifidx: 2 DHCP: D4 D6 Spd: 1000 Mbps inet addr: 10.4.120.211 netmask: 255.255.0.0 gateway: 10.4.0.1 broadcast: 10.4.255.255 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 collisions:0 txqueue:512 tcpaf:2 tcpwnd:65535 RX ucast packets:4 nucast packets:47860 dropped:0 TX ucast packets:0 nucast packets:6 dropped:0 RX bytes:2904343 (2.7 MB) TX bytes:396 (396.0 B) lo0 Link encap: Local Loopback Dev: N/A Ifidx: 1 DHCP: D4 D6 Spd: N/A inet addr: 127.0.0.1 netmask: 255.0.0.0 P-to-P: 127.0.0.1 broadcast: N/A inet6 addr: ::1 Scope:Loopback UP LOOPBACK RUNNING MTU:0 Metric:1 collisions:0 txqueue:0 tcpaf:2 tcpwnd:262140 RX ucast packets:3 nucast packets:0 dropped:0 TX ucast packets:3 nucast packets:0 dropped:0 RX bytes:168 (168.0 B) TX bytes:168 (168.0 B) dns0: 0.0.0.0 dns1: 0.0.0.0 dns2: 0.0.0.0 dns3: 0.0.0.0 default device is: en1 list net interface: 4
网卡删除第二个 IP
在 en1 网卡上删除上文添加的 IP 地址 172.168.0.126。
[root@sylixos:/root]# ifmip en1 del 172.168.0.126
使用
ifconfig
命令查看上文新添加的 IP 已经被成功删除。en2 Link encap: Ethernet HWaddr: be:7a:3c:c9:9a:e5 Dev: dw_1 Ifidx: 3 DHCP: D4 D6 Spd: N/A inet addr: 10.4.130.210 netmask: 255.255.255.0 gateway: 10.4.0.1 broadcast: 10.4.130.255 inet6 addr: fe80::bc7a:3cff:fec9:9ae5 Scope:Link<T0> UP BROADCAST MULTICAST MTU:1500 Metric:1 collisions:0 txqueue:0 tcpaf:2 tcpwnd:65535 RX ucast packets:0 nucast packets:0 dropped:0 TX ucast packets:0 nucast packets:0 dropped:0 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B) en1 Link encap: Ethernet HWaddr: 00:11:22:33:44:98 Dev: dw_0 Ifidx: 2 DHCP: D4 D6 Spd: 1000 Mbps inet addr: 10.4.120.211 netmask: 255.255.0.0 gateway: 10.4.0.1 broadcast: 10.4.255.255 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 collisions:0 txqueue:512 tcpaf:2 tcpwnd:65535 RX ucast packets:47 nucast packets:432195 dropped:0 TX ucast packets:6 nucast packets:6 dropped:0 RX bytes:26538743 (25.3 MB) TX bytes:720 (720.0 B) lo0 Link encap: Local Loopback Dev: N/A Ifidx: 1 DHCP: D4 D6 Spd: N/A inet addr: 127.0.0.1 netmask: 255.0.0.0 P-to-P: 127.0.0.1 broadcast: N/A inet6 addr: ::1 Scope:Loopback UP LOOPBACK RUNNING MTU:0 Metric:1 collisions:0 txqueue:0 tcpaf:2 tcpwnd:262140 RX ucast packets:3 nucast packets:0 dropped:0 TX ucast packets:3 nucast packets:0 dropped:0 RX bytes:168 (168.0 B) TX bytes:168 (168.0 B) dns0: 0.0.0.0 dns1: 0.0.0.0 dns2: 0.0.0.0 dns3: 0.0.0.0 default device is: en1 list net interface: 3
持久设置辅助 ip
使用 ifmip
命令只能临时修改辅助 ip,想要持久修改可以通过 ifparam.ini
文件来实现。
在 ifparam.ini
文件中增加 mipaddr、mgateway、mnetmask 字段配置即可增加一组扩展 ip 地址,一个网卡可以扩展多个 IP 地址。
[root@sylixos:/root]# cat /etc/ifparam.ini
[e1000_0]
enable=1
ipaddr=192.168.196.88
gateway=192.168.196.1
netmask=255.255.255.0
#dhcp=1
#dhcp6
mipaddr=192.168.1.88
mgateway=192.168.1.1
mnetmask=255.255.255.0
default=1
[root@sylixos:/root]# ifconfig
mi2 Link encap: Ethernet HWaddr: ce:63:a8:1e:22:5c
Mif: en1 Ifidx: 3 DHCP: D4 D6 Spd: 1000 Mbps
inet addr: 192.168.1.88 netmask: 255.255.255.0
gateway: 192.168.1.1 broadcast: 192.168.1.255
UP BROADCAST RUNNING MTU:1500 Metric:1
en1 Link encap: Ethernet HWaddr: ce:63:a8:1e:22:5c
Dev: e1000_0 Ifidx: 2 DHCP: D4 D6 Spd: 1000 Mbps
inet addr: 192.168.196.88 netmask: 255.255.255.0
gateway: 192.168.196.1 broadcast: 192.168.196.255
inet6 addr: fe80::cc63:a8ff:fe1e:225c Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
collisions:0 txqueue:0 tcpaf:2 tcpwnd:65535
RX ucast packets:3 nucast packets:842 dropped:0
TX ucast packets:1 nucast packets:7 dropped:0
RX bytes:65476 (63.9 KB) TX bytes:620 (620.0 B)
lo0 Link encap: Local Loopback
Dev: N/A Ifidx: 1 DHCP: D4 D6 Spd: N/A
inet addr: 127.0.0.1 netmask: 255.0.0.0
P-to-P: 127.0.0.1 broadcast: N/A
inet6 addr: ::1 Scope:Loopback
UP LOOPBACK RUNNING MTU:0 Metric:1
collisions:0 txqueue:0 tcpaf:2 tcpwnd:262140
RX ucast packets:3 nucast packets:0 dropped:0
TX ucast packets:3 nucast packets:0 dropped:0
RX bytes:168 (168.0 B) TX bytes:168 (168.0 B)
dns0: 114.114.114.114
dns1: 0.0.0.0
dns2: 0.0.0.0
dns3: 0.0.0.0
default device is: en1
list net interface: 3
[root@sylixos:/root]#
[root@sylixos:/root]# ping 192.168.1.199
Pinging 192.168.1.199
Reply from 192.168.1.199: bytes=32 time=0.955ms TTL=64
Reply from 192.168.1.199: bytes=32 time=0.550ms TTL=64
Reply from 192.168.1.199: bytes=32 time=0.690ms TTL=64
Reply from 192.168.1.199: bytes=32 time=0.567ms TTL=64
Ping statistics for 192.168.1.199:
Packets: Send = 4, Received = 4, Lost = 0(0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0.550ms, Maximum = 0.955ms, Average = 0.690ms
[root@sylixos:/root]# ping 192.168.196.199
Pinging 192.168.196.199
Reply from 192.168.196.199: bytes=32 time=0.259ms TTL=64
Reply from 192.168.196.199: bytes=32 time=0.608ms TTL=64
Reply from 192.168.196.199: bytes=32 time=0.949ms TTL=64
Reply from 192.168.196.199: bytes=32 time=0.660ms TTL=64
Ping statistics for 192.168.196.199:
Packets: Send = 4, Received = 4, Lost = 0(0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0.259ms, Maximum = 0.949ms, Average = 0.619ms
[root@sylixos:/root]#
实际场景
当一个设备只有一个网络接口,要与多台不同网段的主机通信,为了避免频繁地修改设备地址,可以将 SylixOS 设备的网卡改成多 IP 模式,这样只需要每次插拔网线,而不再用频繁往复地修改设备 IP 地址。
进入 SylixOS 设备的Shell界面,通过
ifconfig
命令查看网络接口信息。[root@sylixos:/root]# ifconfig en1 Link encap: Ethernet HWaddr: 00:0a:35:00:01:aa Dev: zynq0 Ifidx: 2 DHCP: D4 D6 Spd: 1000 Mbps inet addr: 10.4.120.11 netmask: 255.255.0.0 gateway: 10.4.0.1 broadcast: 10.4.255.255 inet6 addr: fe80::20a:35ff:fe00:1aa Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 collisions:0 txqueue:0 tcpaf:2 tcpwnd:131070 RX ucast packets:0 nucast packets:4690 dropped:0 TX ucast packets:0 nucast packets:9 dropped:0 RX bytes:283649 (277.0 KB) TX bytes:622 (622.0 B) lo0 Link encap: Local Loopback Dev: N/A Ifidx: 1 DHCP: D4 D6 Spd: N/A inet addr: 127.0.0.1 netmask: 255.0.0.0 P-to-P: 127.0.0.1 broadcast: N/A inet6 addr: ::1 Scope:Loopback UP LOOPBACK RUNNING MTU:0 Metric:1 collisions:0 txqueue:0 tcpaf:2 tcpwnd:262140 RX ucast packets:3 nucast packets:0 dropped:0 TX ucast packets:3 nucast packets:0 dropped:0 RX bytes:168 (168.0 B) TX bytes:168 (168.0 B) dns0: 0.0.0.0 dns1: 0.0.0.0 dns2: 0.0.0.0 dns3: 0.0.0.0 default device is: en1 list net interface: 2
在网卡 en1 上添加一个 IP 地址 192.168.11.5,子网掩码是 255.255.255.0。
[root@sylixos:/root]# ifmip en1 add 192.168.11.5 255.255.255.0
通过
ifconfig
命令查看网络接口信息。[root@sylixos:/root]# ifconfig mi2 Link encap: Ethernet HWaddr: 00:0a:35:00:01:aa Mif: en1 Ifidx: 3 DHCP: D4 D6 Spd: 1000 Mbps inet addr: 192.168.11.5 netmask: 255.255.255.0 gateway: 0.0.0.0 broadcast: 192.168.11.255 UP BROADCAST RUNNING MTU:1500 Metric:1 en1 Link encap: Ethernet HWaddr: 00:0a:35:00:01:aa Dev: zynq0 Ifidx: 2 DHCP: D4 D6 Spd: 1000 Mbps inet addr: 10.4.120.11 netmask: 255.255.0.0 gateway: 10.4.0.1 broadcast: 10.4.255.255 inet6 addr: fe80::20a:35ff:fe00:1aa Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 collisions:0 txqueue:0 tcpaf:2 tcpwnd:131070 RX ucast packets:0 nucast packets:6429 dropped:0 TX ucast packets:0 nucast packets:10 dropped:0 RX bytes:389041 (379.9 KB) TX bytes:664 (664.0 B) lo0 Link encap: Local Loopback Dev: N/A Ifidx: 1 DHCP: D4 D6 Spd: N/A inet addr: 127.0.0.1 netmask: 255.0.0.0 P-to-P: 127.0.0.1 broadcast: N/A inet6 addr: ::1 Scope:Loopback UP LOOPBACK RUNNING MTU:0 Metric:1 collisions:0 txqueue:0 tcpaf:2 tcpwnd:262140 RX ucast packets:3 nucast packets:0 dropped:0 TX ucast packets:3 nucast packets:0 dropped:0 RX bytes:168 (168.0 B) TX bytes:168 (168.0 B) dns0: 0.0.0.0 dns1: 0.0.0.0 dns2: 0.0.0.0 dns3: 0.0.0.0 default device is: en1 list net interface: 3 [root@sylixos:/root]#
通过网线将 SylixOS 设备和 主机 A 相连,在 SylixOS 设备的Shell界面向主机 A 发送 ping 包,通信正常,证明新加的 IP 地址功能正常。
[root@sylixos:/root]# ping 192.168.11.2 Pinging 192.168.11.2 Reply from 192.168.11.2: bytes=32 time=0.213ms TTL=255 Reply from 192.168.11.2: bytes=32 time=0.203ms TTL=255 Reply from 192.168.11.2: bytes=32 time=0.202ms TTL=255 Reply from 192.168.11.2: bytes=32 time=0.204ms TTL=255 Ping statistics for 192.168.11.2: Packets: Send = 4, Received = 4, Lost = 0(0% loss), Approximate round trip times in milli-seconds: Minimum = 0.202ms, Maximum = 0.213ms, Average = 0.205ms [root@sylixos:/root]#
通过网线将 SylixOS 设备和 主机 B 相连,在 SylixOS 设备的Shell界面向主机 B 发送 ping 包,通信正常,证明原有的 IP 地址功能正常。
[root@sylixos:/root]# ping 10.4.0.105 Pinging 10.4.0.105 Reply from 10.4.0.105: bytes=32 time=0.689ms TTL=64 Reply from 10.4.0.105: bytes=32 time=0.677ms TTL=64 Reply from 10.4.0.105: bytes=32 time=0.686ms TTL=64 Reply from 10.4.0.105: bytes=32 time=0.644ms TTL=64 Ping statistics for 10.4.0.105: Packets: Send = 4, Received = 4, Lost = 0(0% loss), Approximate round trip times in milli-seconds: Minimum = 0.644ms, Maximum = 0.689ms, Average = 0.674ms [root@sylixos:/root]#