日期:2014-05-16  浏览次数:20609 次

Linux 在一块网卡上绑定多个IP
配置虚拟主机时有一种方式是在一块网卡上绑定多个IP,操作如下:
首先用ifconfig查看物理网卡
[root@localhost etc]# ifconfig
eth0      Link encap:Ethernet  HWaddr 08:00:27:98:1C:2E  
          inet addr:192.168.1.103  Bcast:255.255.255.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe98:1c2e/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:59217 errors:0 dropped:0 overruns:0 frame:0
          TX packets:45034 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:24197483 (23.0 MiB)  TX bytes:7882365 (7.5 MiB)
          Interrupt:10 Base address:0xd020 

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:62 errors:0 dropped:0 overruns:0 frame:0
          TX packets:62 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:8325 (8.1 KiB)  TX bytes:8325 (8.1 KiB)

lo是本地回环
eh开头的是物理网卡,我只有一块网卡,所以显示eth0

那么现在我们在eth0上绑定其他IP地址

[root@localhost etc]# ifconfig eth0:1 192.168.1.201 netmask 255.255.255.0 up
[root@localhost etc]# route add -host 192.168.1.201 dev eth0:1
[root@localhost etc]#
[root@localhost etc]# ifconfig eth0:2 192.168.1.202 netmask 255.255.255.0 up
[root@localhost etc]# route add -host 192.168.1.202 dev eth0:2


eth0:n 这里的n可以自己随便设置
route 为新地址添加路由并且绑定在相应的eth0:n上

ok,现在在其他机器上ping一下,因该已经通了:)

注意:
1.这里绑定的其他IP,会在机器重启时消失,如果想永久绑定,就需要在开机时写一个shell,把上边的shell贴在:(/etc/rc.local)文件里就OK了:)
2.删除绑定IP:
[root@localhost etc]# ifconfig eth0:1 down
[root@localhost etc]# ifconfig eth0:2 down

3.删除路由(如果已经删除绑定IP,路由会自动消失)
[root@localhost etc]# route del 192.168.1.201 dev eth0:1
[root@localhost etc]# route del 192.168.1.202 dev eth0:2