2017年(25)
分类: 系统运维
2017-05-18 11:47:04
原文地址:Ifconfig命令详细解析 作者:woaimaidong
1.测试环境:
[root@lvskeep ~]# cat /etc/redhat-release
CentOS release 5.4 (Final)
[root@lvskeep ~]# uname -r
2.6.18-164.el5
[root@lvskeep ~]# ifconfig #本机的网络配置
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
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
inet6 addr: fe80::20c:29ff:febc:7a1b/64 Scope:Link
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
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 overruns:0 frame:0
TX packets:16 errors:0 dropped:0 overruns:0 carrier: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网站了。但只是您能看得到,局域网的其它主机或用户无从知道;
2.通过man命令查看系统自带的帮助
[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
Interface #e.g eth0、eth1
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
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:673 errors:0 dropped:0 overruns:0 frame:0
TX packets:338 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:68647 (67.0 KiB) TX bytes:66442 (64.8 KiB)
Interrupt:185 Base address:0x1400
up This flag causes the interface to be activated. It is implicitly specified if an address is assigned to the interface.
e.g
在虚拟机上执行ifconfig eth0 up命令,激活eth0
[root@lvskeep ~]# ifconfig eth0 up
[root@lvskeep ~]# ifconfig #验证是否激活
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
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
inet6 addr: fe80::20c:29ff:febc:7a1b/64 Scope:Link
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
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 overruns:0 frame:0
TX packets:16 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1224 (1.1 KiB) TX bytes:1224 (1.1 KiB)
看到eth0已经成功激活
down This flag causes the driver for this interface to be shut down.
e.g
SecureCRT软件
[root@lvskeep ~]# ifconfig eth0 down
结果:光标不停闪烁
直接在虚拟机里操作
[root@lvskeep ~]# ifconfig
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
inet6 addr: fe80::20c:29ff:febc:7a1b/64 Scope:Link
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
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 overruns:0 frame:0
TX packets:16 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1224 (1.1 KiB) TX bytes:1224 (1.1 KiB)
说明:执行ifconfig eth0 down命令后,网卡eth0就停掉了,不工作了,已经断开了连接,而SecureCRT就是通过eth0连接的虚拟机,所以光标会一直闪烁;通过在虚拟机上执行ifconfig命令验证,eth0确实停掉了。
[-]arp Enable or disable the use of the ARP protocol on this interface.
e.g
[root@lvskeep ~]# ifconfig eth0 –arp #关闭了ARP协议
执行完此命令后,SecureCRT和虚拟机断开连接。关闭ARP,IP地址和MAC地址失去了对应关系,无法识别对应的主机。
[root@lvskeep ~]# ifconfig eth0 arp #开启ARP协议
再次连接正常 arp 命令(附录一)
[-]promisc
Enable or disable the promiscuous mode of the interface. If selected, all packets on the network will be received by the interface.
[-]allmulti
Enable or disable all-multicast mode. If selected, all multicast packets on the network will be received by the interface.
metric N #指定在计算数据包的转送次数时,所要加上的数目
This parameter sets the interface metric.
mtu N This parameter sets the Maximum Transfer Unit (MTU) of an interface.
#设置网络设备的MTU
dstaddr 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.
netmask addr
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.
e.g
[root@lvskeep ~]# ifconfig eth1 192.168.0.30 netmask 255.255.0.0 #子网掩码
[root@lvskeep ~]# ifconfig eth1
eth1 Link encap:Ethernet HWaddr 00:0C:29:BC:7A:1B
inet addr:192.168.0.30 Bcast:192.168.255.255 Mask:255.255.0.0
inet6 addr: fe80::20c:29ff:febc:7a1b/64 Scope:Link
UP BROADCAST RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:25 errors:0 dropped:0 overruns:0 frame:0
TX packets:54 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:6622 (6.4 KiB) TX bytes:11144 (10.8 KiB)
Interrupt:169 Base address:0x1480
注意:netmask不能大写
add addr/prefixlen
Add an IPv6 address to an interface. #设置网络设备IPv6的IP地址
del addr/prefixlen
Remove an IPv6 address from an interface.# 删除网络设备IPv6的IP地址
tunnel aa.bb.cc.dd
Create a new SIT (IPv6-in-IPv4) device, tunnelling to the given destination. #建立IPv4与IPv6之间的隧道通信地址
irq addr
Set the interrupt line used by this device. Not all devices can dynamically change their IRQ setting.# 置网络设备的IRQ
io_addr addr #设置网络设备的I/O地址
Set the start address in I/O space for this device.
mem_start addr #设置网络设备在主内存所占用的起始地址
Set the start address for shared memory used by this device. Only a few devices need this.
media type #设置网络设备的媒介类型
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.
[-]pointopoint [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.
hw class address
Set the hardware address of this interface, if the device driver supports this operation. The
multicast
Set the multicast flag on the interface. This should not normally be needed as the drivers set the flag correctly themselves.
address
The IP address to be assigned to this interface.
txqueuelen 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 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)
实例:
1.如果我们想知道主机所有网络接口的情况,请用下面的命令:
[root@lvskeep ~]# ifconfig -a
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:2974 errors:0 dropped:0 overruns:0 frame:0
TX packets:1342 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:280167 (273.6 KiB) TX bytes:214214 (209.1 KiB)
Interrupt:185 Base address:0x1400
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
inet6 addr: fe80::20c:29ff:febc:7a1b/64 Scope:Link
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
collisions:0 txqueuelen:1000
RX bytes:6622 (6.4 KiB) TX bytes:14376 (14.0 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:108 errors:0 dropped:0 overruns:0 frame:0
TX packets:108 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen: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
collisions:0 txqueuelen:0
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
2.如果我们想查看某个端口,比如我们想查看eth1 的状态,就可以用下面的方法
[root@lvskeep ~]# ifconfig eth1
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
inet6 addr: fe80::20c:29ff:febc:7a1b/64 Scope:Link
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
collisions:0 txqueuelen:1000
RX bytes:6622 (6.4 KiB) TX bytes:14376 (14.0 KiB)
Interrupt:169 Base address:0x1480
ifconfig 配置网络端口的方法
ifconfig 工具配置网络接口的方法是通过指令的参数来达到目的的
ifconfig 网络端口 IP地址 hw
broadcast 广播地址 [up/down]
3.用ifconfig 来调试 eth1网卡的地址
[root@lvskeep ~]# ifconfig eth1 down
[root@lvskeep ~]# ifconfig eth1 192.168.1.30 broadcast 192.168.1.255 netmask 255.255.255.0
[root@lvskeep ~]# ifconfig eth1 up
[root@lvskeep ~]# ifconfig eth1
eth1 Link encap:Ethernet HWaddr 00:0C:29:BC:7A:1B
inet addr:192.168.1.30 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:febc:7a1b/64 Scope:Link
UP BROADCAST RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:25 errors:0 dropped:0 overruns:0 frame:0
TX packets:91 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:6622 (6.4 KiB) TX bytes:21736 (21.2 KiB)
Interrupt:169 Base address:0x1480
说明:
第一行: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
4.设置网卡eth1的IP地址、网络掩码、广播地址,物理地址并且激活它
[root@lvskeep ~]# ifconfig eth1 192.168.0.30 hw ether 00:0C:29:BC:7A:11 broadcast 192.168.0.255 netmask 255.255.255.0 up
SIOCSIFHWADDR: Device or resource busy
先停掉然后再改,MAC地址最好别改,很容易出错,个人理解。