最近由於測試的需要,
在一台linux 機器下配置單網卡多個IP,
指令碼如下
#!/bin/sh# ip_alias.sh#author.....#20130110#for addint or delete 1,000 net-card alias#from 10.3.231.1~200#from 10.3.232.1~200#from 10.3.233.1~200#from 10.3.234.1~200#from 10.3.235.1~200#10.3.231.1 168027905 #10.3.231.200 168028104#10.3.232.1 168028161#10.3.232.200 168028360#10.3.233.1 168028417#10.3.233.200 168028616#10.3.234.1 168028673#10.3.234.200 168028872#10.3.235.1 168028929#10.3.235.200 168029128#usage: sh ip_alias.sh add#usage: sh ip_alias.sh delcc_max=235bb_max=200ip_pre="10.3."net_card_num=0;if [ "$1" == "add" ];thenfor (( i = 231; i <= cc_max ; i ++ ))do for ((j = 1; j <= bb_max; j ++)) do ip=${ip_pre}${i}"."${j} let net_card_num+=1; echo $net_card_num; ifconfig eth0:$net_card_num $ip netmask 255.255.0.0 up echo $ip donedoneelif [ "$1" == "del" ];then for (( i = 231; i <= cc_max ; i ++ )) do for ((j = 1; j <= bb_max; j ++)) do ip=${ip_pre}${i}"."${j} let net_card_num+=1; echo $net_card_num; ifconfig eth0:$net_card_num $ip netmask 255.255.0.0 down echo $ip donedoneelse echo "error parameter" echo "#usage: sh ip_alias.sh add" echo "#usage: sh ip_alias.sh del"fi
參考了...
http://bbs.neten.de/viewthread.php?tid=574
IP Alias - linux中單網卡多IP可以運行命令ifconfig 添加IP Alias
在eth0:0上添加IP地址192.168.0.128/255.255.255.0
複製內容到剪貼簿
代碼:
# ifconfig eth0:0 192.168.0.128netmask 255.255.255.0 up
刪除IP Alias eth0:0
複製內容到剪貼簿
代碼:
# ifconfig eth0:0 down
為一塊網卡配置多IP可以通過命令列設定也可以通過設定檔設定,以下我分別通過這兩種方式實現:
1.用命令列設定
複製內容到剪貼簿
代碼:
ifconfig命令格式:ifconfig 裝置名稱 IP netmask 掩碼 broadcast 廣播位址 動作
# ifconfig
複製內容到剪貼簿
代碼:
eth0 Link encap:Ethernet HWaddr 00:0E:1F:01:6C:50
inet addr:192.168.16.108 Bcast:192.168.16.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:2 errors:0 dropped:0 overruns:0 frame:0
TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:224 (224.0 b) TX bytes:387 (387.0 b)
Interrupt:11 Base address:0x2000
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:773 errors:0 dropped:0 overruns:0 frame:0
TX packets:773 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:52352 (51.1 Kb) TX bytes:52352 (51.1 Kb)
可以看到,當前eth0的IP為192.168.16.108,此時請注意一下MAC(00:0E:1F:01:6C:50)
現在我們為eth0綁定另一個IP,裝置為eth0:0,可以看到,現在又多了一個IP,注意,網卡實際上只有一塊(可以看MAC,都為00:0E:1F:01:6C:50),卻綁定了兩個IP,可以依此配置多個IP。
複製內容到剪貼簿
代碼:
# ifconfig eth0:0 192.168.1.100 netmask 255.255.255.0
# ifconfig
複製內容到剪貼簿
代碼:
eth0 Link encap:Ethernet HWaddr 00:0E:1F:01:6C:50
inet addr:192.168.16.108 Bcast:192.168.16.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:29 errors:0 dropped:0 overruns:0 frame:0
TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:2879 (2.8 Kb) TX bytes:387 (387.0 b)
Interrupt:11 Base address:0x2000
eth0:0 Link encap:Ethernet HWaddr 00:0E:1F:01:6C:50
inet addr:192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:29 errors:0 dropped:0 overruns:0 frame:0
TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:2879 (2.8 Kb) TX bytes:387 (387.0 b)
Interrupt:11 Base address:0x2000
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:7329 errors:0 dropped:0 overruns:0 frame:0
TX packets:7329 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:500674 (488.9 Kb) TX bytes:500674 (488.9 Kb)
2.通過設定檔設定多IP
網卡IP配置的檔案在/etc/sysconfig/network-scripts/下,檔案分別為ehtx或ethx:x
# cd /etc/sysconfig/network-scripts/
# ls
ifcfg-eth0 ifdown-ippp ifdown-ppp ifup-aliases ifup-isdn ifup-ppp ifup-wireless
ifcfg-lo ifdown-ipv6 ifdown-sit ifup-ippp ifup-plip ifup-routes init.ipv6-global
ifdown ifdown-isdn ifdown-sl ifup-ipv6 ifup-plusb ifup-sit network-functions
ifdown-aliases ifdown-post ifup ifup-ipx ifup-post ifup-sl network-functions-ipv6
#這是eth0的對應的內容
#vi ifcfg-eth0
複製內容到剪貼簿
代碼:
DEVICE=eth0
BOOTPROTO=none
ONBOOT=yes
USERCTL=no
PEERDNS=yes
TYPE=Ethernet
IPADDR=192.168.16.108
NETMASK=255.255.255.0
HWADDR=00:0e:1f:01:6c:50
GATEWAY=192.168.16.1
NETWORK=192.168.16.0
BROADCAST=192.168.16.255
配置另一IP,因我們剛才已配置了eth0:0,所以此時要用eth0:1了
# cp ifcfg-eth0 ifcfg-eth0:1
修改設定檔,內容如下
# vi ifcfg-eth0:1
複製內容到剪貼簿
代碼:
DEVICE=eth0:1
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.2.100
NETMASK=255.255.255.0
NETWORK=192.168.2.0
啟動eth0:1,查看,此時是不是就已經有三個IP了?
# ifup eth0:1
# ifconfig
複製內容到剪貼簿
代碼:
eth0 Link encap:Ethernet HWaddr 00:0E:1F:01:6C:50
inet addr:192.168.16.108 Bcast:192.168.16.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:52 errors:0 dropped:0 overruns:0 frame:0
TX packets:10 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:6037 (5.8 Kb) TX bytes:627 (627.0 b)
Interrupt:11 Base address:0x2000
eth0:0 Link encap:Ethernet HWaddr 00:0E:1F:01:6C:50
inet addr:192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:52 errors:0 dropped:0 overruns:0 frame:0
TX packets:10 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:6037 (5.8 Kb) TX bytes:627 (627.0 b)
Interrupt:11 Base address:0x2000
eth0:1 Link encap:Ethernet HWaddr 00:0E:1F:01:6C:50
inet addr:192.168.2.100 Bcast:192.168.2.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:52 errors:0 dropped:0 overruns:0 frame:0
TX packets:10 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:6037 (5.8 Kb) TX bytes:627 (627.0 b)
Interrupt:11 Base address:0x2000
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:14131 errors:0 dropped:0 overruns:0 frame:0
TX packets:14131 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:965895 (943.2 Kb) TX bytes:965895 (943.2 Kb)
OK,配置成功,這個隨便你用哪種方法都可以,用設定檔的方法可以寫一些注釋,而用ifconfig則簡單方便,這就看你的愛好了。
轉自:Linux教程-紅旗.Redhat作業系統命令學習