5.1 ifconfig

ifconfig 是我们查看当前系统的网卡和 ip 地址信息的最常用命令。如果你的机器上还没安装 ifconfig 命令,可以使用如下命令安装:

yum install net-tools
1

安装成功以后,我们就可以使用 ifconfig 查看机器网卡信息了:

[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.206.140  netmask 255.255.255.0  broadcast 192.168.206.255
        inet6 fe80::1599:dcc4:b3e8:7fce  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:ee:01:80  txqueuelen 1000  (Ethernet)
        RX packets 1005  bytes 77224 (75.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 349  bytes 47206 (46.0 KiB)
        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 4  bytes 352 (352.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4  bytes 352 (352.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

上述输出显示了本机当前激活的网卡列表,以及每个激活的网卡(网络设备)的 ipv4、ipv6 地址、子网掩码、广播地址等信息,这里一共两个网卡,分别是 ens33lo

我们可以使用 -s 选项显示网卡信息的精简列表:

[root@localhost ~]# ifconfig -s
Iface      MTU    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
ens33            1500     1086      0      0 0           379      0      0      0 BMRU
lo              65536        4      0      0 0             4      0      0      0 LRU
1
2
3
4

默认情况下,ifconfig 命令只会显示激活的网卡信息,可以使用 -a 选项显示所有的(包括未激活)的网卡信息。

使用 ifconfig up 命令激活某个网卡,使用 ifconfig down 命令禁用某个网卡,这两个命令的用法如下:

ifconfig 网卡名 up
ifconfig 网卡名 down
1
2

我们来演示一下这两个命令的用法:

[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.206.140  netmask 255.255.255.0  broadcast 192.168.206.255
        inet6 fe80::1599:dcc4:b3e8:7fce  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:ee:01:80  txqueuelen 1000  (Ethernet)
        RX packets 1398  bytes 114269 (111.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 601  bytes 97657 (95.3 KiB)
        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 12  bytes 1056 (1.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 12  bytes 1056 (1.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

## 禁用网卡 lo 后默认只能看到一个网卡信息
[root@localhost ~]# ifconfig lo down
[root@localhost ~]# ifconfig 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.206.140  netmask 255.255.255.0  broadcast 192.168.206.255
        inet6 fe80::1599:dcc4:b3e8:7fce  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:ee:01:80  txqueuelen 1000  (Ethernet)
        RX packets 1510  bytes 123232 (120.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 657  bytes 104751 (102.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost ~]# ifconfig -a
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.206.140  netmask 255.255.255.0  broadcast 192.168.206.255
        inet6 fe80::1599:dcc4:b3e8:7fce  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:ee:01:80  txqueuelen 1000  (Ethernet)
        RX packets 1543  bytes 125948 (122.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 675  bytes 107251 (104.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=8<LOOPBACK>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 12  bytes 1056 (1.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 12  bytes 1056 (1.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
## 再次启用网卡 lo
[root@localhost ~]# ifconfig lo up
[root@localhost ~]# ifconfig 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.206.140  netmask 255.255.255.0  broadcast 192.168.206.255
        inet6 fe80::1599:dcc4:b3e8:7fce  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:ee:01:80  txqueuelen 1000  (Ethernet)
        RX packets 1615  bytes 131924 (128.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 715  bytes 112423 (109.7 KiB)
        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 12  bytes 1056 (1.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 12  bytes 1056 (1.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

ifconfig 还可以将一个 ip 地址绑定到某个网卡上,或将一个 ip 地址从某个网卡上解绑,使用如下命令:

# 将指定ip地址绑定到某个网卡
ifconfig 网卡名 add ip地址
# 从某个网卡解绑指定ip地址
ifconfig 网卡名 del ip地址
1
2
3
4

演示上述两个操作如下:

[root@localhost ~]# ifconfig 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.206.140  netmask 255.255.255.0  broadcast 192.168.206.255
        inet6 fe80::1599:dcc4:b3e8:7fce  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:ee:01:80  txqueuelen 1000  (Ethernet)
        RX packets 1615  bytes 131924 (128.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 715  bytes 112423 (109.7 KiB)
        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 12  bytes 1056 (1.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 12  bytes 1056 (1.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost ~]# ifconfig ens33 add 192.168.206.150
[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.206.140  netmask 255.255.255.0  broadcast 192.168.206.255
        inet6 fe80::1599:dcc4:b3e8:7fce  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:ee:01:80  txqueuelen 1000  (Ethernet)
        RX packets 1804  bytes 145940 (142.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 781  bytes 119581 (116.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.206.150  netmask 255.255.255.0  broadcast 192.168.206.255
        ether 00:0c:29:ee:01:80  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)
        RX packets 12  bytes 1056 (1.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 12  bytes 1056 (1.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

上述操作中,网卡 ens33 开始绑定了 ip 地址 192.168.206.140,我们使用 ifconfig add 命令绑定了一个新的 ip :192.168.206.150,现在我们可以使用这个新的 ip 地址来访问原来的网络了。

同理,如果要解绑这个 ip 地址,可以按如下操作:

[root@localhost ~]# ifconfig ens33 del 192.168.206.150
[root@localhost ~]# ifconfig -a
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.206.140  netmask 255.255.255.0  broadcast 192.168.206.255
        inet6 fe80::1599:dcc4:b3e8:7fce  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:ee:01:80  txqueuelen 1000  (Ethernet)
        RX packets 2127  bytes 172321 (168.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 953  bytes 139954 (136.6 KiB)
        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 18  bytes 1560 (1.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 18  bytes 1560 (1.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

在 Windows 上与 ifconfig 相对应的命令是 ipconfig,如在 Windows 上查看所有网卡信息可以使用 ipconfig /all

上次更新: 2025/04/01, 20:53:14

社区交流

讨论与留言

前往 GitHub Issues →

本留言区仅对应当前文章,欢迎补充观点、提出问题或帮助修正文中疏漏。 留言由 GitHub/Gitalk 提供,需要使用 GitHub 登录。

最近更新
第 1 课:专栏导论与安全边界
07-01
第 3 课:安装编译与调试环境
07-01
第 4 课:第三方库下载、编译与依赖管理
07-01
更多文章>