概述
IP 别名(IP Alias),辅助ip地址(Secondary IP Address)
IP alias 是由 ifconfig 程序来创建和维护的,而 secondary IP address 则是由 ip 程序来创建和维护的。ip addr add 创建的 secondary IP address 不能在 ifconfig -a 中看到,反过来,ifconfig 创建的 ethX:Y 却能在 ip addr show 中看到。实际上 ifconfig 只是取到第一个匹配的 IP 地址,而 ip addr show 却是完全取。
multi-homing, IP aliasing, Primary address 与 Secondary address 概念辨析
host address: A unique address assigned to a communications device in a computer. If a computer has multiple communications devices (e.g., Ethernet cards or modems), each of these devices will have its own unique address. This means that a host (computer or router) can be multi-homed, i.e., have multiple IP addresses. This can also be artificially created by assigning different IP addresses to the same device (called IP aliasing).
Linux 中为同一个物理网卡增加多个 IP 地址,以前通过 ifconfig 命令来创建和维护 IP alias,而在新的 IPROUTE2 中通过 ip address 命令来创建和维护 Primary address 与 Secondary address。
* 在每一个接口上可以配置多个 Primary 地址和多个 Secondary 地址。
* 对一个特定的网络掩码(例子中的网络掩码为/24),只能有一个 Primary 地址。
在路由代码中对许多事件和条件作出响应依赖于 IP 地址为 Primary 地址还是 Secondary 地址。下面给一些例子:
* Primary addresses contribute to the entropy of the CPU that happens to run the code that applies the configuration.
* 当删除一个 Primary 地址时,所有相关的 Secondary 地址也被删除。但通过 /proc 可以配置一个选项(net.ipv4.conf.*.promote_secondaries),在当前 Primary 地址被删除时可以将 Secondary 地址提升为 Primary 地址。
* 当主机为本地生成的流量选择源 IP 地址时,只考虑 Primary 地址。
原理
在 Linux 中,一个网卡可以有多个 IP,那么这多个 IP 有什么关系呢?其实这些 IP 组成了一个吊链结构,所谓吊链结构就是一些节点链接 成一条链,然后每个节点带有自己的一条链,如下图所示: