当前位置:首页 > Ubuntu > 正文

Ubuntu ifconfig命令详解(手把手教你配置Linux网络接口)

在使用 Ubuntu 或其他 Linux 系统时,了解如何查看和配置网络接口是系统管理和故障排查的基础技能。本文将详细介绍 ifconfig 命令的使用方法,帮助你轻松掌握 网络接口配置 的核心操作。无论你是刚接触 Linux 的小白,还是有一定经验的用户,都能从中受益。

Ubuntu ifconfig命令详解(手把手教你配置Linux网络接口) ifconfig命令 网络接口配置 Linux网络管理 ifconfig使用教程 第1张

什么是 ifconfig?

ifconfig(interface configuration)是一个用于配置和显示 Linux 系统中网络接口参数的命令行工具。通过它,你可以查看 IP 地址、子网掩码、MAC 地址等信息,也可以启用或禁用网络接口。

安装 ifconfig(Ubuntu 默认可能未安装)

从 Ubuntu 18.04 开始,ifconfig 不再默认安装,取而代之的是更现代的 ip 命令。但很多用户仍习惯使用 ifconfig,因此你可以通过以下命令安装它:

sudo apt updatesudo apt install net-tools  

安装完成后,就可以正常使用 ifconfig 命令了。

基本用法:查看网络接口信息

在终端中直接输入以下命令:

ifconfig  

输出示例:

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500        inet 192.168.1.100  netmask 255.255.255.0  broadcast 192.168.1.255        inet6 fe80::a00:27ff:fe4e:1234  prefixlen 64  scopeid 0x20<link>        ether 08:00:27:4e:12:34  txqueuelen 1000  (Ethernet)        RX packets 12345  bytes 1234567 (1.2 MB)        TX packets 6789   bytes 987654 (987.6 KB)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)  

其中:

  • eth0 是有线网卡接口(也可能是 enp0s3 等新命名方式)
  • lo 是本地回环接口(loopback)
  • inet 表示 IPv4 地址
  • ether 是 MAC 地址

常用操作示例

1. 查看特定接口信息

ifconfig eth0  

2. 启用/禁用网络接口

启用接口(需要 root 权限):

sudo ifconfig eth0 up  

禁用接口:

sudo ifconfig eth0 down  

3. 临时设置 IP 地址(重启后失效)

sudo ifconfig eth0 192.168.1.200 netmask 255.255.255.0  

注意事项与替代方案

虽然 ifconfig 使用简单直观,但它已被视为“过时”工具。现代 Linux 发行版推荐使用 ip 命令(属于 iproute2 工具集),例如:

ip addr showip link set eth0 up  

不过,对于学习和快速排查问题,ifconfig 依然是一个非常实用的工具。掌握它有助于理解 Linux网络管理 的基本原理。

总结

通过本篇 ifconfig使用教程,你应该已经掌握了如何在 Ubuntu 中安装、查看和配置网络接口。无论是日常运维还是面试准备,这些知识都非常实用。建议多在虚拟机中练习,加深理解。

关键词回顾:Ubuntu ifconfig命令网络接口配置Linux网络管理ifconfig使用教程