ifconfig
(界面配置)是一个网络管理工具。它用于配置和查看Linux操作系统中网络接口的状态。和ifconfig
,您可以分配 IP 地址、启用或禁用接口、管理 ARP 缓存、路由等。
在本文中,我们将探讨如何使用ifconfig
命令。
如何安装ifconfig
#
The ifconfig
命令已弃用并替换为ip并且可能不包含在较新的 Linux 发行版中。
如果您收到一条错误消息“ifconfig:未找到命令”,则意味着您的系统上未安装包含该命令的软件包。
Install ifconfig
在 Ubuntu/Debian 上#
在基于 Ubuntu 和 Debian 的 Linux 发行版上,运行以下命令进行安装ifconfig
:
sudo apt install net-tools -y
Install ifconfig
在 Centos 上#
安装ifconfig
在 CentOS 和其他基于 RHEL 的 Linux 发行版上,输入:
sudo dnf install net-tools -y
如何使用ifconfig
命令#
的基本语法ifconfig
命令如下图所示:
ifconfig [-a] [-v] [-s] <interface> [[<AF>] <address>]
Where:
-
interface
- 是网络接口的名称。
-
address
- 是您要分配的 IP 地址。
使用设置的配置ifconfig
命令不是持久的。系统重新启动后,所有更改都会丢失。要使更改永久生效,您需要编辑特定于发行版的配置文件或将命令添加到启动脚本中。
只有 root 或具有 sudo 权限的用户才能配置网络接口。
当不带任何选项调用时,ifconfig
显示所有网络接口的配置信息和关联的 IP 地址:
ifconfig -a
输出包括有关所有活动和非活动网络接口的信息:
docker0 Link encap:Ethernet HWaddr 56:84:7a:fe:97:99
inet addr:172.17.42.1 Bcast:0.0.0.0 Mask:255.255.0.0
UP BROADCAST MULTICAST 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:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
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:4198 errors:0 dropped:0 overruns:0 frame:0
TX packets:4198 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1
RX bytes:498729 (498.7 KB) TX bytes:498729 (498.7 KB)
eth0 Link encap:Ethernet HWaddr 4c:bb:58:9c:f5:55
inet addr:172.20.10.3 Bcast:172.20.10.15 Mask:255.255.255.240
inet6 addr: 2401:4900:1d65:40a1:4ebb:58ff:fe9c:f555/64 Scope:Global
inet6 addr: 2401:4900:1d65:40a1:f1c9:6a90:2d99:924e/64 Scope:Global
inet6 addr: fe80::4ebb:58ff:fe9c:f555/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:84110 errors:0 dropped:0 overruns:0 frame:0
TX packets:59727 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:70667629 (70.6 MB) TX bytes:20886290 (20.8 MB)
要显示任何特定网络接口的配置信息,请在命令后面写入接口名称:
ifconfig eth0
输出将如下所示:
eth0 Link encap:Ethernet HWaddr 4c:bb:58:9c:f5:55
inet addr:172.20.10.3 Bcast:172.20.10.15 Mask:255.255.255.240
inet6 addr: 2401:4900:1d65:40a1:4ebb:58ff:fe9c:f555/64 Scope:Global
inet6 addr: 2401:4900:1d65:40a1:f1c9:6a90:2d99:924e/64 Scope:Global
inet6 addr: fe80::4ebb:58ff:fe9c:f555/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:84110 errors:0 dropped:0 overruns:0 frame:0
TX packets:59727 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:70667629 (70.6 MB) TX bytes:20886290 (20.8 MB)
为网络接口分配 IP 地址和网络掩码#
随着ifconfig
命令,您可以为网络接口分配 IP 地址和网络掩码。
使用以下语法分配 IP 地址和网络掩码:
ifconfig [interface-name] [ip-address] netmask [subnet-mask]
例如,分配IP地址192.168.0.101
和网络掩码255.255.0.0
到界面eth0
,你会运行:
ifconfig eth0 192.168.0.101 netmask 255.255.0.0
您还可以使用接口别名为网络接口分配辅助 IP 地址:
ifconfig eth0:0 192.168.0.102 netmask 255.255.0.0
启用和禁用网络接口#
有时,您可能需要重置网络接口。在这种情况下,ifconfig
命令可用于启用或禁用网络接口。
要禁用活动网络接口,请输入设备名称,后跟down
flag:
ifconfig eth0 down
要启用非活动网络接口,请使用up
flag:
ifconfig eth0 up
启用和禁用混杂模式#
混杂允许网络接口访问和查看网络中的所有数据包。您可以使用ifconfig
命令启用和禁用特定网络设备上的混杂。
要在网络接口上启用混杂模式,请输入promisc
设备名称后的标志:
ifconfig eth0 promisc
要禁用混杂模式,请使用-promisc
flag
ifconfig eth0 -promisc
更改网络接口的 MTU#
MTU“最大传输单元”允许您限制在接口上传输的数据包的大小。
您可以使用以下语法更改 MTU 值:
ifconfig [interface-name] mtu [mtu-value]
例如设置网络接口的MTU值eth0
to 500
,运行以下命令:
ifconfig eth0 mtu 500
更改网络接口的 MAC 地址#
MAC“媒体访问控制”是唯一标识网络上设备的物理地址。
要更改网络接口的 MAC 地址,请使用hw ether
设置新 MAC 地址的标志:
ifconfig eth0 hw ether 00:00:2d:3a:2a:28
结论#
我们已经向您展示了如何使用ifconfig
命令配置并显示有关网络接口的信息。欲了解更多信息ifconfig
,访问ifconfig 命令手册页
.
如果您有任何疑问,请在下面留言。