虚拟主机上给一个网卡设置多个IP地址

配置虚拟主机时有一种方式是在一块网卡上绑定多个IP,操作如下:
首先用ifconfig查看物理网卡

Shell代码 复制代码
  1. [root@devserver1 ~]# ifconfig   
  2. eth0      Link encap:Ethernet  HWaddr 00:E0:4C:F1:5B:E3   
  3.           inet addr:192.168.0.66  Bcast:192.168.0.255  Mask:255.255.255.0  
  4.           inet6 addr: fe80::2e0:4cff:fef1:5be3/64 Scope:Link   
  5.           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1  
  6.           RX packets:136875410 errors:1514 dropped:0 overruns:0 frame:1  
  7.           TX packets:33575076 errors:0 dropped:0 overruns:0 carrier:0  
  8.           collisions:0 txqueuelen:1000  
  9.           RX bytes:850013826 (810.6 MiB)  TX bytes:550942326 (525.4 MiB)   
  10.           Interrupt:177 Base address:0x8000  
  11.   
  12. lo        Link encap:Local Loopback   
  13.           inet addr:127.0.0.1  Mask:255.0.0.0  
  14.           inet6 addr: ::1/128 Scope:Host   
  15.           UP LOOPBACK RUNNING  MTU:16436  Metric:1  
  16.           RX packets:9013362 errors:0 dropped:0 overruns:0 frame:0  
  17.           TX packets:9013362 errors:0 dropped:0 overruns:0 carrier:0  
  18.           collisions:0 txqueuelen:0  
  19.           RX bytes:2146802575 (1.9 GiB)  TX bytes:2146802575 (1.9 GiB)  
[root@devserver1 ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:E0:4C:F1:5B:E3
          inet addr:192.168.0.66  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::2e0:4cff:fef1:5be3/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:136875410 errors:1514 dropped:0 overruns:0 frame:1
          TX packets:33575076 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:850013826 (810.6 MiB)  TX bytes:550942326 (525.4 MiB)
          Interrupt:177 Base address:0x8000

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:9013362 errors:0 dropped:0 overruns:0 frame:0
          TX packets:9013362 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:2146802575 (1.9 GiB)  TX bytes:2146802575 (1.9 GiB)

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

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

 

Shell代码 复制代码
  1. [root@devserver1 ~]# ifconfig eth0:1 192.168.0.201 netmask 255.255.255.0 up   
  2. [root@devserver1 ~]# route add -host 192.168.0.201 dev eth0:1  
  3. [root@devserver1 ~]#   
  4. [root@devserver1 ~]# ifconfig eth0:2 192.168.0.202 netmask 255.255.255.0 up   
  5. [root@devserver1 ~]# route add -host 192.168.0.202 dev eth0:2  
[root@devserver1 ~]# ifconfig eth0:1 192.168.0.201 netmask 255.255.255.0 up
[root@devserver1 ~]# route add -host 192.168.0.201 dev eth0:1
[root@devserver1 ~]#
[root@devserver1 ~]# ifconfig eth0:2 192.168.0.202 netmask 255.255.255.0 up
[root@devserver1 ~]# route add -host 192.168.0.202 dev eth0:2

 

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

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


注意:
1.这里绑定的其他IP,会在机器重启时消失,如果想永久绑定,就需要在开机时写一个shell,把上边的shell贴在:

(/etc/rc.local)文件里就OK了:)
2.删除绑定IP:

Shell代码 复制代码
  1. [root@localhost etc]# ifconfig eth0:1 down   
  2. [root@localhost etc]# ifconfig eth0:2 down  
[root@localhost etc]# ifconfig eth0:1 down
[root@localhost etc]# ifconfig eth0:2 down

 3.删除路由(如果已经删除绑定IP,路由会自动消失)

扫描二维码关注公众号,回复: 1415279 查看本文章
Shell代码 复制代码
  1. [root@devserver1 ~]# route del 192.168.0.201 dev eth0:1  
  2. [root@devserver1 ~]# route del 192.168.0.202 dev eth0:2  
[root@devserver1 ~]# route del 192.168.0.201 dev eth0:1
[root@devserver1 ~]# route del 192.168.0.202 dev eth0:2

附加:

如何启动/禁用网卡

启用

Shell代码 复制代码
  1. ifconfig eth0 up  
ifconfig eth0 up

 禁用

Shell代码 复制代码
  1. ifconfig eth0 down  
ifconfig eth0 down

 给网卡赋予静态ip

Shell代码 复制代码
  1. ifconfig eth0 192.168.1.101  
ifconfig eth0 192.168.1.101

 

猜你喜欢

转载自fireinwind.iteye.com/blog/783997