Categories: Shell编程

Linux ifconfig命令详解

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

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

ifconfig安装

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

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

在 Ubuntu/Debian安装 ifconfig

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

sudo apt install net-tools -y

在Centos安装ifconfig

要在 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”命令在特定网络设备上启用和禁用混合。

要在网络接口上启用混合模式,请在设备名称后输入“混合”标志:

ifconfig eth0 promisc

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

ifconfig eth0 -promisc

更改网络接口的 MTU

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

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

例如,将网络接口“eth0”的 MTU 值设置为“500”,运行以下命令:

ifconfig eth0 mtu 500

更改网络接口的 MAC 地址

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

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

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

以上就是ifconfig所有用法了。

5.0
06
什么是 SSL证书?SSL证书详解
Python 虚拟环境: 如何创建、激活、停用和删除?
嘻嘻

嘻嘻IT: 笔者是一个工作七八年的程序猿老鸟,从事涉及的技术栈主要包括PHP、Linux、Devops等,喜欢研究新技术,尝试新技术,提升技术自动化和开发效率,致力于write less,do more! 技术每年都会层出不穷,领域划分的越来越细,不可能学习所有的东西,保持对技术的好奇心,理解技术中核心思想,做一个有深度,有思想的开发!

Share
Published by
嘻嘻
Tags: linuxshell

Recent Posts

全球货币导航网页上线了!

o在全球化的今天,货币兑换和国…

10小时 ago

bash字符串拼接

在编程中,字符串的拼接是一个非…

11小时 ago

Bash Case详解

Bash case 语句通常用…

11小时 ago

Bash for详解

for循环是编程语言中的基础概…

11小时 ago

liunux中你必须知道alias命令?

在Linux操作系统中,无论你…

1天 ago

zshrc文件详解

Zsh 是一个强大的 shel…

2天 ago