1. Overview

There are times that we need to inspect and reconfigure network interfaces on a Linux machine. This helps us in troubleshooting network problems, installing, or configuring a network interface card.

In this tutorial, we’ll be looking at ifconfig, which is for managing our network interfaces. We’ll look at its syntax and some common use cases.

2. What Is ifconfig?

ifconfig stands for “interface configuration”. It allows us to view and configure network interface settings.

$ ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.91.129  netmask 255.255.255.0  broadcast 192.168.91.255
        ether 00:0c:29:b6:7f:1a  txqueuelen 1000  (Ethernet)
        RX packets 1823  bytes 916481 (916.4 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 950  bytes 105268 (105.2 KB)
        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
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 639  bytes 56025 (56.0 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 639  bytes 56025 (56.0 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

As we can see, just running this command outputs information about all active network interfaces.

3. Basic Syntax

Let’s take a look at the basic syntax:

ifconfig [options] [interface]

The options parameter determines what the ifconfig command displays.

The interface parameter specifies a particular network interface that we want to target with the command.

4. Viewing a Specific Network Interface

Let’s say we wanted to view the details of a single interface ens33:

$ ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.91.129  netmask 255.255.255.0  broadcast 192.168.91.255
        ether 00:0c:29:b6:7f:1a  txqueuelen 1000  (Ethernet)
        RX packets 2281  bytes 949247 (949.2 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 991  bytes 111961 (111.9 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

As we can see, the list now only contains the details of the ens33 interface.

5. Common Display Options

5.1. -a (all)

If we want to show all interfaces, regardless of whether they are active, we can add the -a flag:

$ ifconfig -a
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.91.129  netmask 255.255.255.0  broadcast 192.168.91.255
        ether 00:0c:29:b6:7f:1a  txqueuelen 1000  (Ethernet)
        RX packets 90793  bytes 114935888 (114.9 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 32279  bytes 2094115 (2.0 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=8<LOOPBACK>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 776  bytes 67908 (67.9 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 776  bytes 67908 (67.9 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

This command returned both the ens33 interface as well as the lo interface.

We can see whether an interface is enabled or disabled by looking at the flags, in particular the UP and RUNNING keywords.

From what we can see here, it appears that the ens33 network interface is enabled and running, while the lo interface is disabled.

This command would display only the ens33 network interface if we were to run it without the -a option.

5.2. -s (short)

For a quick summary of the interfaces, with some brief network stats, we can use the -s option:

$ ifconfig -s
Iface      MTU    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
ens33     1500    90798      0      0 0         32284      0      0      0 BMRU
lo       65536      799      0      0 0           799      0      0      0 LRU

This command provides us with a shortened list of network interfaces.

We should note that these network interface names will likely vary from computer to computer.

5.3. -v (verbose)

We can use the -v option to see all active network interfaces with verbose error information:

$ ifconfig -v
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.91.129  netmask 255.255.255.0  broadcast 192.168.91.255
        ether 00:0c:29:b6:7f:1a  txqueuelen 1000  (Ethernet)
        RX packets 90826  bytes 114938592 (114.9 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 32287  bytes 2094984 (2.0 MB)
        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
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 802  bytes 70854 (70.8 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 802  bytes 70854 (70.8 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

While this output looks the same as the default output, it will also provide additional information if there are errors.

6. Taking Interfaces Up and Down

The up and down commands are used to enable and disable specific network interfaces. Let’s imagine we want to disable interface ens33:

$ sudo ifconfig ens33 down

We should note that we need to use sudo to elevate our rights to use the command.

The command stopped the ens33 network interface. However, it didn’t produce any output to tell us.

So, let’s check that the interface has definitely been disabled:

$ ifconfig -s
Iface      MTU    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
lo       65536      817      0      0 0           817      0      0      0 LRU

Now, let’s re-enable the interface and check it again:

$ sudo ifconfig ens33 up
$ ifconfig -s
Iface      MTU    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
ens33     1500    91291      0      0 0         32340      0      0      0 BMRU
lo       65536      821      0      0 0           821      0      0      0 LRU

7. Changing Interface Settings

By default, Linux assigns IP Addresses to most network interfaces via DHCP. This is generally recommended, but if we need to give a network interface a specific static IP Address, Subnet Mask, or Broadcast Address, we can use ifconfig.

7.1. Set Static IP Address

Let’s use ifconfig to set our interface to the static IP address 192.168.91.200:

$ sudo ifconfig ens33 192.168.91.200

Now, let’s have a look at the interface:

$ ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.91.200  netmask 255.255.255.0  broadcast 192.168.91.255
        inet6 fe80::eda4:f797:27d4:df20  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:b6:7f:1a  txqueuelen 1000  (Ethernet)
        RX packets 93199  bytes 115117763 (115.1 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 32845  bytes 2168554 (2.1 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Here we see that the value of the IP Address was changed as expected.

7.2. Set Subnet Mask

When we change the IP Address of a network interface, it’s likely that we’ll also want to change the subnet mask to match.

We use the netmask option for this:

$ sudo ifconfig ens33 netmask 255.255.0.0
$ ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.91.128  netmask 255.255.0.0  broadcast 192.168.255.255
        inet6 fe80::eda4:f797:27d4:df20  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:b6:7f:1a  txqueuelen 1000  (Ethernet)
        RX packets 93400  bytes 115137530 (115.1 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 33066  bytes 2200524 (2.2 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

We can see that the above command has changed the netmask to 255.255.0.0.

7.3. Set Broadcast Address

The broadcast address often assigns itself, but if we wanted to specify a specific broadcast address, we could do so with the broadcast option:

$ sudo ifconfig ens33 broadcast 10.2.255.255
$ ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.91.128  netmask 255.255.255.0  broadcast 10.2.255.255
        inet6 fe80::eda4:f797:27d4:df20  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:b6:7f:1a  txqueuelen 1000  (Ethernet)
        RX packets 93690  bytes 115157142 (115.1 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 33113  bytes 2209591 (2.2 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

As we can see, this command has changed the broadcast address to 10.2.255.255.

7.4. Set IP Address, Subnet Mask, and Broadcast Address in One Line

Specifying each of the networking parameters in individual commands would be inefficient if we needed to change them all.

However, ifconfig allows us to set them all at once:

$ sudo ifconfig ens33 10.2.1.101 netmask 255.255.0.0 broadcast 10.2.255.255

If we run ifconfig again, we now see all of the network settings that we specified:

$ ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.2.1.101  netmask 255.255.0.0  broadcast 10.2.255.255
        inet6 fe80::eda4:f797:27d4:df20  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:b6:7f:1a  txqueuelen 1000  (Ethernet)
        RX packets 94069  bytes 115188532 (115.1 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 33260  bytes 2231587 (2.2 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

8. Conclusion

In this article, we learned what the ifconfig command is used for. Then, we looked at how to use it to review interfaces in a couple of formats.

Next, we saw how the command allows us to bring interfaces up and down and how we can use it to reconfigure networking settings.

For more information on additional options, we can type ifconfig –help or take a look at its man file.


« 上一篇: Linux man 命令
» 下一篇: 从文件中删除空行