Linux ifconfig 命令

Linux命令是对Linux系统进行管理的命令。对于Linux系统来说,无论是中央处理器、内存、磁盘驱动器、键盘、鼠标,还是用户等都是文件,Linux系统管理的命令是它正常运行的核心,与之前的DOS命令类似。linux命令在系统中有两种类型:内置Shell命令和Linux命令。本文主要介绍Linux ifconfig 命令。

 1、命令简介

ifconfig:用于配置和显示网络接口的信息,包括 IP 地址、MAC 地址、子网掩码等。不过在现代的 Linux 系统中,ifconfig 命令已经被较新的 ip 命令所取代。

2、命令语法

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

3、命令描述

Ifconfig命令用于配置内核驻留网络接口。它在启动时按需设置接口。之后,通常需要对网络参数进行查询和管理。在调试期间或需要进行系统调优时也需要它。

如果没有指定参数,ifconfig命令显示当前活动接口的状态。如果只指定了一个接口参数,它只显示指定接口的状态;如果使用-a选项,则显示所有接口的状态,包括down状态。否则,配置接口。

4、命令选项

参数

描述

-a

显示所有当前可用的接口,即使是关闭的状态

-s

显示简短的列表(类似于 netstat -i)

-v

在某些错误情况下显示更多详细信息

interface

接口的名称。

通常是驱动程序名称后跟一个单位号,

例如第一个以太网接口为 eth0。

如果内核支持别名接口,

可以使用 eth0:0 来指定它们,

其中 eth0 是 eth0 的第一个别名。

可以使用别名来分配第二个地址。

要删除别名接口,

请使用 ifconfig eth0:0 down 命令。

注意:

对于每个范围

(即具有相同地址/子网掩码组合的网络),

如果删除第一个(主要)别名,

则删除所有别名。

up

此标志将激活接口。

如果为接口分配地址,

则会隐式指定此标志。

down

此标志将关闭该接口的驱动程序。

[-]arp

启用或禁用此接口上的 ARP 协议。

[-]promisc

启用或禁用接口的混杂模式。

如果选择此选项,

接口将接收网络上的所有数据包。

[-]allmulti

启用或禁用全多播模式。

如果选择此选项,

接口将接收网络上的所有多播数据包。

metric N

此参数设置接口度量值。

mtu N

此参数设置接口的最大传输单元(MTU)。

dstaddr addr

为点对点链接(例如 PPP)

设置远程 IP 地址。此关键字现在已过时;请改用 pointopoint 关键字。

netmask addr

为此接口设置 IP 网络掩码。

默认值为根据接口 IP 地址派生的

常规 A、B 或 C 类网络掩码,

但可以设置为任何值。

add addr/prefixlen

为接口添加 IPv6 地址。

del addr/prefixlen

从接口删除 IPv6 地址。

tunnel aa.bb.cc.dd

创建新的 SIT(IPv6-in-IPv4)设备,

隧道传输到给定目标。

irq addr

设置此设备使用的中断线。

并非所有设备都可以动态更改其 IRQ 设置。

io_addr addr

设置此设备在 I/O 空间中的起始地址。

mem_start addr

设置此设备使用的共享内存的起始地址。

只有少数设备需要此设置。

address

要分配给此接口的 IP 地址。

5、使用示例

1)Ifconfig命令不带参数显示所有激活接口的详细信息。同时也可以查看活动接口的ip地址。可能存在活动的接口,但它们可能没有被分配IP地址

$ ifconfig
eth0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
      ether 20:25:64:08:6a:a4  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
      device interrupt 20  memory 0xf7c00000-f7c20000  

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 5426  bytes 509075 (509.0 KB)
      RX errors 0  dropped 0  overruns 0  frame 0
      TX packets 5426  bytes 509075 (509.0 KB)
      TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
      inet 192.168.31.222  netmask 255.255.255.0  broadcast 192.168.31.255
      inet6 2409:4053:2105:527d:db24:166e:345d:d48a prefixlen 64  scopeid 0x0<global>
      inet6 fe80::fa1c:1e5f:feac:5280  prefixlen 64  scopeid 0x20<link>
      inet6 2409:4053:2105:527d:c113:9da0:ddcc:2235  prefixlen 64  scopeid 0x0<global>
      ether a4:2b:8c:5f:19:0b  txqueuelen 1000  (Ethernet)
      RX packets 15381  bytes 7488890 (7.4 MB)
      RX errors 0  dropped 0  overruns 0  frame 0
      TX packets 16671  bytes 2821518 (2.8 MB)
      TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

2)使用-a选项显示服务器上的所有网络接口,即使网络接口已关闭

$ ifconfig -a
eth0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
      ether 20:25:64:08:6a:a4  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
      device interrupt 20  memory 0xf7c00000-f7c20000  

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 5426  bytes 509075 (509.0 KB)
      RX errors 0  dropped 0  overruns 0  frame 0
      TX packets 5426  bytes 509075 (509.0 KB)
      TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        
sit0     Link encap:IPv6-in-IPv4
         NOARP  MTU:1500  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)

wlan0:   flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
         inet 192.168.31.222  netmask 255.255.255.0  broadcast 192.168.31.255
         inet6 2409:4053:2105:527d:db24:166e:345d:d48a  prefixlen 64  scopeid 0x0<global>
         inet6 fe80::fa1c:1e5f:feac:5280  prefixlen 64  scopeid 0x20<link>
         inet6 2409:4053:2105:527d:c113:9da0:ddcc:2235  prefixlen 64  scopeid 0x0<global>
         ether a4:2b:8c:5f:19:0b  txqueuelen 1000  (Ethernet)
         RX packets 15381  bytes 7488890 (7.4 MB)
         RX errors 0  dropped 0  overruns 0  frame 0
         TX packets 16671  bytes 2821518 (2.8 MB)
         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

3)使用-s选项显示一个短列表,而不是所有的细节。

$ ifconfig -s
Iface      MTU    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0      1500        0      0      0 0             0      0      0      0 BMU
lo       65536     9696      0      0 0          9696      0      0      0 LRU
wlan0     1500    32426      0      0 0         35073      0      0      0 BMRU

4)Ifconfig命令以接口名称(wlan0)作为参数显示接口配置的详细信息。

$ ifconfig wlan0
      wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
      inet 192.168.31.222  netmask 255.255.255.0  broadcast 192.168.31.255
      inet6 2409:4053:2105:527d:db24:166e:345d:d48a  prefixlen 64  scopeid 0x0<global>
      inet6 fe80::fa1c:1e5f:feac:5280  prefixlen 64  scopeid 0x20<link>
      inet6 2409:4053:2105:527d:c113:9da0:ddcc:2235  prefixlen 64  scopeid 0x0<global>
      ether a4:2b:8c:5f:19:0b  txqueuelen 1000  (Ethernet)
      RX packets 39246  bytes 16139815 (16.1 MB)
      RX errors 0  dropped 0  overruns 0  frame 0
      TX packets 42288  bytes 8517460 (8.5 MB)
      TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

5)Ifconfig命令和down选项可用于禁用网络接口。以root权限使用此命令。

$ sudo ifconfig  enp0s25 down
$ ifconfig
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:65536  Metric:1
         RX packets:187930 errors:0 dropped:0 overruns:0 frame:0
         TX packets:187930 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:1000 
         RX bytes:68712556 (68.7 MB)  TX bytes:68712556 (68.7 MB)

wlx503eaa7c4c9b Link encap:Ethernet  HWaddr 50:3e:aa:7c:4c:9b  
         inet addr:192.168.0.4  Bcast:192.168.0.255  Mask:255.255.255.0
         inet6 addr: fe80::15de:9204:3015:b802/64 Scope:Link
         UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
         RX packets:409882 errors:0 dropped:1902 overruns:0 frame:0
         TX packets:308849 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:1000 
         RX bytes:464826692 (464.8 MB)  TX bytes:51670694 (51.6 MB)

6)如果网络接口处于非活动状态,可以使用Ifconfig命令和up选项来激活它。以root权限使用此命令。需要注意的是,网络接口的名称必须在命令选项之前。

$ sudo ifconfig enp0s25 up
$ ifconfig
enp0s25     Link encap:Ethernet  HWaddr 7c:05:07:10:08:8d  
            inet6 addr: fe80::5ba9:5ddd:a609:e410/64 Scope:Link
            UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
            RX packets:0 errors:0 dropped:0 overruns:0 frame:0
            TX packets:4719 errors:0 dropped:0 overruns:0 carrier:0
            collisions:0 txqueuelen:1000 
            RX bytes:0 (0.0 B)  TX bytes:963440 (963.4 KB)
            Interrupt:20 Memory:f7c00000-f7c20000 

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:65536  Metric:1
            RX packets:190712 errors:0 dropped:0 overruns:0 frame:0
            TX packets:190712 errors:0 dropped:0 overruns:0 carrier:0
            collisions:0 txqueuelen:1000 
            RX bytes:69933565 (69.9 MB)  TX bytes:69933565 (69.9 MB)

wlx503eaa7c4c9b   Link encap:Ethernet  HWaddr 50:3e:aa:7c:4c:9b  
                  inet addr:192.168.0.4  Bcast:192.168.0.255  Mask:255.255.255.0
                  inet6 addr: fe80::15de:9204:3015:b802/64 Scope:Link
                  UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
                  RX packets:410653 errors:0 dropped:1929 overruns:0 frame:0
                  TX packets:309651 errors:0 dropped:0 overruns:0 carrier:0
                  collisions:0 txqueuelen:1000 
                  RX bytes:465020094 (465.0 MB)  TX bytes:51915953 (51.9 MB) 

7)ifconfig命令可以与接口名称(eth0)和ip地址一起使用,为指定的接口分配ip地址。以root权限使用此命令。

$ sudo ifconfig eth0 192.168.31.228
$ ifconfig
eth0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
      inet 192.168.31.228  netmask 255.255.255.0  broadcast 192.168.31.255
      ether 20:25:64:08:6a:a4  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
      device interrupt 20  memory 0xf7c00000-f7c20000  

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 16589  bytes 1698274 (1.6 MB)
      RX errors 0  dropped 0  overruns 0  frame 0
      TX packets 16589  bytes 1698274 (1.6 MB)
      TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wlan0:   flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
         inet 192.168.31.222  netmask 255.255.255.0  broadcast 192.168.31.255
         inet6 fe80::fa1c:1e5f:feac:5280  prefixlen 64  scopeid 0x20<link>
         inet6 2409:4053:2105:527d:db24:166e:345d:d48a  prefixlen 64  scopeid 0x0<global>
         inet6 2409:4053:2105:527d:653c:9f2d:750d:4528  prefixlen 64  scopeid 0x0<global>
         ether a4:2b:8c:5f:19:0b  txqueuelen 1000  (Ethernet)
         RX packets 53256  bytes 15930912 (15.9 MB)
         RX errors 0  dropped 0  overruns 0  frame 0
         TX packets 58487  bytes 12868848 (12.8 MB)
         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

8)要同时分配IP地址、网络掩码地址和广播地址,可以使用以下命令。以root权限使用此命令。

$ ifconfig eth0 192.168.31.144 netmask 255.255.255.0 broadcast 192.168.31.255
$ ifconfig
eth0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
      inet 192.168.31.144  netmask 255.255.255.0  broadcast 192.168.31.255
      ether 20:25:64:08:6a:a4  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
      device interrupt 20  memory 0xf7c00000-f7c20000  

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 19543  bytes 2004727 (2.0 MB)
      RX errors 0  dropped 0  overruns 0  frame 0
      TX packets 19543  bytes 2004727 (2.0 MB)
      TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wlan0:   flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
         inet 192.168.31.222  netmask 255.255.255.0  broadcast 192.168.31.255
         inet6 fe80::fa1c:1e5f:feac:5280  prefixlen 64  scopeid 0x20<link>
         inet6 2409:4053:2105:527d:db24:166e:345d:d48a  prefixlen 64  scopeid 0x0<global>
         inet6 2409:4053:2105:527d:653c:9f2d:750d:4528  prefixlen 64  scopeid 0x0<global>
         ether a4:2b:8c:5f:19:0b  txqueuelen 1000  (Ethernet)
         RX packets 62912  bytes 19006480 (19.0 MB)
         RX errors 0  dropped 0  overruns 0  frame 0
         TX packets 69417  bytes 15159473 (15.1 MB)
         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

9)每当网卡接收到一个分组时,它都会验证该分组是否属于自己。否则,它会正常地丢弃分组,但在混杂模式下,它会接受流经网卡的所有分组。Ifconfig命令连同接口名称(eth0)和promisc参数启用混杂模式。在这种模式下,接口卡将捕获所有正在广播的数据包。以root权限使用此命令。这通常被Wireshark等网络嗅探程序用于分析广播数据包。

$ ifconfig eth0 promisc
$ ifconfig
eth0:    flags=4355<UP,BROADCAST,PROMISC,MULTICAST>  mtu 1000
         inet 192.168.31.144  netmask 255.255.255.0  broadcast 192.168.31.255
         ether aa:bb:cc:dd:ee:ff  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
         device interrupt 20  memory 0xf7c00000-f7c20000  

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 22588  bytes 2330398 (2.3 MB)
         RX errors 0  dropped 0  overruns 0  frame 0
         TX packets 22588  bytes 2330398 (2.3 MB)
         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wlan0:   flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
         inet 192.168.31.222  netmask 255.255.255.0  broadcast 192.168.31.255
         inet6 fe80::fa1c:1e5f:feac:5280  prefixlen 64  scopeid 0x20<link>
         inet6 2409:4053:2105:527d:db24:166e:345d:d48a  prefixlen 64  scopeid 0x0<global>
         inet6 2409:4053:2105:527d:653c:9f2d:750d:4528  prefixlen 64  scopeid 0x0<global>
         ether a4:2b:8c:5f:19:0b  txqueuelen 1000  (Ethernet)
         RX packets 73307  bytes 22471053 (22.4 MB)
         RX errors 0  dropped 0  overruns 0  frame 0
         TX packets 80986  bytes 17490208 (17.4 MB)
         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

10)使用带有接口名称(eth0)和-promisc参数的ifconfig命令来禁用混杂模式。

$ ifconfig eth0 -promisc
$ ifconfig
eth0:    flags=4099<UP,BROADCAST,MULTICAST>  mtu 1000
         inet 192.168.31.144  netmask 255.255.255.0  broadcast 192.168.31.255
         ether aa:bb:cc:dd:ee:ff  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
         device interrupt 20  memory 0xf7c00000-f7c20000  

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 22990  bytes 2375724 (2.3 MB)
         RX errors 0  dropped 0  overruns 0  frame 0
         TX packets 22990  bytes 2375724 (2.3 MB)
         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wlan0:   flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
         inet 192.168.31.222  netmask 255.255.255.0  broadcast 192.168.31.255
         inet6 fe80::fa1c:1e5f:feac:5280  prefixlen 64  scopeid 0x20<link>
         inet6 2409:4053:2105:527d:db24:166e:345d:d48a  prefixlen 64  scopeid 0x0<global>
         inet6 2409:4053:2105:527d:653c:9f2d:750d:4528  prefixlen 64  scopeid 0x0<global>
         ether a4:2b:8c:5f:19:0b  txqueuelen 1000  (Ethernet)
         RX packets 74842  bytes 23020724 (23.0 MB)
         RX errors 0  dropped 0  overruns 0  frame 0
         TX packets 82714  bytes 17824215 (17.8 MB)
         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

推荐阅读
cjavapy编程之路首页