Linux命令的ifconfig - 网络配置命令



本文链接: http://codingstandards.iteye.com/blog/1130135  (转载请注明出处)

 
用途说明

ifconfig命令常用来显示系统中的网络接口(网卡)信息,也可以用来配置网络接口(configure a network interface),如激活、关闭、设置地址等。在Linux系统中,网卡命名规律:eth0为第一块以太网卡(Ethernet Card),eth1为第二块。lo为环回接口,它的IP地址固定为127.0.0.1,掩码8位。

 
常用参数

格式:ifconfig

显示当前激活的网络接口信息。
If no arguments are given, ifconfig displays the status of the currently active interfaces.

 

格式:ifconfig {INTERFACE}

显示指定网络接口的信息。比如:eth0, eth1。

If a single interface  argument  is given, it displays the status of the given interface only;

 

格式:ifconfig -a

显示所有网络接口的信息,无论是否激活。

if a single -a argument is given, it displays the status of all interfaces, even those that are down.

Display info on all network interfaces on server, active or inactive.

 

其他格式,用来配置网络接口。

Otherwise, it configures an interface.

 

格式:ifconfig {INTERFACE} up

格式:ifup {INTERFACE}

激活指定网络接口。比如:eth0, eth1。

This  flag  causes the interface to be activated.  It is implicitly specified if an address is assigned to the interface.

 

格式:ifconfig {INTERFACE} down

格式:ifdown {INTERFACE}

关闭指定网络接口。

This flag causes the driver for this interface to be shut  down.

格式:ifconfig {INTERFACE} {IP}

格式:ifconfig {INTERFACE} {IP} netmask {NETMASK}

为指定网络接口设置IP地址和掩码,并自动激活。比如:eth0, eth0:0, eth0:1,后两个为虚拟网卡。

 

格式:ifconfig {INTERFACE} add {IP}

格式:ifconfig {INTERFACE}:0 {IP}

为指定网络接口添加IP地址。

 

格式:ifconfig {INTERFACE} del {IP}

为指定网络接口删除IP地址。

 
使用示例
示例一 通过ifconfig命令查看当前网络接口及状态

ifconfig不带参数,只打印处于激活状态的网络接口。

[root@jfht ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:1B:78:40:8C:20
          inet addr:211.103.28.xx  Bcast:211.103.28.31  Mask:255.255.255.224
          inet6 addr: fe80::21b:78ff:fe40:8c20/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:134856806 errors:0 dropped:0 overruns:0 frame:0
          TX packets:140723373 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:1680519599 (1.5 GiB)  TX bytes:2804853589 (2.6 GiB)
          Interrupt:114 Memory:fa000000-fa012800

 

现在解释一下输出信息的含义:

eth0:网络接口
link encap:网络类型
HWaddr:网卡物理地址
Inet addr :IP地址
Bcast:广播地址
Mask:子网掩码
UP:正在使用的网络接口
RX packets,TX packets接收和传输的数据包个数
RX byte,TX byte表示接搜和传输的具体数目
Interrupt:终端信息
Base address:内存地址



eth1      Link encap:Ethernet  HWaddr 00:1B:78:40:8C:22
          inet addr:192.168.1.191  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::21b:78ff:fe40:8c22/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:29821173 errors:0 dropped:0 overruns:0 frame:0
          TX packets:28680326 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:4264958692 (3.9 GiB)  TX bytes:427504706 (407.7 MiB)
          Interrupt:122 Memory:f8000000-f8012800

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:30263265 errors:0 dropped:0 overruns:0 frame:0
          TX packets:30263265 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:63016162 (60.0 MiB)  TX bytes:63016162 (60.0 MiB)

 

ifconfig命令跟上-a参数,打印所有配置的网络接口,不论其是否激活。
[root@jfht ~]# ifconfig -a
eth0      Link encap:Ethernet  HWaddr 00:1B:78:40:8C:20
          inet addr:211.103.28.xx  Bcast:211.103.28.31  Mask:255.255.255.224
          inet6 addr: fe80::21b:78ff:fe40:8c20/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:134856877 errors:0 dropped:0 overruns:0 frame:0
          TX packets:140723396 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:1680524793 (1.5 GiB)  TX bytes:2804859207 (2.6 GiB)
          Interrupt:114 Memory:fa000000-fa012800

eth1      Link encap:Ethernet  HWaddr 00:1B:78:40:8C:22
          inet addr:192.168.1.191  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::21b:78ff:fe40:8c22/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:29821183 errors:0 dropped:0 overruns:0 frame:0
          TX packets:28680336 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:4264959332 (3.9 GiB)  TX bytes:427505346 (407.7 MiB)
          Interrupt:122 Memory:f8000000-f8012800

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:30263271 errors:0 dropped:0 overruns:0 frame:0
          TX packets:30263271 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:63016642 (60.0 MiB)  TX bytes:63016642 (60.0 MiB)

sit0      Link encap:IPv6-in-IPv4
          NOARP  MTU:1480  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:0
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

 

ifconfig命令跟上指定网络接口名称,就可以查看具体的网卡信息了。
[root@jfht ~]# ifconfig eth1
eth1      Link encap:Ethernet  HWaddr 00:1B:78:40:8C:22
          inet addr:192.168.1.191  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::21b:78ff:fe40:8c22/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:29821190 errors:0 dropped:0 overruns:0 frame:0
          TX packets:28680343 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:4264959780 (3.9 GiB)  TX bytes:427505794 (407.7 MiB)
          Interrupt:122 Memory:f8000000-f8012800

[root@jfht ~]#

 
示例二 在VMWare虚拟机中用ifconfig命令关闭和激活网卡

VMWare虚拟机,网络连接采用NAT方式,操作系统为RHEL3.4。从Windows的cmd窗口通过ping命令来确认。

 

开始时,eth0处于激活状态。

C:\Users\zhy>ping 192.168.227.128

正在 Ping 192.168.227.128 具有 32 字节的数据:
来自 192.168.227.128 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.227.128 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.227.128 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.227.128 的回复: 字节=32 时间<1ms TTL=64

192.168.227.128 的 Ping 统计信息:
    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
    最短 = 0ms,最长 = 0ms,平均 = 0ms

 

在Linux的console中执行 ifconfig eth0 down 命令之后,就无法ping通了。
C:\Users\zhy>ping 192.168.227.128

正在 Ping 192.168.227.128 具有 32 字节的数据:
请求超时。
请求超时。
来自 192.168.227.1 的回复: 目标主机无法访问。
请求超时。

192.168.227.128 的 Ping 统计信息:
    数据包: 已发送 = 4,已接收 = 1,丢失 = 3 (75% 丢失),

 

在Linux的console中执行 ifconfig eth0 up 命令之后,又可以ping通了。
C:\Users\zhy>ping 192.168.227.128

正在 Ping 192.168.227.128 具有 32 字节的数据:
来自 192.168.227.128 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.227.128 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.227.128 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.227.128 的回复: 字节=32 时间<1ms TTL=64

192.168.227.128 的 Ping 统计信息:
    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
    最短 = 0ms,最长 = 0ms,平均 = 0ms

 
示例三 为一块网卡配置多个地址

在eth0上已经有一个ip地址了,再在其上增加一个ip地址。

[root@node34 root]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:E3:D2:65
          inet addr:192.168.227.128  Bcast:192.168.227.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:14766 errors:0 dropped:0 overruns:0 frame:0
          TX packets:18009 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:1530995 (1.4 Mb)  TX bytes:3088071 (2.9 Mb)
          Interrupt:10 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:2310 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2310 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:203796 (199.0 Kb)  TX bytes:203796 (199.0 Kb)

[root@node34 root]#
[root@node34 root]# ifconfig eth0:1 192.168.227.188 netmask 255.255.255.0
[root@node34 root]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:E3:D2:65
          inet addr:192.168.227.128  Bcast:192.168.227.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:14878 errors:0 dropped:0 overruns:0 frame:0
          TX packets:18097 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:1541605 (1.4 Mb)  TX bytes:3097295 (2.9 Mb)
          Interrupt:10 Base address:0x2000

eth0:1    Link encap:Ethernet  HWaddr 00:0C:29:E3:D2:65
          inet addr:192.168.227.188  Bcast:192.168.227.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:14883 errors:0 dropped:0 overruns:0 frame:0
          TX packets:18106 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:1541935 (1.4 Mb)  TX bytes:3098261 (2.9 Mb)
          Interrupt:10 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:2312 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2312 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:203972 (199.1 Kb)  TX bytes:203972 (199.1 Kb)

 

C:\Users\zhy>ping 192.168.227.188

正在 Ping 192.168.227.188 具有 32 字节的数据:
来自 192.168.227.188 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.227.188 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.227.188 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.227.188 的回复: 字节=32 时间<1ms TTL=64

192.168.227.188 的 Ping 统计信息:
    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
    最短 = 0ms,最长 = 0ms,平均 = 0ms

 

[root@node34 root]# ifconfig eth0:1 del 192.168.227.188
[root@node34 root]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:E3:D2:65
          inet addr:192.168.227.128  Bcast:192.168.227.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:15306 errors:0 dropped:0 overruns:0 frame:0
          TX packets:18496 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:1585467 (1.5 Mb)  TX bytes:3141665 (2.9 Mb)
          Interrupt:10 Base address:0x2000

eth0:2    Link encap:Ethernet  HWaddr 00:0C:29:E3:D2:65
          inet addr:192.168.227.189  Bcast:192.168.227.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:15311 errors:0 dropped:0 overruns:0 frame:0
          TX packets:18505 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:1585797 (1.5 Mb)  TX bytes:3142711 (2.9 Mb)
          Interrupt:10 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:2322 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2322 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:204852 (200.0 Kb)  TX bytes:204852 (200.0 Kb)

C:\Users\zhy>ping 192.168.227.188

正在 Ping 192.168.227.188 具有 32 字节的数据:
来自 192.168.227.1 的回复: 目标主机无法访问。
请求超时。
请求超时。
请求超时。

192.168.227.188 的 Ping 统计信息:
    数据包: 已发送 = 4,已接收 = 1,丢失 = 3 (75% 丢失),

 
示例四 使用ifconfig命令配置的网络设置,在机器重启之后就无效了

用ifconfig命令配置的网卡信息,在网卡重启后机器重启后,配置就不存在。要想将上述的配置信息永远的存的电脑里,那就要修改网卡的配置文件了。

[root@node34 root]# ifconfig eth0:1 192.168.227.189
[root@node34 root]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:E3:D2:65
          inet addr:192.168.227.128  Bcast:192.168.227.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:215 errors:0 dropped:0 overruns:0 frame:0
          TX packets:251 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:21887 (21.3 Kb)  TX bytes:22716 (22.1 Kb)
          Interrupt:10 Base address:0x2000

eth0:1    Link encap:Ethernet  HWaddr 00:0C:29:E3:D2:65
          inet addr:192.168.227.189  Bcast:192.168.227.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:215 errors:0 dropped:0 overruns:0 frame:0
          TX packets:251 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:21887 (21.3 Kb)  TX bytes:22716 (22.1 Kb)
          Interrupt:10 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:394 errors:0 dropped:0 overruns:0 frame:0
          TX packets:394 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:29423 (28.7 Kb)  TX bytes:29423 (28.7 Kb)

[root@node34 root]# reboot

Broadcast message from root (pts/1) (Thu Jul 21 19:49:25 2011):

The system is going down for reboot NOW!
[root@node34 root]#


Last login: Wed Jul 20 12:19:18 2011 from 192.168.227.1
[root@node34 root]# ifconfig -a
eth0      Link encap:Ethernet  HWaddr 00:0C:29:E3:D2:65
          inet addr:192.168.227.128  Bcast:192.168.227.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:77 errors:0 dropped:0 overruns:0 frame:0
          TX packets:104 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:8916 (8.7 Kb)  TX bytes:10906 (10.6 Kb)
          Interrupt:10 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:354 errors:0 dropped:0 overruns:0 frame:0
          TX packets:354 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:25651 (25.0 Kb)  TX bytes:25651 (25.0 Kb)

[root@node34 root]#

 
示例五 系统中网络接口配置文件样本,采用DHCP方式,并增加虚拟网卡

[root@node34 root]# cat /etc/sysconfig/network-scripts/ifcfg-eth0  
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=dhcp

 

[root@node34 root]# cat /etc/sysconfig/network-scripts/ifcfg-eth0:0  
DEVICE=eth0:0
ONBOOT=yes
#BOOTPROTO=dhcp
BOOTPROTO=static
IPADDR=192.168.227.227
NETMASK=255.255.255.0
ONBOOT=yes

 

[root@node34 root]# service network restart
正在关闭接口 eth0:                                        [  确定  ]
关闭环回接口:                                             [  确定  ]
设置网络参数:                                             [  确定  ]
弹出环回接口:                                             [  确定  ]
弹出界面 eth0:                                            [  确定  ]

[root@node34 root]# ifconfig -a
eth0      Link encap:Ethernet  HWaddr 00:0C:29:E3:D2:65
          inet addr:192.168.227.128  Bcast:192.168.227.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:389 errors:0 dropped:0 overruns:0 frame:0
          TX packets:341 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:40273 (39.3 Kb)  TX bytes:37785 (36.8 Kb)
          Interrupt:10 Base address:0x2000

eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:E3:D2:65
          inet addr:192.168.227.227  Bcast:192.168.227.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:389 errors:0 dropped:0 overruns:0 frame:0
          TX packets:341 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:40273 (39.3 Kb)  TX bytes:37785 (36.8 Kb)
          Interrupt:10 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:355 errors:0 dropped:0 overruns:0 frame:0
          TX packets:355 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:25703 (25.1 Kb)  TX bytes:25703 (25.1 Kb)

[root@node34 root]#

 
示例六 系统中网络接口配置文件样本,固定IP地址

[root@jfht ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
# Broadcom Corporation NetXtreme II BCM5706 Gigabit Ethernet
DEVICE=eth0
BOOTPROTO=static
BROADCAST=211.103.28.31
HWADDR=00:1B:78:40:8C:20
IPADDR=211.103.28.xx
IPV6INIT=yes
IPV6_AUTOCONF=yes
NETMASK=255.255.255.224
NETWORK=211.103.28.0
ONBOOT=yes
[root@jfht ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1
# Broadcom Corporation NetXtreme II BCM5706 Gigabit Ethernet
DEVICE=eth1
BOOTPROTO=static
BROADCAST=192.168.1.255
HWADDR=00:1B:78:40:8C:22
IPADDR=192.168.1.191
NETMASK=255.255.255.0
NETWORK=192.168.1.0
ONBOOT=yes
[root@jfht ~]#

 

 
问题思考
相关资料

【1】OldHawk Linux系统下ifconfig命令使用及结果分析
http://www.cnblogs.com/taobataoma/archive/2007/12/27/1016689.html
【2】鸟哥的Linux私房菜 ifconfig
http://linux.vbird.org/linux_server/0140networkcommand.php#ifconfig
【3】豆豆网 Redhat Linux网卡配置与绑定
http://tech.ddvip.com/2007-11/119605865737942.html

你可能感兴趣的:(Linux命令的ifconfig - 网络配置命令)