ifconfig 是一个用来查看、配置、启用或禁用网络接口的工具,这个工具极为常用的。可以用这个工具来临时性的配置网卡的IP地址、掩码、广播地址、网关等。也可以把它写入一个文件中(比如/etc/rc.d/rc.local),这样系统引导后,会读取这个文件,为网卡设置IP地址

1 ifconfig 查看网络接口状态;

ifconfig 如果不接任何参数,就会输出当前网络接口的情况;

[root@localhost ~]# ifconfigeth0      Link encap:Ethernet  HWaddr 00:C0:9F:94:78:0E          inet addr:192.168.1.88  Bcast:192.168.1.255  Mask:255.255.255.0          inet6 addr: fe80::2c0:9fff:fe94:780e/64 Scope:Link          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1          RX packets:850 errors:0 dropped:0 overruns:0 frame:0          TX packets:628 errors:0 dropped:0 overruns:0 carrier:0          collisions:0 txqueuelen:1000          RX bytes:369135 (360.4 KiB)  TX bytes:75945 (74.1 KiB)          Interrupt:10 Base address:0x3000lo        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:57 errors:0 dropped:0 overruns:0 frame:0          TX packets:57 errors:0 dropped:0 overruns:0 carrier:0          collisions:0 txqueuelen:0          RX bytes:8121 (7.9 KiB)  TX bytes:8121 (7.9 KiB)


解说:
eth0 表示第一块网卡, 其中 HWaddr 表示网卡的物理地址,可以看到目前这个网卡的物理地址(MAC地址)是 00:C0:9F:94:78:0E ; inet addr 用来表示网卡的IP地址,此网卡的 IP地址是 192.168.1.88,广播地址, Bcast:192.168.1.255,掩码地址Mask:255.255.255.0

lo 是表示主机的回坏地址,这个一般是用来测试一个网络程序,但又不想让局域网或外网的用户能够查看,只能在此台主机上运行和查看所用的网络接口。比如把 HTTPD服务器的指定到回坏地址,在浏览器输入 127.0.0.1 就能看到你所架WEB网站了。但只是您能看得到,局域网的其它主机或用户无从知道;

如果想知道主机所有网络接口的情况,请用下面的命令;

[root@localhost ~]# ifconfig -a


如果想查看某个端口,比如查看eth0 的状态,就可以用下面的方法;

[root@localhost ~]# ifconfig eth0

2 ifconfig 配置网络接口;

ifconfig 可以用来配置网络接口的IP地址、掩码、网关、物理地址等;值得一说的是用ifconfig 为网卡指定IP地址,这只是用来调试网络用的,并不会更改系统关于网卡的配置文件。如果您想把网络接口的IP地址固定下来,目前有三个方法:一是通过各个发行和版本专用的工具来修改IP地址;二是直接修改网络接口的配置文件;三是修改特定的文件,加入ifconfig 指令来指定网卡的IP地址,比如在redhat或Fedora中,把ifconfig 的语名写入/etc/rc.d/rc.local文件中;

ifconfig 配置网络端口的方法:

ifconfig 工具配置网络接口的方法是通过指令的参数来达到目的的,我们只说最常用的参数;

ifconfig 网络端口 IP地址 hw <HW> MAC地址 netmask 掩码地址 broadcast 广播地址 [up/down]
* 实例一:
比如我们用ifconfig 来调试 eth0网卡的地址

[root@localhost ~]# ifconfig eth0 down
[root@localhost ~]# ifconfig eth0 192.168.1.99 broadcast 192.168.1.255 netmask 255.255.255.0
[root@localhost ~]# ifconfig eth0 up
[root@localhost ~]# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:11:00:00:11:11
           inet addr:192.168.1.99 Bcast:192.168.1.255 Mask:255.255.255.0
           UP BROADCAST MULTICAST MTU:1500 Metric:1
           RX packets:0 errors:0 dropped:0 overruns:0 frame:0
           TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
           collisions:0 txqueuelen:1000
           RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
           Interrupt:11 Base address:0x3400


注解: 上面的例子我们解说一下;

第一行:ifconfig eth0 down 表示如果eth0是激活的,就把它DOWN掉。此命令等同于 ifdown eth0;
第二行:用ifconfig 来配置 eth0的IP地址、广播地址和网络掩码;
第三行:用ifconfig eth0 up 来激活eth0 ; 此命令等同于 ifup eth0
第四行:用 ifconfig eth0 来查看 eth0的状态;

当然您也可以用直接在指令IP地址、网络掩码、广播地址的同时,激活网卡;要加up参数;比如下面的例子;

[root@localhost ~]# ifconfig eth0 192.168.1.99 broadcast 192.168.1.255 netmask 255.255.255.0 up


* 实例二:在这个例子中,我们要学会设置网络IP地址的同时,学会设置网卡的物理地址(MAC地址);

比如我们设置网卡eth1的IP地址、网络掩码、广播地址,物理地址并且激活它;

[root@localhost ~]# ifconfig eth1 192.168.1.252 hw ether 00:11:00:00:11:11 netmask 255.255.255.0 broadcast 192.168.1.255 up



[root@localhost ~]# ifconfig eth1 hw ether 00:11:00:00:11:22
[root@localhost ~]# ifconfig eth1 192.168.1.252 netmask 255.255.255.0 broadcast 192.168.1.255 up


其中 hw 后面所接的是网络接口类型, ether表示乙太网, 同时也支持 ax25 、ARCnet、netrom等,详情请查看 man ifconfig ;

为网卡配置多IP

为一块网卡配置多IP可以通过命令行设置也可以通过配置文件设置,以下我分别通过这两种方式实现:

1.用命令行设置
ifconfig命令格式:ifconfig 设备名 IP netmask 掩码 broadcast 广播地址 动作

[root@localhost root]# 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:0x2000lo        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。

[root@localhost network-scripts]# ifconfig eth0:0 192.168.1.100 netmask 255.255.255.0
[root@localhost root]# 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:0x2000eth0: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:0x2000lo        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

[root@localhost root]# cd /etc/sysconfig/network-scripts/
[root@localhost 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的对应的内容
[root@localhost network-scripts]#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了
[

root@localhost network-scripts]# cp ifcfg-eth0 ifcfg-eth0:1


修改配置文件,内容如下

[root@localhost network-scripts]# 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了?

[root@localhost network-scripts]# ifup eth0:1
[root@localhost network-scripts]# 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:0x2000eth0: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:0x2000eth0: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:0x2000lo        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)

(*^_^*)配置成功!