Linux一個主機配多個IP
首先我們在linux下查看該主機的IP地址:
#ifconfig
eth0 Link encap:Ethernet HWaddr 00:16:8d:B7:6A:E0
inet addr:192.168.1.111 Bcast:192.168.1.255 Mask:255.255.252.0
inet6 addr: fd80::213:8eff:feb7:2ae0/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:7077 errors:0 dropped:0 overruns:0 frame:0
TX packets:54 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:654811 (639.4 KiB) TX bytes:6229 (6.0 KiB)
Interrupt:10 Base address:0xb800
這裡我們只看到該主機只有一個IP地址,我們要在網卡eth0上再綁定另外一個IP,因為一般的機器它只有一張網卡,所以我們就在同一張網卡上進行多個IP的綁定。好了,我們開始做吧。
1.首先進入網卡的配置文件
#cd /etc/sysconfig/network-scripts
2.復制網卡eth0的配置文件
#cp ifcfg-eth0 ifcfg-eth0:0
3.修改ifcfg-eht0:0文件
#vi ifcfg-eth0:0
將其中的IP地址改為你的另外一個IP地址,比如我的修改如下:
DEVICE=eth0:0 #網卡的設置名稱,要跟文件名一致
BOOTPROTO=static
HWADDR=00:16:8d:B7:6A:E0
IPADDR=192.168.1.112
NETMASK=255.255.252.0
GATEWAY=192.168.0.0
ONBOOT=yes
TYPE=Ethernet
注意的是,eth0:0的物理網卡地址必須要和eth0d物理網卡地址,即HWaddr要一致,因為是在同一網卡上綁定的多個IP。保存好後,我們就可以 重啟網卡了.
4.重新啟動網卡
/etc/init.d/network restart
5.啟動後,再查看IP地址
#ifconfig
eth0 Link encap:Ethernet HWaddr 00:16:8d:B7:6A:E0
inet addr:192.168.1.111 Bcast:192.168.1.255 Mask:255.255.252.0
inet6 addr: fd80::213:8eff:feb7:2ae0/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:7077 errors:0 dropped:0 overruns:0 frame:0
TX packets:54 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:654811 (639.4 KiB) TX bytes:6229 (6.0 KiB)
Interrupt:10 Base address:0xb800
eth0:0 Link encap:Ethernet HWaddr 00:16:8d:B7:6A:E0
inet addr:192.168.1.112 Bcast:192.168.1.255 Mask:255.255.252.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
Interrupt:10 Base address:0xb800
看看,機器是不是有兩個IP地址了啊,呵呵,可以試著用另外一台機器ping下~~~
#ping 192.168.1.111
PING 192.168.1.111 (192.168.1.111) 56(84) bytes of data.
64 bytes from 192.168.1.111: icmp_seq=0 ttl=64 time=0.061 ms
64 bytes from 192.168.1.111: icmp_seq=1 ttl=64 time=0.048 ms
64 bytes from 192.168.1.111: icmp_seq=2 ttl=64 time=0.052 ms
#ping 192.168.1.112
PING 192.168.1.112 (192.168.1.112) 56(84) bytes of data.
64 bytes from 192.168.1.112: icmp_seq=0 ttl=64 time=0.063 ms
64 bytes from 192.168.1.112: icmp_seq=1 ttl=64 time=0.055 ms
64 bytes from 192.168.1.112: icmp_seq=2 ttl=64 time=0.055 ms