Linux之(21)ifconfig命令总结
Author:OnceDay Date:2023年2月5日
漫漫长路,有人对你微笑过嘛…
参考文档:
- ifconfig 详解_博雅勇士的博客-CSDN博客_ifconfig
- Linux ifconfig命令 | 菜鸟教程 (runoob.com)
- 查看网卡信息:ifconfig命令及详细介绍_-纸短情长的博客-CSDN博客_ifconfig
- ifconfig(8) - Linux manual page (man7.org)
- ifconfig命令 - 腾讯云开发者社区-腾讯云 (tencent.com)
1.概述
Linux ifconfig命令用于显示或设置网络设备。
ifconfig
可设置网络设备的状态,或是显示目前的设置。
默认的ifconfig
命令直接输出当前处于UP
状态的所有接口信息:
onceday@ubuntu:~$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.30.142.10 netmask 255.255.240.0 broadcast 172.30.143.255
inet6 fe80::215:5dff:fe41:2615 prefixlen 64 scopeid 0x20<link>
ether 00:15:5d:41:26:15 txqueuelen 1000 (Ethernet)
RX packets 218 bytes 41518 (41.5 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 16 bytes 1216 (1.2 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
首先看看,这些信息代表的含义。
1.2 网络接口的名字
eth0
是以太网口的常见名称,此外还有一种名称是ep0s3
,这里p0s3
有特殊的含义,代表PCI接口的物理位置(0,3),其中横坐标代表bus,纵坐标代表slot。lo
就是本地环回口的常见的名字。接口的名称只是一种约定俗称的东西,一般统一命名即可。
1.3 接口标志状态
flags是该接口的标志状态,后面ifconfig
帮助解析具体的位含义。在Linux内核源码include/uapi/linux/if.h
里可查找相关定义。如下所示:
标识名称 | 描述 |
---|---|
IFF_UP(1<<0) | interface is up.代表网口接口是开启状态。 |
IFF_BROADCAST(1<<1) | broadcast address valid. |
IFF_DEBUG(1<<2) | turn on debugging. |
IFF_LOOPBACK(1<<3) | is a loopback net. |
IFF_POINTOPOINT(1<<4) | interface is has p-p link. |
IFF_NOTRAILERS(1<<5) | avoid use of trailers. |
IFF_RUNNING(1<<6) | interface RFC2863 OPER_UP.(运输层已连接,如网口网线被插上) |
IFF_NOARP(1<<7) | no ARP protocol. |
IFF_PROMISC(1<<8) | receive all packets.(混杂模式,即监听该网络所有的流量包) |
IFF_ALLMULTI(1<<9) | receive all multicast packets. |
IFF_MASTER(1<<10) | master of a load balancer. |
IFF_SLAVE(1<<11) | slave of a load balancer. |
IFF_MULTICAST(1<<12) | Supports multicast. |
IFF_PORTSEL(1<<13) | can set media type. |
IFF_AUTOMEDIA(1<<14) | auto media select active. |
IFF_DYNAMIC(1<<15) | dialup device with changing addresses. |
IFF_LOWER_UP(1<<16) | driver signals L1 up. |
IFF_DORMANT(1<<17) | driver signals dormant. |
IFF_ECHO(1<<18) | echo sent packets. |
1.4 网络接口信息
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.30.142.10 netmask 255.255.240.0 broadcast 172.30.143.255
inet6 fe80::215:5dff:fe41:2615 prefixlen 64 scopeid 0x20<link>
ether 00:15:5d:41:26:15 txqueuelen 1000 (Ethernet)
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
“Maximum Transmission Unit,最大传输单元,指的是数据链路层的最大payload,由硬件网卡设置MTU,是一个硬性限制”。
可参考文档:
一般默认填写1500即可,这个和整个链路层的最大传送单元有关,合适即可。对于环路口,其不需要在实际物理物理链路上传输,因此就直接指定最大值了,这样传送包的效率会更高。
inet
是IPv4地址,netmask
是网络掩码,用于划分子网的,找出同一局域网的成员,broadcast
是本局域网上的广播地址(子网号全为1)。
inet6
是IPv6地址,prefixlen
是网络前缀,和IPv4的网络掩码类似,用与划分子网,找出同一局域网的成员 。scopeid
是网络范围,IPv6地址对资源做了划分,fe80
开头的是链路本地地址(局域网地址),不允许对外通信,因此scopeid
便可以体现出这点。
地址类型 | 地址前缀(二进制) | IPv6前缀标识 |
---|---|---|
(单播地址)未指定地址 | 00…0(128 bits) | ::/128 |
(单播地址)环回地址 | 00…1(128 bits) | ::1/128 |
(单播地址)链路本地地址 | 1111111010 | FE80::/10 |
(单播地址)唯一本地地址 | 1111 110 | FC00::/7(包括FD00::/8和不常用的FC00::/8) |
(单播地址)站点本地地址 (已弃用,被唯一本地地址代替) | 1111111011 | FEC0::/10 |
(单播地址)全局单播地址 | 其他形式 | - |
组播地址 | 11111111 | FF00::/8 |
任播地址 | 从单播地址空间中进行分配, 使用单播地址的格式 |
上面是IPv6的地址类型划分,根据此规则,scopeid可有以下形式的取值规则:
#define IPV6_ADDR_ANY 0x0000U
#define IPV6_ADDR_UNICAST 0x0001U
#define IPV6_ADDR_MULTICAST 0x0002U
#define IPV6_ADDR_LOOPBACK 0x0010U
#define IPV6_ADDR_LINKLOCAL 0x0020U
#define IPV6_ADDR_SITELOCAL 0x0040U
#define IPV6_ADDR_COMPATv4 0x0080U
#define IPV6_ADDR_SCOPE_MASK 0x00f0U
0x00
,global地址,这类是可跨本地链路传播的。0x10
,本地环回地址,不可对外传播,只能本主机上使用。0x20
,本地链路地址,可以在本地局域网上传播。0x40
,本地站点地址,已废弃。
对于物理或者虚拟的网络接口,一般都有相应的驱动程序,现在常用的是ether
以太网类型,以太网会有一个mac地址,如下:
ether 00:15:5d:41:26:15 txqueuelen 1000 (Ethernet)
txqueuelen
是网卡传送队列的长度,表示最大缓存包的数量,超过这个数量的包会被丢弃。
对于本地环路口,其各项值如下:
loop txqueuelen 1000 (Local Loopback)
loop
就代表这是本地环回口。
1.5 网络流量信息
RX packets 218 bytes 41518 (41.5 KB)
RX errors 0 dropped 0 overruns 0 frame 0
- RX packets :接受到的总包数
- RX bytes :接受到的总字节数
- RX errors :接收时,产生错误的数据包数
- RX dropped :接收时,丢弃的数据包数
- RX overruns :接收时,由于速度过快而丢失的数据包数
- RX frame (框架) :接收时,发生frame错误而丢失的数据包数
TX packets 16 bytes 1216 (1.2 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
- TX packets :发送的总包数
- TX bytes :发送的总字节数
- TX errors :发送时,产生错误的数据包数
- TX dropped :发送时,丢弃的数据包数
- TX overruns :发送时,由于速度过快而丢失的数据包数
- TX carrier :发送时, 发生carrier错误而丢失的数据包数(运输工具)
- TX collisions :发送时, 冲突信息包的数目
1.6 硬件类型
loop (Local Loopback)
、slip (Serial Line IP)
、cslip (VJ Serial Line IP)
、slip6 (6-bit Serial Line IP)
、cslip6 (VJ 6-bit Serial Line IP)
、adaptive (Adaptive Serial Line IP)
、ash (Ash)
、ether (Ethernet)
、ax25 (AMPR AX.25)
、netrom (AMPR NET/ROM)
、rose (AMPR ROSE)
、tunnel (IPIP Tunnel)
、ppp (Point-to-Point Protocol)
、hdlc ((Cisco)-HDLC)
、lapb (LAPB)
、 arcnet (ARCnet)
、dlci (Frame Relay DLCI)
、frad (Frame Relay Access Device)
、sit (IPv6-in-IPv4)
、fddi (Fiber Distributed Data Interface)
、hippi (HIPPI)
、irda (IrLAP)
、ec (Econet)
、x25 (generic X.25)
、eui64 (Generic EUI-64)
1.7 地址族
unix (UNIX Domain)
、inet (DARPA Internet)
、inet6 (IPv6)
、ax25 (AMPR AX.25)
、netrom (AMPR NET/ROM)
、rose (AMPR ROSE)
、ipx (Novell IPX)
、ddp (Appletalk DDP)
、ec (Econet)
、ash (Ash)
、x25 (CCITT X.25)
1.8 内核文件系统
在/proc/net/dev
可直接查看各网络设备的统计情况。
在/proc/net/if_inet6
可查看IPv6的设置信息。
2. 命令具体信息
2.1 命令参数
onceday->~:# ifconfig --help
Usage:
ifconfig [-a] [-v] [-s] <interface> [[<AF>] <address>]
[add <address>[/<prefixlen>]]
[del <address>[/<prefixlen>]]
[[-]broadcast [<address>]] [[-]pointopoint [<address>]]
[netmask <address>] [dstaddr <address>] [tunnel <address>]
[outfill <NN>] [keepalive <NN>]
[hw <HW> <address>] [mtu <NN>]
[[-]trailers] [[-]arp] [[-]allmulti]
[multicast] [[-]promisc]
[mem_start <NN>] [io_addr <NN>] [irq <NN>] [media <type>]
[txqueuelen <NN>]
[[-]dynamic]
[up|down] ...
下面是常见选项的解释:
选项 | 描述 |
---|---|
-a | display all interfaces which are currently available, even if down |
-s | display a short list (like netstat -i) |
-v | be more verbose for some error conditions |
interface | The name of the interface. This is usually a driver name followed by a unit number, for example eth0 for the first Ethernet interface. |
[AF] address | The IP address to be assigned to this interface. |
add address[/prefixlen][add [/]] | Add an IPv6 address to an interface. |
[del <address>[/<prefixlen>]] | Remove an IPv6 address from an interface. |
[[-]broadcast [<address>]] | If the address argument is given, set the protocol broadcast address for this interface. Otherwise, set (or clear) the IFF_BROADCAST flag for the interface. |
[[-]pointopoint [<address>]] | 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. |
[netmask <address>] | 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. |
[dstaddr <address>] | Set the remote IP address for a point-to-point link (such as PPP). This keyword is now obsolete; use the pointopoint keyword instead. |
[outfill <NN>] | (暂不清楚) |
[keepalive <NN>] | (用于保活) |
[hw <HW> <address>] | Set the hardware address of this interface, if the device driver supports this operation. |
[mtu <NN>] | This parameter sets the Maximum Transfer Unit (MTU) of an interface. |
[[-]trailers] | (暂不清楚) |
[[-]arp] | Enable or disable the use of the ARP protocol on this interface. |
[[-]allmulti] | Enable or disable all-multicast mode. If selected, all multicast packets on the network will be received by the interface. |
[multicast] | Set the multicast flag on the interface. This should not normally be needed as the drivers set the flag correctly themselves. |
[[-]promisc] | Enable or disable the promiscuous mode of the interface. If selected, all packets on the network will be received by the interface. |
[mem_start <NN>] | Set the start address for shared memory used by this device. Only a few devices need this. |
[io_addr <NN>] | Set the start address in I/O space for this device. |
[irq <NN>] | Set the interrupt line used by this device. Not all devices can dynamically change their IRQ setting. |
[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. |
[txqueuelen <NN>] | 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 disturbing interactive traffic like telnet too much. |
[[-]dynamic] | (暂不清楚) |
[up] | This flag causes the interface to be activated. It is implicitly specified if an address is assigned to the interface; you can suppress this behavior when using an alias interface by appending an - to the alias (e.g. eth0:0-). It is also suppressed when using the IPv4 0.0.0.0 address as the kernel will use this to implicitly delete alias interfaces. |
[down] | This flag causes the driver for this interface to be shut down. |
name <newname> | Change the name of this interface to newname. The interface must be shut down first. |
3.命令实际参数
3.1 显示网卡信息
显示所有网卡信息:
onceday@ubuntu:~$ ifconfig -a
bond0: flags=5122<BROADCAST,MASTER,MULTICAST> mtu 1500
ether be:a9:5c:8e:bd:43 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
以较短的形式显示网卡信息:
onceday@ubuntu:~$ ifconfig -s -a
Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
bond0 1500 0 0 0 0 0 0 0 0 BMm
dummy0 1500 0 0 0 0 0 0 0 0 BO
eth0 1500 1346 0 0 0 35 0 0 0 BMRU
eth0:0 1500 - no statistics available - BMRU
eth0:2 1500 - no statistics available - BMRU
eth0:0:1 1500 - no statistics available - BMRU
lo 65536 4 0 0 0 4 0 0 0 LRU
sit0 1480 0 0 0 0 0 0 0 0 O
tunl0 1480 0 0 0 0 0 0 0 0 O
显示指定网卡的信息:
onceday@ubuntu:~$ ifconfig -s eth0
Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0 1500 1362 0 0 0 35 0 0 0 BMRU
3.2 启动和关闭网卡
启动和关闭网卡很简单:
ifconfig eth0 down
ifconfig eth0 up
3.3 修改网卡IP信息
此外,可以给网卡添加IP地址,如下:
onceday@ubuntu:~$ sudo ifconfig eth0 192.168.1.56 netmask 255.255.255.0 broadcast 192.168.1.255
onceday@ubuntu:~$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.56 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::215:5dff:fe41:267c prefixlen 64 scopeid 0x20<link>
ether 00:15:5d:41:26:7c txqueuelen 1000 (Ethernet)
RX packets 1750 bytes 206733 (206.7 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 35 bytes 3709 (3.7 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
上面同时指定了网络掩码和广播地址,这些是可选的。
还可以给网卡添加多个IP地址(需要属于同一子网范围):
onceday@ubuntu:~$ sudo ifconfig eth0 add 192.168.1.59 netmask 255.255.0.0
onceday@ubuntu:~$ sudo ifconfig eth0 add 192.168.1.60 netmask 255.255.0.0
onceday@ubuntu:~$ sudo ifconfig eth0 add 192.168.1.61 netmask 255.255.0.0
onceday@ubuntu:~$ sudo ifconfig eth0 add 192.168.1.62
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.56 netmask 255.255.0.0 broadcast 192.168.255.255
inet6 fe80::215:5dff:fe41:267c prefixlen 64 scopeid 0x20<link>
ether 00:15:5d:41:26:7c txqueuelen 1000 (Ethernet)
RX packets 2059 bytes 242492 (242.4 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 36 bytes 3779 (3.7 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.60 netmask 255.255.0.0 broadcast 192.168.255.255
ether 00:15:5d:41:26:7c txqueuelen 1000 (Ethernet)
eth0:2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.61 netmask 255.255.0.0 broadcast 192.168.255.255
ether 00:15:5d:41:26:7c txqueuelen 1000 (Ethernet)
eth0:3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.62 netmask 255.255.0.0 broadcast 192.168.255.255
ether 00:15:5d:41:26:7c txqueuelen 1000 (Ethernet)
如果添加另外一个不同范围的地址,那么会清除之前的地址。
可以使用如下形式删除指定网卡后面的IP地址:
ifconfig eth0 del 192.168.1.90
ifconfig eth0 down eth0:0
eth0:0是eth0上准备删除IP地址的别名。
下面是为网卡配置IPv6地址:
onceday@ubuntu:~$ sudo ifconfig eth0:0 add 2001::10/64
onceday@ubuntu:~$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.56 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::215:5dff:fe41:267c prefixlen 64 scopeid 0x20<link>
inet6 2001::10 prefixlen 64 scopeid 0x0<global>
ether 00:15:5d:41:26:7c txqueuelen 1000 (Ethernet)
RX packets 2551 bytes 299276 (299.2 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 46 bytes 4791 (4.7 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
使用下面命令即可删除IPv6地址:
ifconfig eth0:0 del 2001::10/64
3.4 修改网卡属性(MAC和MTU等)
修改网卡的MAC吗,需要首选关闭网卡, 然后设置MAC地址,再开启网卡:
onceday@ubuntu:~$ sudo ifconfig eth0 down
onceday@ubuntu:~$ sudo ifconfig eth0 hw ether 00:11:12:13:14:15
onceday@ubuntu:~$ sudo ifconfig eth0 up
onceday@ubuntu:~$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.30.134.204 netmask 255.255.240.0 broadcast 172.30.143.255
inet6 fe80::211:12ff:fe13:1415 prefixlen 64 scopeid 0x20<link>
ether 00:11:12:13:14:15 txqueuelen 1000 (Ethernet)
RX packets 52 bytes 6244 (6.2 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 16 bytes 1352 (1.3 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
开启和关闭ARP的设置如下:
ifconfig eth0 arp #开启ARP协议
ifconfig eth0 -arp #关闭ARP协议
关闭ARP后网络接口的标识如下(有明显的NOARP标识):
eth0: flags=4291<UP,BROADCAST,RUNNING,NOARP,MULTICAST> mtu 1500
设置MTU值为1800,如下:
ifconfig eth0 mtu 1800
设置网卡工作在混杂模式,如下:
ifconfig eth0 promisc #开启混杂模式,接收所有包
ifconfig eth0 -promisc #关闭混杂模式,只接受发给自己的包。
是否接受网络上的多播消息:
ifconfig eth0 allmulti #开启接收所有多播报文
ifconfig eth0 -allmulti #关闭该模式
下面是上述标识设立之后的ifconfig输出内容:
eth0: flags=5059<UP,BROADCAST,RUNNING,NOARP,PROMISC,ALLMULTI,MULTICAST> mtu 1800
关闭更多flags标识定义,可以参考本文章1.3节信息。
此外还有点对点链路和IPv6 in IPv4设置,这些可以仿照上面例子来操作。