linux下一个网卡如何配置多个IP? 最近遇到一个问题需要单网卡配置多个ip地址。文本控制台下面有netconfig,还有ifconfig工具,使用man ifconfig查看,最常用的给网卡配置ip的命令为
#ifconfig eth0 192.168.10.1 netmask 255.255.255.0 up
说明:
在Linux中eth0是第一块网卡,其他依次为eth1,eth2......eth*
192.168.10.1是给网卡配置的第一个ip地址
netmask 255.255.255.0 配置的是子网掩码
up是表示立即激活
如果给单个网卡eth0配置多个ip地址如何操作呢,如果使用ifconfig命令那么上边需要改动的地方只有eth0这个而已,经查阅很多资料才知道再给网卡配置多个ip地址是要将eth0改为eth0:x(x是0-255例如eth0:0或者eth0:1等等),eth0:x称为虚拟网络接口,是建立在网络接口(eth0)之上。
所以给单网卡配置多ip的方法就是使用命令:
#ifconfig eth0:0 192.168.0.1 netmask 255.255.255.0 up
#ifconfig eth0:1 192.168.0.2 netmask 255.255.255.0 up
配置网卡的配置文件在目录/etc/sysconfig/network-script/下 ,原来网卡的配置文件名为ifcfg-eth0 ,我们给一块网卡配置多个IP地址的配置文件命名为ifcfg-eth0:1和ifcfg-eth0 :2
下面看操作如下:
Last login: Sat Apr 19 15:04:59 2014 from 192.168.10.109
[root@QMD ~]# ifconfig ####先查看一下网卡配置
eth0 Link encap:Ethernet HWaddr 00:0C:29:ED:06:42
inet addr:192.168.10.103 Bcast:192.168.10.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:feed:642/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:6971 errors:0 dropped:0 overruns:0 frame:0
TX packets:3101 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:3193538 (3.0 MiB) TX bytes:348586 (340.4 KiB)
Interrupt:19 Base address:0x2000
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:9 errors:0 dropped:0 overruns:0 frame:0
TX packets:9 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:612 (612.0 b) TX bytes:612 (612.0 b)
[root@QMD ~]# cd /etc/sysconfig/network-scripts/ ####切换到网卡的配置路径
[root@QMD network-scripts]# ls ####查看一下要配置的网卡
ifcfg-eth0 ifdown-post ifup-eth ifup-routes
ifcfg-lo ifdown-ppp ifup-ippp ifup-sit
ifdown ifdown-routes ifup-ipv6 ifup-tunnel
ifdown-bnep ifdown-sit ifup-isdn ifup-wireless
ifdown-eth ifdown-tunnel ifup-plip init.ipv6-global
ifdown-ippp ifup ifup-plusb net.hotplug
ifdown-ipv6 ifup-aliases ifup-post network-functions
ifdown-isdn ifup-bnep ifup-ppp network-functions-ipv6
[root@QMD network-scripts]# cp ifcfg-eth0 ifcfg-eth0:1 ####拷贝网卡配置文件eth0:1
[root@QMD network-scripts]# cp ifcfg-eth0 ifcfg-eth0:2 ####拷贝网卡配置文件改名为eth0:2
[root@QMD network-scripts]# vi ifcfg-eth0:1 ####然后编辑这块网卡
DEVICE=eth0:1 ###将设备名改为eth0:1
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
DNS2=8.8.8.8
DNS1=202.106.0.20
IPV6INIT=no
USERCTL=no
IPADDR=192.168.10.111 ####修改一下IP地址
NETMASK=255.255.255.0
GATEWAY=192.168.10.1
~
"ifcfg-eth0:1" 12L, 191C written
=============================================================
=============================================================
[root@QMD network-scripts]# vi ifcfg-eth0:2 ####再来编辑第二块网卡
DEVICE=eth0:2 ###将设备名改为eth0:2
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
DNS2=8.8.8.8
DNS1=202.106.0.20
IPV6INIT=no
USERCTL=no
IPADDR=192.168.10.222 ###同样修改IP地址
NETMASK=255.255.255.0
GATEWAY=192.168.10.1
~
"ifcfg-eth0:2" 12L, 189C written
=================================================================
=================================================================
[root@QMD network-scripts]# /etc/init.d/network restart ####然后重启一下网卡
Shutting down interface eth0: [ OK ]
Shutting down loopback interface: [ OK ]
Bringing up loopback interface: [ OK ]
Bringing up interface eth0: RTNETLINK answers: File exists
[ OK ]
[root@QMD ~]# ifconfig ####查看一下网卡是否配好
eth0 Link encap:Ethernet HWaddr 00:0C:29:9E:F1:C8
inet addr:192.168.10.103 Bcast:192.168.10.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe9e:f1c8/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:14092 errors:0 dropped:0 overruns:0 frame:0
TX packets:9607 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1181525 (1.1 MiB) TX bytes:1031417 (1007.2 KiB)
Interrupt:19 Base address:0x2000
eth0:1 Link encap:Ethernet HWaddr 00:0C:29:9E:F1:C8
inet addr:192.168.10.111 Bcast:192.168.10.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
Interrupt:19 Base address:0x2000
eth0:2 Link encap:Ethernet HWaddr 00:0C:29:9E:F1:C8
inet addr:192.168.10.222 Bcast:192.168.10.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
Interrupt:19 Base address:0x2000
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:53 errors:0 dropped:0 overruns:0 frame:0
TX packets:53 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:4770 (4.6 KiB) TX bytes:4770 (4.6 KiB)
[root@QMD network-scripts]# ls ####查看一下多了两个网卡配置文件
ifcfg-eth0 ifdown-isdn ifup-eth ifup-sit
ifcfg-eth0:1 ifdown-post ifup-ippp ifup-tunnel
ifcfg-eth0:2 ifdown-ppp ifup-ipv6 ifup-wireless
ifcfg-lo ifdown-routes ifup-isdn init.ipv6-global
ifdown ifdown-sit ifup-plip net.hotplug
ifdown-bnep ifdown-tunnel ifup-plusb network-functions
ifdown-eth ifup ifup-post network-functions-ipv6
ifdown-ippp ifup-aliases ifup-ppp
ifdown-ipv6 ifup-bnep ifup-routes
如何关闭一个ip呢?
则使用
#ifconfig eth*[:x] down(*代表的是网卡编号,x代表虚拟接口号0-255)
[root@QMD network-scripts]# ifconfig eth0:1 down ####关闭eth0:1 IP
[root@QMD network-scripts]# ifconfig ###查看一下
eth0 Link encap:Ethernet HWaddr 00:0C:29:9E:F1:C8
inet addr:192.168.10.103 Bcast:192.168.10.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe9e:f1c8/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:14483 errors:0 dropped:0 overruns:0 frame:0
TX packets:9737 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1215239 (1.1 MiB) TX bytes:1046779 (1022.2 KiB)
Interrupt:19 Base address:0x2000
eth0:2 Link encap:Ethernet HWaddr 00:0C:29:9E:F1:C8
inet addr:192.168.10.222 Bcast:192.168.10.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
Interrupt:19 Base address:0x2000
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:53 errors:0 dropped:0 overruns:0 frame:0
TX packets:53 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:4770 (4.6 KiB) TX bytes:4770 (4.6 KiB)