2

Linux ifconfig命令

 3 years ago
source link: https://www.myfreax.com/linux-ifconfig-command/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

ifconfig(interface configuration)是一种网络管理工具。它用于在 Linux 操作系统中配置和查看网络接口的状态。使用ifconfig,您可以分配 IP 地址、启用或禁用网络接口、管理ARP 缓存、路由等。

在本文中,我们将探讨如何使用ifconfig命令。

如何安装 ifconfig

ifconfig命令已弃用并替换为ip 在较新的 Linux 发行版,并且可能不包含在该发行版中。

如果您收到一条错误消息“ifconfig: command not found”,则表示您的系统上未安装包含该命令的软件包。

ifconfig在 Ubuntu/Debian 上安装

在基于 Ubuntu 和基于 Debian 的 Linux 发行版上,运行以下命令进行安装ifconfig

sudo apt install net-tools -y

ifconfig在 Centos 上安装

要在CentOS和其他基于RHEL的Linux发行版上安装ifconfig,请键入:

sudo dnf install net-tools -y

如何使用ifconfig命令

ifconfig命令的基本语法如下所示:

ifconfig [-a] [-v] [-s] <interface> [[<AF>] <address>]
  • 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标志:

ifconfig eth0 down

要启用非活动网络接口,请使用以下up标志:

ifconfig eth0 up

启用和禁用混杂模式

混杂模式允许网络接口访问和查看网络中的所有数据包。您可以使用ifconfig命令在特定网络设备上启用和禁用混杂模式。

要在网络接口上启用混杂模式,请在设备名称后输入标志promisc

ifconfig eth0 promisc

要禁用混杂模式,请使用-promisc标志

ifconfig eth0 -promisc

更改网络接口的 MTU

MTU“最大传输单元”允许您限制在接口上传输的数据包的大小。

您可以使用以下语法更改 MTU 值:

ifconfig [interface-name] mtu [mtu-value]

例如,设置一个网络接口的MTU值eth0500,运行下面的命令:

ifconfig eth0 mtu 500

更改网络接口的 MAC 地址

MAC“媒体访问控制”是唯一标识网络上设备的物理地址。

要更改网络接口的 MAC 地址,请使用该hw ether标志来设置新的 MAC 地址:

ifconfig eth0 hw ether 00:00:2d:3a:2a:28

我们已经向您展示了如何使用该ifconfig命令来配置和显示有关网络接口的信息。有关更多信息ifconfig,请访问ifconfig 命令手册页

如果您有任何问题,请在下方留言。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK