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

本文介绍如何在同一块物理网卡上配置多个IP地址的方法,并提供具体的Shell命令实例。此外,还介绍了如何设置这些IP地址在系统重启后仍能生效,以及如何删除已配置的IP地址和相关路由。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

配置虚拟主机时有一种方式是在一块网卡上绑定多个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,路由会自动消失)

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

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值