侧边栏壁纸
Linux入门自学网博主等级

每日学一条Linux命令,终成Linux大神

  • 累计撰写 725 篇文章
  • 累计创建 143 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

Linux ifconfig命令详解:如何配置和显示网络接口(附实例教程和注意事项)

Linux ifconfig命令介绍

ifconfig是interface configuration的缩写,它是一个用来配置和显示Linux内核中网络接口的网络工具。ifconfig可以用来设置网络接口的状态,如启动或停止,分配IP地址,设置子网掩码,修改MAC地址等。ifconfig也可以用来显示网络接口的信息,如IP地址,MAC地址,传输速率,数据包统计等。

Linux ifconfig命令适用的Linux版本

ifconfig是一个传统的Linux命令,它在大多数Linux发行版中都可以使用,但是一些较新的发行版,如Ubuntu 18.04,CentOS 8,Fedora 28等,已经不再默认安装ifconfig,而是使用ip命令来代替。ip命令是一个更强大和灵活的网络工具,它支持IPv6,可以管理路由,隧道,流量控制等功能。如果你想在这些发行版中使用ifconfig,你需要手动安装net-tools包,这是一个包含了ifconfig和其他一些传统网络工具的软件包。安装net-tools的命令如下:

  • Ubuntu/Debian
[linux@bashcommandnotfound.cn ~]$ sudo apt install net-tools
  • CentOS 8/Fedora 28
[linux@bashcommandnotfound.cn ~]$ sudo dnf install net-tools

Linux ifconfig命令的基本语法

ifconfig的基本语法格式如下:

ifconfig [网络接口] [参数]

其中,网络接口是指定要配置或显示的网络接口的名称,如eth0,lo等。如果不指定网络接口,ifconfig会显示所有活动的网络接口的信息。参数是用来设置或修改网络接口的选项,如IP地址,子网掩码,广播地址等。如果不指定参数,ifconfig只会显示网络接口的信息,不会进行任何修改。

Linux ifconfig命令的常用选项说明

ifconfig命令的常用选项如下表所示:

选项说明
-a显示所有网络接口的信息,包括未激活的
-s显示简化的网络接口信息
up激活指定的网络接口
down停止指定的网络接口
IP地址设置指定网络接口的IP地址
netmask 地址设置指定网络接口的子网掩码
broadcast 地址设置指定网络接口的广播地址
hw 类型 地址设置指定网络接口的硬件类型和地址,如hw ether 00:11:22:33:44:55
mtu 数字设置指定网络接口的最大传输单元(MTU)
promisc开启指定网络接口的混杂模式,可以接收所有经过的数据包
-promisc关闭指定网络接口的混杂模式
arp开启指定网络接口的地址解析协议(ARP)
-arp关闭指定网络接口的地址解析协议(ARP)
allmulti开启指定网络接口的所有多播模式,可以接收所有多播数据包
-allmulti关闭指定网络接口的所有多播模式

Linux ifconfig命令的实例

以下是一些使用ifconfig命令的实例:

  • 显示所有活动的网络接口的信息
[linux@bashcommandnotfound.cn ~]$ ifconfig
  • 显示指定网络接口的信息,如eth0
[linux@bashcommandnotfound.cn ~]$ ifconfig eth0
  • 设置指定网络接口的IP地址,如192.168.1.100
[linux@bashcommandnotfound.cn ~]$ sudo ifconfig eth0 192.168.1.100
  • 设置指定网络接口的IP地址和子网掩码,如192.168.1.100/24
[linux@bashcommandnotfound.cn ~]$ sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0
  • 设置指定网络接口的IP地址,子网掩码和广播地址,如192.168.1.100/24,广播地址为192.168.1.255
[linux@bashcommandnotfound.cn ~]$ sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0 broadcast 192.168.1.255
  • 修改指定网络接口的MAC地址,如00:11:22:33:44:55
[linux@bashcommandnotfound.cn ~]$ sudo ifconfig eth0 hw ether 00:11:22:33:44:55
  • 激活指定网络接口,如eth0
[linux@bashcommandnotfound.cn ~]$ sudo ifconfig eth0 up
  • 停止指定网络接口,如eth0
[linux@bashcommandnotfound.cn ~]$ sudo ifconfig eth0 down
  • 开启指定网络接口的混杂模式,如eth0
[linux@bashcommandnotfound.cn ~]$ sudo ifconfig eth0 promisc
  • 关闭指定网络接口的混杂模式,如eth0
[linux@bashcommandnotfound.cn ~]$ sudo ifconfig eth0 -promisc
  • 开启指定网络接口的所有多播模式,如eth0
[linux@bashcommandnotfound.cn ~]$ sudo ifconfig eth0 allmulti
  • 关闭指定网络接口的所有多播模式,如eth0
[linux@bashcommandnotfound.cn ~]$ sudo ifconfig eth0 -allmulti
  • 显示指定网络接口的IP地址,如eth0
[linux@bashcommandnotfound.cn ~]$ ifconfig eth0 | grep 'inet ' | awk '{print $2}'
  • 显示指定网络接口的MAC地址,如eth0
[linux@bashcommandnotfound.cn ~]$ ifconfig eth0 | grep 'ether ' | awk '{print $2}'
  • 显示指定网络接口的数据包统计,如eth0
[linux@bashcommandnotfound.cn ~]$ ifconfig eth0 | grep 'RX packets\|TX packets'

Linux ifconfig命令的注意事项

  • ifconfig命令需要root权限才能修改网络接口的设置,所以需要使用sudo命令来提升权限。
  • ifconfig命令只能临时修改网络接口的设置,如果想要永久保存,需要修改网络配置文件,如/etc/network/interfaces等。
  • ifconfig命令已经被废弃,建议使用ip命令来代替,ip命令的语法和功能更加强大和灵活。
  • 如果在使用ifconfig命令时遇到bash: ifconfig: command not found的错误,说明你的系统没有安装net-tools包,你需要手动安装它,或者使用ip命令来代替。
0

评论区