天天看点

Ifconfig命令详细解析

<b>1.</b><b>测试环境:</b>

[root@lvskeep ~]# cat

/etc/redhat-release

CentOS

release 5.4 (Final)

[root@lvskeep ~]# uname -r

2.6.18-164.el5

[root@lvskeep ~]# ifconfig               <b>#</b><b>本机的网络配置</b>

eth0      Link encap:Ethernet  HWaddr 00:0C:29:BC:7A:11 

          inet addr:10.0.0.30  Bcast:10.0.0.255  Mask:255.255.255.0

          inet6 addr: fe80::20c:29ff:febc:7a11/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:893 errors:0 dropped:0

overruns:0 frame:0

          TX packets:447 errors:0 dropped:0 overruns:0

carrier:0

          collisions:0 txqueuelen:1000

          RX bytes:91733 (89.5 KiB)  TX bytes:84456 (82.4 KiB)

          Interrupt:185 Base address:0x1400

          inet addr:192.168.0.30  Bcast:192.168.0.255  Mask:255.255.255.0

          inet6 addr: fe80::20c:29ff:febc:7a1b/64 Scope:Link

          RX packets:25 errors:0 dropped:0

          TX packets:44 errors:0 dropped:0

overruns:0 carrier:0

          RX bytes:6622 (6.4 KiB)  TX bytes:7942 (7.7 KiB)

          Interrupt:169 Base address:0x1480

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:16 errors:0 dropped:0

          TX packets:16 errors:0 dropped:0

          collisions:0 txqueuelen:0

          RX bytes:1224 (1.1 KiB)  TX bytes:1224 (1.1 KiB)

说明:

eth0 表示第一块网卡, 其中 HWaddr 表示网卡的物理地址,我们可以看到目前这

个网卡的物理地址(MAC地址)是 00:0C:29:BC:7A:1B ; inet addr 用来表示网卡

的IP地址,此网卡的 IP地址是 10.0.0.30, 广播地址,

Bcast:10.0.0.255,掩码地址Mask:255.255.255.0

lo 是表示主机的回坏地址,这个一般是用来测试一个网络程序,但又不想让局域

网或外网的用户能够查看,只能在此台主机上运行和查看所用的网络接口。比如我

们把HTTPD服务器的指定到回坏地址,在浏览器输入 127.0.0.1 就能看到你所架

WEB网站了。但只是您能看得到,局域网的其它主机或用户无从知道;

<b>2.</b><b>通过man命令查看系统自带的帮助</b>

[root@lvskeep ~]# man ifconfig

NAME

       ifconfig - configure a network interface

SYNOPSIS   #摘要,概要,大意

       ifconfig [interface]

       ifconfig interface [aftype] options |

address ...

DESCRIPTION

       Ifconfig is used to configure the

kernel-resident network interfaces.  It

is used at boot time to set up interfaces as necessary.  After that, it is usually only needed when

debugging or when system tuning  is

needed.

If no arguments are given,

ifconfig displays the status of the currently active interfaces.  If a single interface argument is given, it

displays the status of the given interface only; if a single -a argument is  given, it displays the status of all

interfaces, even those that are down.  Otherwise, it configures an interface.

Address

Families

       If the first argument after the

interface name is recognized as the name of a supported address family,that

address family is used for decoding and displaying all protocol addresses.  Currently supported address families include

inet (TCP/IP, default), inet6 (IPv6), ax25 (AMPR Packet Radio), ddp  (Appletalk

Phase

2), ipx (Novell IPX) and netrom (AMPR Packet radio).  All numbers supplied as parts in IPv4 dotted

decimal notation may be decimal, octal, or hexadecimal, as specified in the ISO

C standard (that is, a leading 0x or 0X implies hexadecimal; otherwise, a

leading implies octal; otherwise,the number is interpreted as decimal). Use of

hexamedial and octal numbers is not RFC-compliant and therefore its  use is discouraged and may go away.

OPTIONS

       <b>Interface   #e.g eth0</b><b>、eth1</b>

              The  name of the interface.  This is usually a driver name followed by a

unit number, for example eth0 for the first Ethernet interface.

e.g

[root@lvskeep ~]# ifconfig

eth0

          inet addr:10.0.0.30  Bcast:10.0.0.255  Mask:255.255.255.0

          RX packets:673 errors:0 dropped:0 overruns:0 frame:0

          TX packets:338 errors:0 dropped:0 overruns:0 carrier:0

          RX bytes:68647 (67.0 KiB)  TX

bytes:66442 (64.8 KiB)

 <b>up</b>     This flag causes the interface to be activated.  It is implicitly  specified  if  an  address  is assigned to the interface.

在虚拟机上执行ifconfig eth0 up命令,激活eth0

[root@lvskeep ~]# ifconfig eth0 up

[root@lvskeep ~]# ifconfig              <b>#</b><b>验证是否激活</b>

          RX packets:893 errors:0 dropped:0 overruns:0

frame:0

          TX packets:447 errors:0 dropped:0

eth1      Link encap:Ethernet  HWaddr 00:0C:29:BC:7A:1B 

lo        Link encap:Local Loopback 

看到eth0已经成功激活

<b>down </b>  This flag causes the

driver for this interface to be shut down.

SecureCRT软件

eth0 down

结果:光标不停闪烁

直接在虚拟机里操作

eth1      Link

encap:Ethernet  HWaddr 00:0C:29:BC:7A:1B 

          inet

addr:192.168.0.30  Bcast:192.168.0.255  Mask:255.255.255.0

          UP BROADCAST

RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:25

errors:0 dropped:0 overruns:0 frame:0

          TX packets:44

errors:0 dropped:0 overruns:0 carrier:0

          collisions:0

txqueuelen:1000

          RX bytes:6622 (6.4

KiB)  TX bytes:7942 (7.7 KiB)

          Interrupt:169 Base

address:0x1480

lo        Link encap:Local

Loopback 

          inet6 addr:

::1/128 Scope:Host

          UP LOOPBACK

RUNNING  MTU:16436  Metric:1

          RX packets:16

          TX packets:16

txqueuelen:0

          RX bytes:1224 (1.1

KiB)  TX bytes:1224 (1.1 KiB)

说明:执行ifconfig eth0 down命令后,网卡eth0就停掉了,不工作了,已经断开了连接,而SecureCRT就是通过eth0连接的虚拟机,所以光标会一直闪烁;通过在虚拟机上执行ifconfig命令验证,eth0确实停掉了。

       <b>[-]arp</b> Enable or disable the use of the ARP protocol on this interface.

[root@lvskeep ~]# ifconfig eth0 –arp            <b> #</b><b>关闭了ARP协议</b>

执行完此命令后,SecureCRT和虚拟机断开连接。关闭ARP,IP地址和MAC地址失去了对应关系,无法识别对应的主机。

[root@lvskeep ~]# ifconfig eth0 arp               <b>#</b><b>开启ARP协议</b>

再次连接正常   <b>arp  </b><b>命令(附录一)</b>

<b>[-]promisc</b>

              Enable or disable the promiscuous

mode of the interface.  If selected, all

packets on the network will be received by the interface.

       <b>[-]allmulti</b>

              Enable or disable all-multicast

mode.  If selected, all multicast packets

on the network will  be received by the

interface.

       <b>metric</b>

N   #指定在计算数据包的转送次数时,所要加上的数目<b></b>

              This parameter sets the interface

metric.

       <b>mtu</b>

N  This parameter sets the Maximum

Transfer Unit (MTU) of an interface.

              #设置网络设备的MTU

       <b>dstaddr</b>

addr

              Set the remote IP address for a

point-to-point link (such as PPP).  This

keyword is now obsolete;use the pointopoint keyword instead.

       <b>netmask</b>

              Set the IP network mask for this

interface.  This value defaults to the

usual class  A,  B  or  C network mask (as derived

from the interface IP address), but it can be set to any value.

[root@lvskeep ~]# ifconfig eth1

192.168.0.30 netmask 255.255.0.0    #子网掩码

          inet addr:192.168.0.30  Bcast:192.168.255.255  Mask:255.255.0.0

          UP BROADCAST RUNNING NOARP

MULTICAST  MTU:1500  Metric:1

          TX packets:54 errors:0 dropped:0

          RX bytes:6622 (6.4 KiB)  TX bytes:11144 (10.8 KiB)

注意:netmask不能大写

       <b>add</b>

addr/prefixlen

              Add an IPv6 address to an

interface. #设置网络设备IPv6的IP地址

       <b>del</b><b> addr/prefixlen</b>

              Remove an IPv6 address from an

interface.# 删除网络设备IPv6的IP地址

       <b>tunnel</b>

aa.bb.cc.dd

              Create a new SIT (IPv6-in-IPv4)

device, tunnelling to the given destination.  #建立IPv4与IPv6之间的隧道通信地址

       <b>irq</b>

              Set  the  interrupt  line  used by this device.  Not all devices can dynamically change their

IRQ setting.# 置网络设备的IRQ

      <b> io_addr addr  </b>#设置网络设备的I/O地址<b></b>

              Set the start address in I/O

space for this device.

       <b>mem_start</b>

addr    #设置网络设备在主内存所占用的起始地址<b></b>

              Set the start address for shared

memory used by this device.  Only a few

devices need this.

       <b>media</b>

type     #设置网络设备的媒介类型<b></b>

              Set the physical port or medium

type to be used by the device.  Not all

devices can  change this setting,  and  those  that  can  vary  in  what values they support.  Typical values for type are 10base2 (thin

Ethernet), 10baseT (twisted-pair 10Mbps Ethernet), AUI (external  transceiver)and so on. The special medium

type of auto can be used to tell the driver to auto-sense the media.Again, not

all drivers can do this.

       [-]broadcast [addr] #将要送往指定地址的数据包当成广播数据包来处理

              If the address argument is given, set

the protocol broadcast address for this interface.Other-wise, set (or clear)

the IFF_BROADCAST flag for the interface.

       <b>[-]pointopoint</b>

[addr] # 与指定地址的网络设备建立直接连线,此模式具有保密功能。

              This  keyword  enables  the point-to-point mode

of an interface, meaning that it is a direct link between two machines with

nobody else listening on it. If the address argument is also given, set the

protocol address of the other side  of  the  link,just  like the obsolete dstaddr keyword does.  Otherwise, set or clear the IFF_POINTOPOINT flag for the interface.

       <b>hw</b>

class address

              Set the hardware address of this

interface, if the device driver supports  this  operation.   The

       <b>multicast</b>

              Set  the  multicast  flag on the interface. This should not

normally be needed as the drivers set the flag correctly themselves.

       <b>address</b>

              The IP address to be assigned to

this interface.

       <b>txqueuelen</b>

length

              Set the length of the transmit

queue of the device. It is useful to set this to small values  for slower  devices  with a high latency

(modem links, ISDN) to prevent fast bulk transfers from dis-

              turbing interactive traffic like

telnet too much.

NOTES

       Since kernel release 2.2 there are no

explicit interface statistics for alias  interfaces  anymore.  The statistics  printed  for the original address are shared with all alias addresses on the same

device. If  you want per-address statistics

you should add explicit accounting rules for the address using  the ipchains(8) command.

Interrupt  problems  with Ethernet device drivers fail with EAGAIN. See

http://www.scyld.com/expert/irq-conflict.html for more information.

FILES

       /proc/net/socket

       /proc/net/dev

       /proc/net/if_inet6

BUGS

       While appletalk DDP and IPX addresses

will be displayed they cannot be altered by this command.

SEE

ALSO

       route(8), netstat(8), arp(8), rarp(8),

ipchains(8)

AUTHORS

       Fred N. van Kempen,

       Alan Cox,

       Phil Blundell,

       Andi Kleen

net-tools                       14 August 2000                     IFCONFIG(8)

<b>实例:</b>

<b>1.</b><b>如果我们想知道主机所有网络接口的情况,请用下面的命令:</b>

<b>[root@lvskeep</b>

~]# ifconfig -a

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:2974 errors:0 dropped:0 overruns:0 frame:0

          TX packets:1342 errors:0 dropped:0 overruns:0 carrier:0

          RX bytes:280167 (273.6 KiB)  TX

bytes:214214 (209.1 KiB)

          UP BROADCAST RUNNING NOARP MULTICAST  MTU:1500  Metric:1

          RX packets:25 errors:0 dropped:0 overruns:0 frame:0

          TX packets:66 errors:0 dropped:0 overruns:0 carrier:0

          RX bytes:6622 (6.4 KiB)  TX bytes:14376 (14.0 KiB)

          RX packets:108 errors:0 dropped:0 overruns:0 frame:0

          TX packets:108 errors:0 dropped:0 overruns:0 carrier:0

          RX bytes:11816 (11.5 KiB)  TX

bytes:11816 (11.5 KiB)

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

          RX bytes:0 (0.0 b)  TX bytes:0

(0.0 b)

<b>2.</b><b>如果我们想查看某个端口,比如我们想查看eth1 的状态,就可以用下面的方法</b>

~]# ifconfig eth1

          RX bytes:6622 (6.4 KiB)  TX

bytes:14376 (14.0 KiB)

<b>ifconfig </b><b>配置网络端口的方法</b>

<b>ifconfig </b><b>工具配置网络接口的方法是通过指令的参数来达到目的的</b>

<b>ifconfig  </b><b>网络端口  IP地址    hw   MAC地址  netmask  掩码地址   </b>

<b>broadcast  </b><b>广播地址   [up/down]</b>

<b>3.</b><b>用ifconfig 来调试 eth1网卡的地址</b>

eth1 down

eth1 192.168.1.30 broadcast 192.168.1.255 netmask 255.255.255.0

eth1 up

eth1

          inet addr:192.168.1.30  Bcast:192.168.1.255  Mask:255.255.255.0

          TX packets:91 errors:0 dropped:0 overruns:0 carrier:0

bytes:21736 (21.2 KiB)

第一行:ifconfig eth0 down 表示如果eth0是激活的,就把它DOWN掉。此命令等同于 ifdown eth0;

第二行:用ifconfig 来配置 eth0的IP地址、广播地址和网络掩码;

第三行:用ifconfig eth0 up 来激活eth0 ; 此命令等同于 ifup eth0

第四行:用 ifconfig eth0 来查看 eth0的状态;

也可以用直接在指令IP地址、网络掩码、广播地址的同时,激活网卡;要加up参数

[root@lvskeep ~]# ifconfig eth1 192.168.1.30

broadcast 192.168.1.255 netmask 255.255.255.0 up

<b>4.</b><b>设置网卡eth1的IP地址、网络掩码、广播地址,物理地址并且激活它</b>

<b>[root@lvskeep ~]# ifconfig</b>

eth1 192.168.0.30 hw ether 00:0C:29:BC:7A:1<b>1</b><b> broadcast 192.168.0.255 netmask 255.255.255.0 up</b>

<b>SIOCSIFHWADDR: Device or</b>

resource busy

<b>先停掉然后再改,MAC地址最好别改,很容易出错,</b><b>个人理解。</b><b></b>

继续阅读