📜  Linux 中的 ifconfig 命令和示例(1)

📅  最后修改于: 2023-12-03 15:02:43.445000             🧑  作者: Mango

Linux 中的 ifconfig 命令和示例

简介

在 Linux 中,ifconfig 是一个用于配置和显示网络接口信息的命令行工具。它可以用来查询、设置和修改网络接口的状态、IP 地址、子网掩码、广播地址等信息。ifconfig 命令常用于网络故障排查、配置网络接口等任务。

使用方法

ifconfig 命令的基本语法如下:

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
        ether 00:0c:29:4f:c0:65  txqueuelen 1000  (以太网)
        RX packets 120527  bytes 15023634 (14.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 21346  bytes 2632090 (2.5 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (本地环回)
        RX packets 8  bytes 616 (616 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8  bytes 616 (616 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
查看指定网络接口信息
$ ifconfig eth0

输出示例:

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.100  netmask 255.255.255.0  broadcast 192.168.1.255
        ether 00:0c:29:4f:c0:65  txqueuelen 1000  (以太网)
        RX packets 120527  bytes 15023634 (14.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 21346  bytes 2632090 (2.5 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
设置 IP 地址
$ sudo ifconfig eth0 192.168.1.100

该命令将 eth0 网络接口的 IP 地址设置为 192.168.1.100。

关闭网络接口
$ sudo ifconfig eth0 down

该命令将关闭 eth0 网络接口。

打开网络接口
$ sudo ifconfig eth0 up

该命令将打开 eth0 网络接口。

添加子网掩码
$ sudo ifconfig eth0 netmask 255.255.255.0

该命令将设置 eth0 网络接口的子网掩码为 255.255.255.0。

添加广播地址
$ sudo ifconfig eth0 broadcast 192.168.1.255

该命令将设置 eth0 网络接口的广播地址为 192.168.1.255。

结论

ifconfig 命令是一个强大而常用的 Linux 网络管理工具,可以查询、配置和修改网络接口的信息。本文提供了 ifconfig 命令的基本使用方法和示例,希望对程序员在 Linux 环境下进行网络管理有所帮助。