快捷搜索:   nginx

如何在Linux机器上设定IP别名

   指令:
* 首先载入 IP Alias 模组(如果你将它编译进核心可跳过此步骤):
/sbin/insmod /lib/modules/`uname -r`/ipv4/ip_alias.o
* 再来,建立回绕(loopback)装置,以及 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 号码及预设递送路径:
/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
就这样了.
在上面的例子中,为了说明我用的是 RFC 1918 所定义的私用 IP 号码.用你正式或
私有的 IP 号码来取代它.
上面的例子只展示了三个 IP 号码.在 /usr/include/linux/net_alias.h 中定义的
最大值是 256.在一片网路卡上 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 指令产生而非用手写的!

顶(0)
踩(0)

您可能还会对下面的文章感兴趣:

最新评论