初学者的Linux ifconfig命令教程(7个示例)

初学者的Linux ifconfig命令教程(7个示例)

网络管理工作包括很多东西,正因为如此,有几个Linux命令行工具可以帮助您完成不同类型的网络相关任务。 在本教程中,我们将讨论一个名为ifconfig的实用程序。 但在开始之前,值得一提的是本文讨论的所有示例都已在Ubuntu 16.04 LTS机器上进行了测试。

Linux ifconfig命令

ifconfig命令使您可以配置网络接口。 当然,您也可以使用此工具获取与网络接口相关的信息。 以下是它的语法:

ifconfig [-v] [-a] [-s] [interface]
ifconfig [-v] interface [aftype] options | address ...

下面是该工具的手册页如何解释它:

       Ifconfig  is  used to configure the kernel-resident network interfaces.
       It is used at boot time to set up interfaces as necessary.  After that,
       it  is  usually  only  needed  when  debugging or when system tuning is
       needed.

       If no arguments are given, ifconfig displays the  status  of  the  cur?
       rently  active interfaces.  If a single interface argument is given, it
       displays the status of the given interface only; if a single  -a  argu?
       ment  is  given,  it  displays the status of all interfaces, even those
       that are down.  Otherwise, it configures an interface.

以下是一些Q&A样式的示例,应该给你一个关于ifconfig命令如何工作的好主意。

Q1。 如何使用ifconfig命令?

基本用法很简单 - 只需执行'ifconfig'命令即可使用任何选项。

ifconfig

在输出中,您将看到与所有活动接口相关的信息。 例如,在我的情况下,产生了以下输出。

请注意,如果您希望命令显示所有接口(包括当前处于关闭状态的接口),请使用-a命令行选项。

Q2。 如何使ifconfig显示紧凑的输出?

对于短列表输出,请使用-s命令行选项。

ifconfig -s

以下是一个示例输出:

Q3。 如何使ifconfig显示有关特定接口的信息?

这可以通过将接口的名称指定为ifconfig命令的参数来完成。

ifconfig [interface-name]

例如:

Q4。 如何使用ifconfig禁用或启用网络接口?

为此,只需指定'向下'或'向上'标志。 例如,要禁用名为eth0的接口,请按以下方式使用ifconfig命令:

ifconfig eth0 down

同样,要启用,请使用以下命令:

ifconfig eth0 up

Q5。 如何使用ifconfig更改网络MTU?

这可以通过使用'mtu'标志以及新的mtu值来完成。

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

例如:

ifconfig eth0 mtu 900

PS:对于那些不知道MTU是什么的人,请到这里

Q6。 如何在界面上启用混杂模式?

混杂模式允许网络接收网络上的所有数据包。 如果这是您的要求,您可以使用'promisc'标志启用此模式。

例如:

ifconfig eth0 promisc

请注意,您可以通过以下方式禁用混杂模式:

ifconfig eth0 -promisc

Q7。 如何更改网络接口的硬件地址?

ifconfig命令还允许您更改网络接口对应的MAC地址。 这可以通过以下方式进行:

ifconfig [network-name] hw [class] [hardware-address]

例如:

ifconfig eth0 hw ether 70:4d:7b:70:d2:3e

以下是手册页介绍此选项的方式:

              Set the hardware address of this interface, if the device driver
              supports  this  operation.  The keyword must be followed by the
              name of the hardware class and the printable ASCII equivalent of
              the  hardware  address.   Hardware  classes  currently supported
              include ether (Ethernet), ax25 (AMPR AX.25), ARCnet  and  netrom
              (AMPR NET/ROM).

结论

所以你可以看到,ifconfig命令可以让你做很多与网络接口相关的事情。 虽然我们在这里讨论了相当多的命令行选项,但还有很多其他命令行选项。 一旦您完成了我们在这里讨论的那些练习后,请转到该工具的手册页以了解更多信息。

赞(52) 打赏
未经允许不得转载:优客志 » 系统运维
分享到:

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏