加载IP别名模块 (如果已将该模块编译到内核中,则可以跳过此步骤)
/sbin/insmod /lib/modules/`uname -r`/ipv4/ip_alias.o |
设置环回、eth0 和所有以 eth0 接口的主 IP 地址开头的 IP 地址
/sbin/ifconfig lo 127.0.0.1 /sbin/ifconfig eth0 up /sbin/ifconfig eth0 172.16.3.1 /sbin/ifconfig eth0:0 172.16.3.10 /sbin/ifconfig eth0:1 172.16.3.100 |
172.16.3.1 是主 IP 地址,而 .10 和 .100 是别名。关键在于 eth0:x,其中 x=0,1,2,...n 代表不同的 IP 地址。主 IP 地址不需要设置别名。
设置路由。首先路由环回,然后是网络,最后是以默认(最初分配的)IP 地址开始的各个 IP 地址
/sbin/route add -net 127.0.0.0 /sbin/route add -net 172.16.3.0 dev eth0 /sbin/route add -host 172.16.3.1 dev eth0 /sbin/route add -host 172.16.3.10 dev eth0:0 /sbin/route add -host 172.16.3.100 dev eth0:1 /sbin/route add default gw 172.16.3.200 |
就这样。
在上面的 IP 地址示例中,我使用了私有 IP 地址 (RFC 1918) 来进行说明。请用您自己的官方或私有 IP 地址替换它们。
该示例仅显示 3 个 IP 地址。 最大值定义为 256,位于/usr/include/linux/net_alias.h。在一张网卡上设置 256 个 IP 地址已经很多了 :-)!
这是我的/sbin/ifconfig看起来像这样
lo Link encap:Local Loopback inet addr:127.0.0.1 Bcast:127.255.255.255 Mask:255.0.0.0 UP BROADCAST LOOPBACK RUNNING MTU:3584 Metric:1 RX packets:5088 errors:0 dropped:0 overruns:0 TX packets:5088 errors:0 dropped:0 overruns:0 eth0 Link encap:10Mbps Ethernet HWaddr 00:8E:B8:83:19:20 inet addr:172.16.3.1 Bcast:172.16.3.255 Mask:255.255.255.0 UP BROADCAST RUNNING PROMISC MULTICAST MTU:1500 Metric:1 RX packets:334036 errors:0 dropped:0 overruns:0 TX packets:11605 errors:0 dropped:0 overruns:0 Interrupt:7 Base address:0x378 eth0:0 Link encap:10Mbps Ethernet HWaddr 00:8E:B8:83:19:20 inet addr:172.16.3.10 Bcast:172.16.3.255 Mask:255.255.255.0 UP BROADCAST RUNNING MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 TX packets:0 errors:0 dropped:0 overruns:0 eth0:1 Link encap:10Mbps Ethernet HWaddr 00:8E:B8:83:19:20 inet addr:172.16.3.100 Bcast:172.16.3.255 Mask:255.255.255.0 UP BROADCAST RUNNING MTU:1500 Metric:1 RX packets:1 errors:0 dropped:0 overruns:0 TX packets:0 errors:0 dropped:0 overruns:0 |
和/proc/net/aliases:
device family address eth0:0 2 172.16.3.10 eth0:1 2 172.16.3.100 |
和/proc/net/alias_types:
type name n_attach 2 ip 2 |
当然,/proc/net中的内容是由 ifconfig 命令创建的,而不是手动创建的!