【网络协议-5】Linux OS 下 IP 地址怎么看

Demon.Lee 2021年11月25日 2,046次浏览

本文实践环境:
Operating System: CentOS Linux release 8.4.2105
Kernel: 4.18.0-305
Architecture: x86-64

每台联网的设备都有 IP 地址,不管是智能手机,还是笔记本电脑,都能在对应的网络设置中查看当前子网中分配的 IP 地址。比如下图是 iPhone 连接某个 Wifi 后,被自动分配的 IP 地址。

而笔者这里要说的是 Linux OS 下 IP 地址如何查看,通过梳理的过程进一步了解 IP 相关的基础知识。

在 Linux OS 下有两种方式可以获取 IP 地址,分别是 ifconfigip addr,下面我们就先来看看 ifconfig

ifconfig

废话不多说,先看结果:

[demonlee@i-mluwuwl1 ~]$ ifconfig
docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:21:4d:89:bb  txqueuelen 0  (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

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.120.74.18  netmask 255.255.255.0  broadcast 10.120.74.255
        inet6 fe80::5054:99ff:fe2f:37a0  prefixlen 64  scopeid 0x20<link>
        ether 52:54:99:2f:37:b1  txqueuelen 1000  (Ethernet)
        RX packets 5879430  bytes 2556721519 (2.3 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3170903  bytes 505027424 (481.6 MiB)
        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 88  bytes 7197 (7.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 88  bytes 7197 (7.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[demonlee@i-mluwuwl1 ~]$ 

如果具备一点点网络知识的话,从上面的输出结果就能看出:当前机器有 3 个网卡,每个网卡对应的 IP(v4) 地址分别是:172.17.0.1,10.120.74.18 和 127.0.0.1,通过排除法,基本也能猜到当前机器的 IP 是 10.120.74.18。

我就想看一个 IP 地址而已,为啥给我这么多信息,它们又是什么意思呢?我们先从这个命令的官方说明开始了解:

[demonlee@i-mluwuwl1 ~]$ man ifconfig
IFCONFIG(8) 

NAME
       ifconfig - configure a network interface

SYNOPSIS
       ifconfig [-v] [-a] [-s] [interface]
       ifconfig [-v] interface [aftype] options | address ...

NOTE
       This program is obsolete!  For replacement check ip addr and ip link.  For statistics use ip -s link.

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 '0' implies octal; otherwise, the number is interpreted as decimal). Use of hexadecimal and octal numbers is not RFC-compliant and therefore its use is discouraged.

OPTIONS
       -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.  If  your kernel  supports  alias  interfaces,  you  can  specify  them  with syntax like eth0:0 for the first alias of eth0. You can use them to assign more addresses. To delete an alias interface use ifconfig eth0:0 down.  Note: for every scope (i.e.  same  net  with  address/netmask  combination)  all aliases are deleted, if you delete the first (primary).

...
...
...

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 iptables(8) command.

       Since net-tools 1.60-4 ifconfig is printing byte counters and human readable counters with IEC 60027-2 units. So 1 KiB are 2^10 byte.  Note,  the  numbers are truncated to one decimal (which can by quite a large error if you consider 0.1 PiB is 112.589.990.684.262 bytes :)

       Interrupt  problems with Ethernet device drivers fail with EAGAIN (SIOCSIIFLAGS: Resource temporarily unavailable) it is most likely a interrupt conflict.
       See http://www.scyld.com/expert/irq-conflict.html for more information.

FILES
       /proc/net/dev
       /proc/net/if_inet6

BUGS
       Ifconfig uses the ioctl access method to get the full address information, which limits hardware  addresses  to  8  bytes.   Because  Infiniband  hardware address has 20 bytes, only the first 8 bytes are displayed correctly.  Please use ip link command from iproute2 package to display link layer informations including the hardware address.

       While appletalk DDP and IPX addresses will be displayed they cannot be altered by this command.

...
...
...

net-tools                           2008-10-03                   IFCONFIG(8)

从最前面一段的描述中,可以了解到关于 ifconfig 的这样几条信息:

1)它用来配置网络接口(网卡)信息的;
2)对应程序已经废弃,不再维护,请使用 ip addrip link 来代替;
3)命令后面不带任何参数,则展示被启用的网卡,如果使用 -a 参数(即 ifconfig -a)则展示所有网卡信息,也可以使用 ifconfig 网卡名称 来展示指定网卡的信息。

有了上面这些基础了解之后,我们知道上面输出的第 1 列为网卡信息,即有 3 个网口:docker0, eth0 和 lo。
我们以 eth0 为例,来详细了解一下每一行每一列的意义。

[demonlee@i-mluwuwl1 ~]$ ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.120.74.18  netmask 255.255.255.0  broadcast 10.120.74.255
        inet6 fe80::5054:99ff:fe2f:37b1  prefixlen 64  scopeid 0x20<link>
        ether 52:54:99:2f:37:b1  txqueuelen 1000  (Ethernet)
        RX packets 6601496  bytes 2790220080 (2.5 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3592967  bytes 572609705 (546.0 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[demonlee@i-mluwuwl1 ~]$ 

一般情况下,ifconfig 输出的几行信息可以总结为:

1)状态信息:即对应网络接口的状态,包括网络接口名称,运行状态,支持的功能等,同时还有 MTU 大小。

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500

eth0:网卡名称
flags=4163<...>:表示一堆与当前网络接口相关的状态标志,参考 stackexchange 上的一个答案,这个 4163 表示各个状态标志位代表值之和,比如 up=0x1, running=0x40, broadcast=0x2, multicast=0x1000,loopback=0x8。这些值的定义在官方源码的 if.h 头文件中可以找到。
UP:表示该网络接口已被初始化,即与该网络接口相关的内核模块已被加载,如果是 DOWN 表示未加载。
BROADCAST:表示该网络接口支持广播功能。
RUNNING:表示该接口可以(或已经在)传输数据,即运行中。
MULTICAST:表示该网络接口支持多播功能。
mtu 1500:表示最大传输单元(Maximum Transmission Unit)为 1500 字节[1],即该网络接口一次能传输的最大包大小,超过该值就得拆包。

2)IP 地址:当前网络接口的 IPv4 和 IPv6 地址信息,IPv4 还包含子网掩码,广播地址等。

inet 10.120.74.18 netmask 255.255.255.0 broadcast 10.120.74.255
inet6 fe80::5054:99ff:fe2f:37b1 prefixlen 64 scopeid 0x20<link>

inet 10.120.74.18:IPv4 地址。
netmask 255.255.255.0: 子网掩码。
broadcast 10.120.74.255:广播地址。
inet6 fe80::5054:99ff:fe2f:37b1:IPv6 地址。
prefixlen 64:IPv6 地址的前缀长度(即主机标识长度,有的文章也称为掩码长度)[2]
scopeid 0x20<link>:该 IPv6 地址的作用域为当前链路,不能跨链路通信[3]

3)MAC 地址

ether 52:54:99:2f:37:b1 txqueuelen 1000 (Ethernet)

ether 52:54:99:2f:37:b1:MAC 地址。
txqueuelen 1000:网卡设置的传送队列长度。
(Ethernet):网络接口类型为以太网,而 (Local Loopback) 表示本地回环接口。

4)接收数据包的统计情况

RX packets 6601496 bytes 2790220080 (2.5 GiB)
RX errors 0 dropped 0 overruns 0 frame 0

packets:数据包总数。
bytes:数据包的字节总数。
errors:发生错误的数据包。
dropped:被丢弃的数据包。
overruns:队列溢出的数据包,发送数据过快,队列处理不过来。
frame:发生帧错误的数据包[4]

5)发送数据包的统计情况

TX packets 3592967 bytes 572609705 (546.0 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

与上面 RX 相同的参数,这里略过。
carrier:发生载波错误的数据包[5]
collisions:发送冲突的报文数。

基于上面的释义,另外两个网卡(docker0 和 lo)的含义也就很容易理解了。这里特别提一下 lo 回环接口,它是一个虚拟网络接口,不从外界接收或发送数据,而是用于当前主机系统内部的数据传输,因此它不需要驱动程序。

关于使用 ifconfig 进行相关网络参数的设置,不是这篇文章的核心,所以这里一笔带过。通过 ifconfig -h 命令,能大致知道有哪些操作选项,再结合 man ifconfig 即可明白对应选项的功能。

[demonlee@i-mluwuwl1 ~]$ ifconfig -h
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] ...

  <HW>=Hardware Type.
  List of possible hardware types:
    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) x25 (generic X.25) infiniband (InfiniBand) 
    eui64 (Generic EUI-64) 
  <AF>=Address family. Default: inet
  List of possible address families:
    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) ash (Ash) 
    x25 (CCITT X.25) 
[demonlee@i-mluwuwl1 ~]$

上面输出的用法说明中,列出了相关硬件类型和地址族缩写的含义,可以帮助我们进一步了解底层相关的网络设备。

在前面 man ifconfig 说明中,就曾提到 ifconfig 已被废弃,需要使用 ip addrip link 来替代,下面我们就来看看 ip addr 这个命令有啥不一样。

ip addr

同样,我们先来看看这个命令的输出结果:

[demonlee@i-mluwuwl1 ~]$ ip addr 
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 52:54:99:2f:37:b1 brd ff:ff:ff:ff:ff:ff
    inet 10.120.74.18/24 brd 10.120.74.255 scope global dynamic noprefixroute eth0
       valid_lft 67987sec preferred_lft 67987sec
    inet6 fe80::5054:99ff:fe2f:37b1/64 scope link 
       valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
    link/ether 02:42:21:4d:89:bb brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever
[demonlee@i-mluwuwl1 ~]$

因为有了前面 ifconfig 命令的梳理,再看 ip addr 的输出,很多值即使不懂,也能猜出个大概了。

通过 ip addr help 可以对相关命令选项有一个初步了解:

[demonlee@i-mluwuwl1 ~]$ ip addr help
Usage: ip address {add|change|replace} IFADDR dev IFNAME [ LIFETIME ]
                                                      [ CONFFLAG-LIST ]
       ip address del IFADDR dev IFNAME [mngtmpaddr]
       ip address {save|flush} [ dev IFNAME ] [ scope SCOPE-ID ]
                            [ to PREFIX ] [ FLAG-LIST ] [ label LABEL ] [up]
       ip address [ show [ dev IFNAME ] [ scope SCOPE-ID ] [ master DEVICE ]
                         [ type TYPE ] [ to PREFIX ] [ FLAG-LIST ]
                         [ label LABEL ] [up] [ vrf NAME ] ]
       ip address {showdump|restore}
IFADDR := PREFIX | ADDR peer PREFIX
          [ broadcast ADDR ] [ anycast ADDR ]
          [ label IFNAME ] [ scope SCOPE-ID ] [ metric METRIC ]
SCOPE-ID := [ host | link | global | NUMBER ]
FLAG-LIST := [ FLAG-LIST ] FLAG
FLAG  := [ permanent | dynamic | secondary | primary |
           [-]tentative | [-]deprecated | [-]dadfailed | temporary |
           CONFFLAG-LIST ]
CONFFLAG-LIST := [ CONFFLAG-LIST ] CONFFLAG
CONFFLAG  := [ home | nodad | mngtmpaddr | noprefixroute | autojoin ]
LIFETIME := [ valid_lft LFT ] [ preferred_lft LFT ]
LFT := forever | SECONDS
TYPE := { vlan | veth | vcan | vxcan | dummy | ifb | macvlan | macvtap |
          bridge | bond | ipoib | ip6tnl | ipip | sit | vxlan | lowpan |
          gre | gretap | erspan | ip6gre | ip6gretap | ip6erspan | vti |
          nlmon | can | bond_slave | ipvlan | geneve | bridge_slave |
          hsr | macsec | netdevsim }
[demonlee@i-mluwuwl1 ~]$

从输出结果来看,我们输入的 ip addr 其实是 ip address 的缩写,两者输出的结果是一样的,如果想再偷懒一点,即 ip a

接着,我们通过 man ip-address 看看 Manual 中的描述:

[demonlee@i-mluwuwl1 ~]$ man ip-address
IP-ADDRESS(8)                    Linux                                IP-ADDRESS(8)

NAME
       ip-address - protocol address management

SYNOPSIS
       ip [ OPTIONS ] address  { COMMAND | help }

       ip address { add | change | replace } IFADDR dev IFNAME [ LIFETIME ] [ CONFFLAG-LIST ]

       ip address del IFADDR dev IFNAME [ mngtmpaddr ]

       ip address { save | flush } [ dev IFNAME ] [ scope SCOPE-ID ] [ metric METRIC ] [ to PREFIX ] [ FLAG-LIST ] [ label PATTERN ] [ up ]

       ip address [ show [ dev IFNAME ] [ scope SCOPE-ID ] [ to PREFIX ] [ FLAG-LIST ] [ label PATTERN ] [ master DEVICE ] [ type TYPE ] [ vrf NAME ] [ up ] ]

       ip address { showdump | restore }

       ...
       ...
       ...

DESCRIPTION
       The address is a protocol (IPv4 or IPv6) address attached to a network device. Each device must have at least one address to use the corresponding protocol. It is possible to have
       several different addresses attached to one device. These addresses are not discriminated, so that the term alias is not quite appropriate for them and we do not use it in this docu‐
       ment.

       The ip address command displays addresses and their properties, adds new addresses and deletes old ones.

   ip address add - add new protocol address.
       dev IFNAME
              the name of the device to add the address to.

       local ADDRESS (default)
              the address of the interface. The format of the address depends on the protocol. It is a dotted quad for IP and a sequence of hexadecimal halfwords separated by colons for
              IPv6. The ADDRESS may be followed by a slash and a decimal number which encodes the network prefix length.
       peer ADDRESS
              the address of the remote endpoint for pointopoint interfaces.  Again, the ADDRESS may be followed by a slash and a decimal number, encoding the network prefix length. If a peer
              address is specified, the local address cannot have a prefix length. The network prefix is associated with the peer rather than with the local address.

       broadcast ADDRESS
              the broadcast address on the interface.

              It is possible to use the special symbols '+' and '-' instead of the broadcast address. In this case, the broadcast address is derived by setting/resetting the host bits of the
              interface prefix.

       label LABEL
              Each address may be tagged with a label string.  In order to preserve compatibility with Linux-2.0 net aliases, this string must coincide with the name of the device or must be
              prefixed with the device name followed by colon.  The maximum allowed total length of label is 15 characters.

       scope SCOPE_VALUE
              the scope of the area where this address is valid.  The available scopes are listed in file /etc/iproute2/rt_scopes.  Predefined scope values are:

                      global - the address is globally valid.

                      site - (IPv6 only, deprecated) the address is site local, i.e. it is valid inside this site.

                      link - the address is link local, i.e. it is valid only on this device.

                      host - the address is valid only inside this host.

       metric NUMBER
              priority of prefix route associated with address.

       valid_lft LFT
              the valid lifetime of this address; see section 5.5.4 of RFC 4862. When it expires, the address is removed by the kernel.  Defaults to forever.

       preferred_lft LFT
              the preferred lifetime of this address; see section 5.5.4 of RFC 4862. When it expires, the address is no longer used for new outgoing connections. Defaults to forever.

       home   (IPv6 only) designates this address the "home address" as defined in RFC 6275.

       mngtmpaddr
              (IPv6 only) make the kernel manage temporary addresses created from this one as template on behalf of Privacy Extensions (RFC3041). For this to become active, the use_tempaddr
              sysctl setting has to be set to a value greater than zero.  The given address needs to have a prefix length of 64. This flag allows to use privacy extensions in a manually con‐
              figured network, just like if stateless auto-configuration was active.

       nodad  (IPv6 only) do not perform Duplicate Address Detection (RFC 4862) when adding this address.

       optimistic
              (IPv6 only) When performing Duplicate Address Detection, use the RFC 4429 optimistic variant.

       noprefixroute
              Do not automatically create a route for the network prefix of the added address, and don't search for one to delete when removing the address. Changing an address to add this
              flag will remove the automatically added prefix route, changing it to remove this flag will create the prefix route automatically.

       autojoin
              Joining multicast groups on Ethernet level via ip maddr command does not work if connected to an Ethernet switch that does IGMP snooping since the switch would not replicate
              multicast packets on ports that did not have IGMP reports for the multicast addresses.

   ip address delete - delete protocol address
       Arguments: coincide with the arguments of ip addr add.  The device name is a required argument. The rest are optional.  If no arguments are given, the first address is deleted.

   ip address show - look at protocol addresses
       dev IFNAME (default)
              name of device.

       scope SCOPE_VAL
              only list addresses with this scope.

    ...
    ...
    ...
    
EXAMPLES
       ip address show
           Shows IPv4 and IPv6 addresses assigned to all network interfaces. The 'show' subcommand can be omitted.

       ip address show up
           Same as above except that only addresses assigned to active network interfaces are shown.

       ip address show dev eth0
           Shows IPv4 and IPv6 addresses assigned to network interface eth0.

       ip address add 2001:0db8:85a3::0370:7334/64 dev eth1
           Adds an IPv6 address to network interface eth1.

       ip address delete 2001:0db8:85a3::0370:7334/64 dev eth1
           Delete the IPv6 address added above.

       ip address flush dev eth4 scope global
           Removes all global IPv4 and IPv6 addresses from device eth4. Without 'scope global' it would remove all addresses including IPv6 link-local ones.

SEE ALSO
       ip(8)

...
...
...

从 Manual 中可以了解到以下信息:
1)ip address 用来进行协议地址管理,这里的地址指的是连接到网络设备上的协议(IPv4 或 IPv6)地址。
2)ip address 可以显示地址及其属性信息,也可以增加或删除地址。
3)查看协议地址一般使用 ip address show ,这里的 show 子命令可以忽略。但查看某个具体设备时, show 子命令不可以忽略,可以用 ip address show <网卡名称>ip address show dev <网卡名称>

好,有了前面的铺垫,接下来,我们还是以 eth0 网卡为例来详细看看输出的内容。

[demonlee@i-mluwuwl1 ~]$ ip address show dev eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 52:54:99:2f:37:b1 brd ff:ff:ff:ff:ff:ff
    inet 10.120.74.18/24 brd 10.120.74.255 scope global dynamic noprefixroute eth0
       valid_lft 29664sec preferred_lft 29664sec
    inet6 fe80::5054:99ff:fe2f:37b1/64 scope link 
       valid_lft forever preferred_lft forever
[demonlee@i-mluwuwl1 ~]$

输出结果的第 1 列是网络接口的序号,第 2 列才是网络接口的名称。与 ifconfig 雷同,我们也可以将输出的信息划分为以下几类:

1)状态信息

eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000

<BROADCAST,MULTICAST,UP,LOWER_UP>:这里的状态标志,除了 LOWER_UP 之外,其他的在前文都已介绍过。而 LOWER_UP 是一个物理层链接标志,表示以太网电缆被插入,设备连接到了网络上。LOWER_UPUP 不同,后者还要求网络接口被启用[6]
mtu 1500:这个前面也已介绍,表示该网卡最大传输单元为 1500 字节。
qdisc fq_codel:其中的 qdisc 表示 queueing discipline,即流量控制的排队规则,内核通过某个网络接口发送数据时,需要按照该接口对应的排队规则将数据包加入到队列中。qdisc 值如果是 pfifo_fast, 可以简单理解为先进先出,而如果是 fq_codel,则其含义为 Fair Queuing (FQ) with Controlled Delay (CoDel),即公平队列控制延迟规则[7]
state UP:网络接口已启动。
group default:网络接口组为 default。
qlen 1000:传输队列长度(queue length)为 1000 个数据包。

2)MAC 地址

link/ether 52:54:99:2f:37:b1 brd ff:ff:ff:ff:ff:ff

link/ether 52:54:99:2f:37:b1:表示数据链路层的协议为以太网,对应 MAC 地址为 52:54:99:2f:37:b1
brd ff:ff:ff:ff:ff:ff:表示广播地址为 ff:ff:ff:ff:ff:ff, 它是一个特殊的广播地址,发往这个地址的以太网帧将会被送到以太网的每一台主机。如果是交换机收到目的地址为 ff:ff:ff:ff:ff:ff 的帧,则会将它广播到所有端口[8]

Wireshark 对 ARP 协议抓包

3)IP 地址

inet 10.120.74.18/24 brd 10.120.74.255 scope global dynamic noprefixroute eth0
     valid_lft 29664sec preferred_lft 29664sec

inet 10.120.74.18/24 brd 10.120.74.255 scope global:这些在前面也已经介绍过,不再赘述。
dynamic noprefixroute eth0:表示 eth0 网卡的地址是动态分配的,而 noprefixrouteman ip-address 中的解释是不会动态添加前缀路由。
preferred_lft 29664sec:在 man ip-address 中也有说明,表示该 IP 地址的首选生存周期,超时后 IP 地址就变成了 deprecated[9],默认值为 forever。
valid_lft 29664sec:表示该 IP 地址的有效时长(大于等于 preferred_lft),过期后 IP 地址将会变成 invalid[10],默认值为 forever。

inet6 fe80::5054:99ff:fe2f:37b1/64 scope link
     valid_lft forever preferred_lft forever

IPv6 部分的相关参数在前文也介绍过,这里也直接略过。

思考

前面通过两种方式查看 IP 地址,并对相关输出参数的含义进行梳理,将有助于我们进一步理解 IP 地址相关的底层知识,为后续的网络知识体系学习打下基础。

很多东西,最怕被问,一问就发现自己好像这也没完全搞懂,那也不是很清楚。学习就是这样。

从开头的 man ifconfig 输出中也看到,ifconfig 已废弃,那么问题来了:ifconfigip addr 背后到底有哪些东西,又有哪些联系呢?

别怕,下一回我们接着聊。

参考资料


  1. 不同的数据链路对应的 MTU 是不同的。一般情况下,以太网中为 1500 字节,FDDI 中为 4352 字节,ATM 中为 9180 字节。 ↩︎

  2. IPv6 长度一共 128 比特,即 16 字节,一般分为三部分:全局路由前缀,子网 ID,接口 ID,后续有机会再进行更细致的梳理。 ↩︎

  3. 其他作用域还有 Host,Global,Site等,https://serverfault.com/questions/63014/ip-address-scope-parameter。另,后面的 man ip-address 的描述文档中也有说明。 ↩︎

  4. https://serverfault.com/questions/185331/exact-meaning-of-rx-errors-and-frame-in-ifconfig-output ↩︎

  5. https://serverfault.com/questions/325800/what-is-and-what-problems-result-in-a-carrier-error ↩︎

  6. https://stackoverflow.com/questions/36715664/using-ip-what-does-lower-up-mean/43665047 ↩︎

  7. https://man7.org/linux/man-pages/man8/tc-fq_codel.8.html ↩︎

  8. 一般在 ARP 协议中会使用到该广播地址,即已知目标 IP 地址,求目标 MAC 地址。在一个局域网链路内,只有 IP 和 port 还不能将数据送到对应的主机,还得 MAC 地址,因为主机收到数据包需要先比较 MAC 地址是不是发给自己的,如果不是,数据将会被丢弃,https://fasionchan.com/network/arp/arp/ ↩︎

  9. 参考 RFC 4862,里面对相关概念有详细解释,https://www.rfc-editor.org/rfc/pdfrfc/rfc4862.txt.pdf ↩︎

  10. 一般使用 DHCP 协议动态分配的地址,相当于是租用,当租期过去一半的时候,客户端主机会向 DHCP Server 发送 DHCP request 消息,进行续约,https://time.geekbang.org/column/article/8015 ↩︎