在 Ubuntu 和其他 Linux 发行版中,命令行工具(command line tools)通常是与计算机内部工作交互的最佳方式。ifconfig 命令用于在Linux上配置网络设置。
ifconfig是 ip 命令的旧版本,但由于其更简单的语法和易用性,许多人仍然更喜欢 ifconfig。但是您究竟如何使用 ifconfig 进行网络接口配置呢?这是一个演练。
安装网络工具
默认情况下,ifconfig 命令不会安装到Linux发行版中。(Linux)大多数系统管理员会在设置时安装它所属的软件包,但在某些情况下,您的计算机上可能会丢失它。您可以轻松安装它。
如果您尝试在未安装 ifconfig 命令的系统上使用它,您将收到一条错误消息 - 以及有关如何安装它的说明。只需输入命令:
sudo apt install 网络工具
这将安装 ifconfig 和其他一些网络配置实用程序。请注意,安装 ifconfig 和启用或禁用网络需要
sudo 。
使用ifconfig
查看网络信息(Network Information Using)
使用 ifconfig 命令最简单的方法是不带任何参数直接运行它。这会显示所有活动接口的信息,包括以太网和无线连接。
这提供了有关网络接口的所有相关信息,包括MTU值(最大传输单元(Transmission Unit))、IP 地址(IP addresses)和数据包历史记录。如果您只希望技术规范采用更易于理解的格式并带有接口名称,请输入 ifconfig -s。
但是,这两个版本都不包括当前不活动的网络。要查看所有网络接口,请改用 ifconfig -a 命令。
当您已经知道网络的名称并且只想要特定接口的状态时,您可以将其名称附加到 ifconfig 命令中。像这样:
ifconfig eth0
使用 ifconfig
启用和禁用网络(Disabling Networks)
监控运行网络的状态并不是使用 ifconfig 唯一可以做的事情。您还可以使用该命令配置这些接口的几乎所有方面,包括MTU和使用的掩码。
最简单的用例是启动或停止网络。您可以使用以下命令执行此操作:
sudo ifconfig 命名
“name”参数是网络的实际名称。这可用于启用使用 ifconfig -a 命令检测到的任何非活动接口。
要禁用网络,只需使用 down 而不是 up:
sudo ifconfig dummy0 关闭
这将禁用接口。
使用 ifconfig 配置网络
修改与网络接口关联的任何值都很容易。请(Just)记住在命令前加上 sudo,因为所有这些更改都需要管理员权限才能生效。
此功能最常见的用途是为网络分配自定义 IP 地址。例如,这是为 dummy0 接口提供新 IP 地址的方法:
sudo ifconfig dummy0 29.95.245.112
您可以在网络名称后输入任何有效的 IP 地址来分配它。如果您输入了错误的内容,您只会收到一条消息。否则,更改将立即生效。
您还可以像这样指定网络掩码:
sudo ifconfig dummy0 网络掩码 255.255.255.0
设置广播地址的语法类似:
sudo ifconfig dummy0 广播 77.40.108.115
如果您要修改网络的所有这些 IP 地址,将所有命令集中在一起会更有效。像大多数Linux命令一样,您可以在同一行中包含多个参数。
一个网络接口也可以有一个或多个别名。这些由 name:x 的语法指定,其中 name 是网络的名称和 xa 数字。例如,这里是如何将别名 IP 地址添加到 dummy0 网络。
sudo ifconfig dummy0:0 69.72.169.2
您可以通过使用 ifconfig 获取其状态来确认它是否有效。
ifconfig dummy0:0
你还能用 ifconfig 做什么?
ifconfig 命令有一个广泛的参数列表,您可以使用这些参数来修改网络接口的许多其他变量。您可以启用多播、设置MTU和激活混杂模式等。
官方手册页(official man pages)有一个完整的列表,列出了可以与 ifconfig 一起使用的所有参数,以及每个参数的技术定义。当然,您可以使用 man ifconfig 命令从终端本身调用手册。
什么时候应该使用 ifconfig?
对于大多数用户来说,几乎没有理由使用 ifconfig 命令。默认提供的自动设置足以满足正常的 Internet 使用。
但是,如果您是一名系统管理员,想要对其网络接口进行更精细的控制,那么 ifconfig 正是您的理想之选。您可以在启动系统时使用该命令正确配置网络接口,然后几乎忘记它。
它在诊断网络问题时也很方便,因为您可以查询任何网络接口的状态并对其进行配置。您可以使用 ifconfig 重新启动网络,甚至可以切换技术参数,直到它工作更顺畅。
Linux Ifconfig Command: How to Use It
In Ubuntu and other Linux distroѕ, command line tools are usually the best way to interact with the inner workings of the computer. The ifconfig command is used for configuring network settings on Linux.
Ifconfig is an older version of the ip command, but many still prefer ifconfig due to its simpler syntax and ease of use. But how exactly do you use ifconfig for network interface configuration? Here is a walkthrough.
Installing Net-Tools
The ifconfig command doesn’t come installed into Linux distributions by default. Most system administrators will install the package it belongs to when setting things up, but in some cases, it might be missing on your computer. You can install it easily.
If you try using the ifconfig command on a system that doesn’t have it installed, you’ll get an error message – along with instructions on how to install it. Just enter the command:
sudo apt install net-tools
This will install ifconfig and a few other network configuration utilities. Note that sudo is required for installing ifconfig and enabling or disabling networks.
Viewing Network Information Using ifconfig
The simplest way to use the ifconfig command is to run it directly without any parameters. This brings up information on all active interfaces, including ethernet and wireless connections.
This provides all pertinent information about the network interfaces, including MTU value (Maximum Transmission Unit), IP addresses, and packet history. If you only want the technical specs in a more digestible format with interface names, enter ifconfig -s.
However, both of these versions don’t include currently inactive networks. To view all network interfaces, use the ifconfig -a command instead.
When you already know the name of the networks and only want the status of a particular interface, you can append its name to the ifconfig command. Like this:
ifconfig eth0
Enabling and Disabling Networks With ifconfig
Monitoring the status of running networks isn’t the only thing you can do with ifconfig. You can also use the command to configure almost every aspect of these interfaces, down to the MTU and the masking used.
The simplest use case is to start up or stop networks. You can do this by using the following command:
sudo ifconfig name up
The “name” parameter is the actual name of the network. This can be used to enable any of the inactive interfaces detected using the ifconfig -a command.
To disable a network, just use down instead of up:
sudo ifconfig dummy0 down
This will disable the interface.
Configuring Networks With ifconfig
Modifying any value associated with a network interface is easy. Just remember to prepend the command with sudo, as all these changes require administrator privileges to take effect.
The most common use of this feature is to assign a custom IP address to a network. For example, this is how to give a new IP address to the dummy0 interface:
sudo ifconfig dummy0 29.95.245.112
You can enter any valid IP address after the network name to assign it. You will only get a message if you entered something incorrectly. Otherwise, the change will immediately take effect.
You can also specify a netmask like this:
sudo ifconfig dummy0 netmask 255.255.255.0
The syntax for setting a broadcast address is similar:
sudo ifconfig dummy0 broadcast 77.40.108.115
If you’re looking to modify all these IP addresses for a network, it’s more efficient to lump all the commands together. Like most Linux commands, you can include multiple arguments in the same line.
A network interface can also have one or more aliases. These are specified by the syntax of name:x, where name is the network’s name and x a digit. For example, here is how to add an alias IP address to the dummy0 network.
sudo ifconfig dummy0:0 69.72.169.2
You can confirm that it worked by using ifconfig to get its status.
ifconfig dummy0:0
What Else Can You Do With ifconfig?
The ifconfig command has an extensive list of arguments that you can use to modify many other variables of a network interface. You can enable multicast, set the MTU, and activate promiscuous mode, among other things.
The official man pages have a complete list of all the arguments that can be used with ifconfig, along with a technical definition of what each does. You can, of course, invoke the manual from the terminal itself using the man ifconfig command.
When Should You Use ifconfig?
For most users, there’s little reason to be using the ifconfig command. The automatic settings provided by default are good enough for normal internet usage.
But if you’re a system administrator who wants more granular control over their network interfaces, then ifconfig is just the tool for you. You can use the command upon booting up the system to properly configure network interfaces and then pretty much forget about it.
It’s handy in diagnosing network issues as well since you can query the status of any network interface and configure it. You can use ifconfig to restart networks or even switch technical parameters until it works more smoothly.