Skip to main content

Linux networking: ifconfig versus ip

The ifconfig command has served us well, but now it's time to look to the future with its replacement, the ip command.
Image
ifconfig vs ip
Image by 272447 from Pixabay

What was the first Linux networking command that you learned? If I were a betting man, I would wager that many of you (like myself) learned how to look at your IP address and ethernet configurations. I would also wager that an overwhelming majority of Linux users first learned to do so via the ifconfig command.

Interface Configuration, a.k.a. ifconfig, has to be one of the most used Linux commands of all time, and it's pretty plain to see why. It's easy to remember (unless you are coming from a Windows environment), and it displays most, if not all, of the necessary networking information for a huge number of use cases.

On the other hand, we have the ip command suite. This command-line tools package is the new kid on the block, relatively speaking, and has been chosen as the way forward by the bleeding edge of Linux users. With added functionality and a steadily growing user base, the ip command is a serious contender for your muscle memory or aliases.

So let's take a look at these two commands to see what is on the ip command suite offers.

The sure thing

You know your favorite ball cap? The one that has the sweat stains inside the headliner, but throwing it on just feels right? That's ifconfig. It's safe, it's familiar, and you feel comfortable using it. The ifconfig command still has a lot to offer its users. Whether its displaying network settings, configuring an IP address or netmask, creating aliases for interfaces, or setting MAC address, ifconfig can handle it. Let's take a look at how to use ifconfig to accomplish some more common tasks you may find yourself working on completing.

Displaying current network settings

This is the most basic and overused form of the ifconfig command. Chances are, you are running this to get information about a particular interface, and while this works, it will probably over-deliver. Let's look at the syntax and output and see where we could improve our I/O.

    [tcarrigan@rhel ~]$ ifconfig
    enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 10.0.3.16  netmask 255.255.255.0  broadcast 10.0.2.255
            inet6 fe80::7678:fd9:450c:13c1  prefixlen 64  scopeid 0x20<link>
            ether 08:00:27:30:52:a7  txqueuelen 1000  (Ethernet)
            RX packets 20  bytes 2424 (2.3 KiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 61  bytes 6699 (6.5 KiB)
            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 0  bytes 0 (0.0 B)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 0  bytes 0 (0.0 B)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
            inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
            ether 52:54:00:b7:7b:c7  txqueuelen 1000  (Ethernet)
            RX packets 0  bytes 0 (0.0 B)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 0  bytes 0 (0.0 B)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

As you can see, there is a lot of information to sift through here. ifconfig, devoid of any arguments or options, will list all active interfaces with detail. So how can we narrow it down to exactly what we want? Many times you just want to look at a specific interface.

Display entries for specific interface

To view details for a specific interface, use the standard ifconfig command followed by the interface name. For example:

    [tcarrigan@rhel ~]$ ifconfig enp0s3
    enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 10.0.3.16  netmask 255.255.255.0  broadcast 10.0.2.255
            inet6 fe80::7678:fd9:450c:13c1  prefixlen 64  scopeid 0x20<link>
            ether 08:00:27:30:52:a7  txqueuelen 1000  (Ethernet)
            RX packets 34234  bytes 46491161 (44.3 MiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 3779  bytes 333350 (325.5 KiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Enabling and disabling an interface

With the ifconfig command, you can do far more than just view configurations. Let's take a look at how to enable and disable an interface.

To enable an interface, you have two options for syntax:

    [root@rhel ~]# ifconfig enp0s3 up

or

    [root@rhel ~]# ifup enp0s3

The same goes for disabling an interface. Only instead of up we use down, as seen here:

    [root@rhel ~]# ifconfig enp0s3 down

or

    [root@rhel ~]# ifdown enp0s3

In this line of work, it really doesn't get much easier than that.

Assigning an IP address to an interface

To assign an IP address to a given interface, you use the following syntax:

 [root@rhel ~]# ifconfig [interface] [IP]

For example:

  [root@rhel ~]# ifconfig eth0 192.168.0.1

Assigning a netmask to an interface

To assign a netmask to an interface, you use the following:

    [root@rhel ~]# ifconfig [interface] netmask [netmask]

It should look like this:

    [root@rhel ~]# ifconfig eth0 netmask 255.255.255.254

Set MTU for an interface

The MTU or Maximum Transmission Unit is a very important figure to pay attention to during troubleshooting. This value allows you to limit the size of packets sent over the specific interface. I once worked a data replication failure for over three weeks before figuring out that the MTU was too large for the replication interface. Fun times *insert eye roll here*. I say this to make a point. Not every network interface can support jumbo packets. Be sure that the set MTU is supported for your interface. To set the MTU:

    [root@rhel ~]# ifconfig eth0 mtu 1080

There is a lot more that ifconfig can do; however, in the interest of brevity, I am going to move along. If you want more information on ifconfig, check out the man page or your friendly neighborhood search engine.

[ Getting started with networking? Check out the Linux networking cheat sheet. ]

What's trending?

The ip command is the future of network config commands. ifconfig has been officially deprecated for the ip suite, so while many of us are still using the old ways, it is time to put those habits to rest and move on with the world.

Displaying entries

Since there is no "without options" variant of the ip command, let's look at how to display IP addresses associated with interfaces.

    [root@rhel ~]# ip addr show
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
        inet 127.0.0.1/8 scope host lo
           valid_lft forever preferred_lft forever
        inet6 ::1/128 scope host
           valid_lft forever preferred_lft forever
    2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
        link/ether 08:00:27:30:52:a7 brd ff:ff:ff:ff:ff:ff
        inet 10.0.3.16/24 brd 10.0.2.255 scope global dynamic noprefixroute enp0s3
           valid_lft 83478sec preferred_lft 83478sec
        inet6 fe80::7678:fd9:450c:13c1/64 scope link noprefixroute
           valid_lft forever preferred_lft forever
    3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
        link/ether 52:54:00:b7:7b:c7 brd ff:ff:ff:ff:ff:ff
        inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
           valid_lft forever preferred_lft forever
    4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel master virbr0 state DOWN group default qlen 1000
        link/ether 52:54:00:b7:7b:c7 brd ff:ff:ff:ff:ff:ff

You will notice that this presents much of the same information as the base ifconfig command.

Adding a new address

To add an IP address to an existing interface, use the following:

    [root@rhel ~]# ip addr add 192.168.0.1 dev eth0

The dev flag is used before designating which device or interface that the IP address is being set on.

Removing an address

To remove an IP address from an interface, use the following:

    [root@rhel ~]# ip addr del 192.168.0.1 dev eth0

Enable and disable an interface

Much like the ifconfig command, you can also cycle an interface on/off using the ip command. To enable an interface, use the following:

    [root@rhel ~]# ip link set eth0 up

to disable:

    [root@rhel ~]# ip link set eth0 down

View the routing table

One of the best things you can use while troubleshooting is to view routing information. To do this, use the following:

    [root@rhel ~]# ip route show
    default via 10.0.3.2 dev enp0s3 proto dhcp metric 100
    10.0.3.0/24 dev enp0s3 proto kernel scope link src 10.0.3.16 metric 100
    192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1 linkdown

You can also add and remove static routes from the routing table.

To add a static route, we use the following:

    [root@rhel ~]# ip route add 10.5.5.10/22 via 192.168.0.1 dev eth0

To remove a static route, we use the following:

    [root@rhel ~]# ip route del 10.5.5.10/22

Note that these are not persistent, and you must take additional steps to ensure that the routes created actually survive a reboot. To create the persistent route, we need to create an entry in /etc/sysconfig/network-scripts/route-eth0 (substitute eth0 with whatever interface the route is created on). Something like this:

    [root@rhel ~]# vi /etc/sysconfig/network-scripts/route-eth0
        10.5.5.10/22 via 192.168.0.1 dev eth0

Over before it began

This article is not so much a fight between the two as it is an exploration of old and new. The decision has already been made, as ifconfig is being deprecated. The ip command suite is the way forward. However, just because something is new doesn't mean it is superior for every purpose. At the same time, however, "we've always done it this way" is not an excuse to stop improving. What are your thoughts? My honest takeaway is that ifconfig meets most of my needs (at this point in my career). However, the ip command is a more powerful tool and will be a staple for networking experts for years to come.

[ Want more for your network? Download a free ebook on network automation with Ansible. ]

Topics:   Networking  
Author’s photo

Tyler Carrigan

Tyler is a community manager at Enable Sysadmin, a submarine veteran, and an all-round tech enthusiast! He was first introduced to Red Hat in 2012 by way of a Red Hat Enterprise Linux-based combat system inside the USS Georgia Missile Control Center. More about me

Related Content

OUR BEST CONTENT, DELIVERED TO YOUR INBOX