How to use Ifconfig command

Table of Contents

Introduction – Ifconfig overview

Are you wondering how to use the ifconfig command or looking to explore important features it supports ?

This article will help answer all your questions. Ifconfig is one of the most used  commands in the linux environment. It provides means to both view details and configure an interface.  

To find the path of the command execution on the linux machine you can check the PATH environment variable using the following env command 

root@DiscoveringSystems-ubuntu:/usr/sbin# env | grep -i path
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin

Let’s check the exact location of the ifconfig command script 

root@DiscoveringSystems-ubuntu:/# cd /usr/bin
root@DiscoveringSystems-ubuntu:/usr/bin# ls | grep -i ifconfig
root@DiscoveringSystems-ubuntu:/usr/bin# cd ..
root@DiscoveringSystems-ubuntu:/usr# cd sbin/
root@DiscoveringSystems-ubuntu:/usr/sbin# ls | grep -i ifconfig
ifconfig

To check for all the options available for use along with the ifconfig command , you can use the help option 

root@DiscoveringSystems-ubuntu:/# ifconfig -h

How to use Ifconfig command to check available interfaces 

Ifconfig by default wont show the interfaces which was down , Here in this example , we can see that the interface ens38 seems to be not showing 

root@DiscoveringSystems-ubuntu:/$ ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.44.151  netmask 255.255.255.0  broadcast 192.168.44.255
        inet6 fe80::20c:29ff:fec0:a8b7  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:c0:a8:b7  txqueuelen 1000  (Ethernet)
        RX packets 254  bytes 28557 (28.5 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 133  bytes 16559 (16.5 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 172  bytes 13688 (13.6 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 172  bytes 13688 (13.6 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

If option ‘a’ is used along with the ifconfig we can see that all the interfaces is present on a linux machine , irrespective of whether is up or down

root@DiscoveringSystems-ubuntu:/$ ifconfig -a
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.44.151  netmask 255.255.255.0  broadcast 192.168.44.255
        inet6 fe80::20c:29ff:fec0:a8b7  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:c0:a8:b7  txqueuelen 1000  (Ethernet)
        RX packets 323  bytes 33703 (33.7 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 192  bytes 25735 (25.7 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens38: flags=4098<BROADCAST,MULTICAST>  mtu 1500
        ether 00:0c:29:c0:a8:c1  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

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 172  bytes 13688 (13.6 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 172  bytes 13688 (13.6 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

How to bring an interface up or down using Ifconfig 

To bring an interface which is down to be up which we know that it is connected to the switch externally , the following commands needs to be used 

root@DiscoveringSystems-ubuntu:/$  ifconfig ens38 up

root@DiscoveringSystems-ubuntu:/$ ifconfig ens38


ens38: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::20c:29ff:fec0:a8c1  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:c0:a8:c1  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6  bytes 516 (516.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

To bring down an interface , we can do the following 

root@DiscoveringSystems-ubuntu:/$ ifconfig ens38 down

root@DiscoveringSystems-ubuntu:/$ ifconfig ens38
ens38: flags=4098<BROADCAST,MULTICAST>  mtu 1500
        ether 00:0c:29:c0:a8:c1  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 12  bytes 936 (936.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

How to use short option (-s) along with Ifconfig command

We can use a short option(-s) with Ifconfig command to show only the list of interfaces instead of the detailed format.

root@DiscoveringSystems-ubuntu:/# ifconfig -s
Iface      MTU    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
ens33     1500     3343      0      0 0          2020      0      0      0 BMRU
ens38     1500        0      0      0 0            47      0      0      0 BMRU
lo       65536      202      0      0 0           202      0      0      0 LRU

How to use ifconfig to assign an ip and remove ip address from an interface 

The following ifconfig commands assign the IP address for an interface and the subnet mask.

root@DiscoveringSystems-ubuntu:/# ifconfig ens38 192.168.94.10
root@DiscoveringSystems-ubuntu:/# ifconfig ens38 netmask 255.255.255.0

root@DiscoveringSystems-ubuntu:/# ifconfig ens38
ens38: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.94.10  netmask 255.255.255.0  broadcast 192.168.94.255
        inet6 fe80::20c:29ff:fec0:a8c1  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:c0:a8:c1  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 22  bytes 1732 (1.7 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Or 

root@DiscoveringSystems-ubuntu:/# ifconfig ens38 192.168.94.10 netmask 255.255.255.0
root@DiscoveringSystems-ubuntu:/# ifconfig ens38
ens38: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.94.10  netmask 255.255.255.0  broadcast 192.168.94.255
        inet6 fe80::20c:29ff:fec0:a8c1  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:c0:a8:c1  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 43  bytes 3394 (3.3 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

The following ifconfig example can be used to remove ip address from an Interface 

root@DiscoveringSystems-ubuntu:/# ifconfig ens38 0.0.0.0
root@DiscoveringSystems-ubuntu:/# ifconfig ens38
ens38: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::20c:29ff:fec0:a8c1  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:c0:a8:c1  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 44  bytes 3464 (3.4 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

How to use DHCP for assigning an IP address to an interface 

If we would like to use DHCP for of assigning of an automatic  Ip address , we may need to use dhclient command instead of the Ifconfig , as ifconfig can help with the static assigning of Ips only.Working of the DHCP is out of scope of this article , so will be covered on a separate article 

root@DiscoveringSystems-ubuntu:/# dhclient -4 ens38

After executing the above mentioned command Immediately we will be able to see the following packet , DHCP discover will be sent out , until the DHCP [ Discover Offer Request Acknowledgement ]  process completes and it gets an Ip address 

00:09:25.521529 00:0c:29:c0:a8:c1 > Broadcast, ethertype IPv4 (0x0800), length 342: (tos 0x10, ttl 128, id 0, offset 0, flags [none], proto UDP (17), length 328)
    0.0.0.0.bootpc > 255.255.255.255.bootps: [udp sum ok] BOOTP/DHCP, Request from 00:0c:29:c0:a8:c1, length 300, xid 0x91bf9a20, secs 27, Flags [none] (0x0000)
          Client-Ethernet-Address 00:0c:29:c0:a8:c1
          Vendor-rfc1048 Extensions
            Magic Cookie 0x63825363
            DHCP-Message Option 53, length 1: Discover
            Hostname Option 12, length 32: "DiscoveringSystems-ubuntu.ds.com"
            Parameter-Request Option 55, length 13:
              Subnet-Mask, BR, Time-Zone, Default-Gateway
              Domain-Name, Domain-Name-Server, Option 119, Hostname
              Netbios-Name-Server, Netbios-Scope, MTU, Classless-Static-Route
              NTP

How to change the MTU of an Interface using Ifconfig

We can use the ifconfig command to change the MTU of an interface, MTU [ Maximum Transmission Unit ] usually represents the size of the IP packet including the IP header without an ethernet header.

root@DiscoveringSystems-ubuntu:/# ifconfig ens38 mtu 1000

root@DiscoveringSystems-ubuntu:/# ifconfig ens38
ens38: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1000
        inet 192.168.94.10  netmask 255.255.255.0  broadcast 192.168.94.255
        ether 00:0c:29:c0:a8:c1  txqueuelen 1000  (Ethernet)
        RX packets 8  bytes 632 (632.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 52  bytes 11968 (11.9 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

After setting the MTU , you might be wondering what will happen if i am sending a icmp / ping packet of size 1500 bytes 

root@DiscoveringSystems-ubuntu:/# ping 192.168.94.20 -s 1500
PING 192.168.94.20 (192.168.94.20) 1500(1528) bytes of data.
1508 bytes from 192.168.94.20: icmp_seq=1 ttl=64 time=1.10 ms

Now while checking the packet capture of those simultaneously while pinging , we see the Fragmented packets going out when we size our pings to 1500 bytes as the MTU is set only to 1000 Bytes . The working of the Fragmentation is not in the scope of this article

[root@discoveringsystems-centos /]# tcpdump -nevvi ens33
tcpdump: listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
00:19:30.021596 00:0c:29:c0:a8:c1 > 00:0c:29:5f:1c:7d, ethertype IPv4 (0x0800), length 1010: (tos 0x0, ttl 64, id 64379, offset 0, flags [+], proto ICMP (1), length 996)
    192.168.94.10 > 192.168.94.20: ICMP echo request, id 3, seq 1, length 976
00:19:30.021625 00:0c:29:c0:a8:c1 > 00:0c:29:5f:1c:7d, ethertype IPv4 (0x0800), length 566: (tos 0x0, ttl 64, id 64379, offset 976, flags [none], proto ICMP (1), length 552)
    192.168.94.10 > 192.168.94.20: ip-proto-1
00:19:30.021713 00:0c:29:5f:1c:7d > 00:0c:29:c0:a8:c1, ethertype IPv4 (0x0800), length 1514: (tos 0x0, ttl 64, id 24566, offset 0, flags [+], proto ICMP (1), length 1500)
    192.168.94.20 > 192.168.94.10: ICMP echo reply, id 3, seq 1, length 1480

To set the MTU to the default value 1500 bytes you can do the following 

root@DiscoveringSystems-ubuntu:/# ifconfig ens38 mtu 1500
root@DiscoveringSystems-ubuntu:/# ifconfig ens38
ens38: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.94.10  netmask 255.255.255.0  broadcast 192.168.94.255
        inet6 fe80::20c:29ff:fec0:a8c1  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:c0:a8:c1  txqueuelen 1000  (Ethernet)
        RX packets 53  bytes 33908 (33.9 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 128  bytes 54310 (54.3 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Maximum value a ubuntu machine allowed me to set was MTU of 16110 bytes. Most of the production deployments will be set to the default of 1500 bytes to avoid complexity. However some advanced network users set it to MTU around 9000 and configure the switches in between with MTU of 9214 to handle this large MTU from the host side to reduce  the overhead due to packet header while data transfer. 

root@DiscoveringSystems-ubuntu:/# ifconfig ens38 mtu 16111
SIOCSIFMTU: Invalid argument

root@DiscoveringSystems-ubuntu:/# ifconfig ens38 mtu 16110

root@DiscoveringSystems-ubuntu:/# ifconfig ens38
ens38: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 16110
        inet 192.168.94.10  netmask 255.255.255.0  broadcast 192.168.94.255
        inet6 fe80::20c:29ff:fec0:a8c1  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:c0:a8:c1  txqueuelen 1000  (Ethernet)
        RX packets 53  bytes 33908 (33.9 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 157  bytes 58332 (58.3 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

How to enable promiscuous mode on an interface using Ifconfig 

Running an interface in promiscuous mode is usually done by advanced users. Promiscuous mode allows the interface to receive the packets and allow it for further processing. Without Promiscuous mode , the Interface drops the packet on the nic which is not destined to the mac owned by the nic card. For example , Let’s say you are capturing packets received on the interface ens38 for analysis , you won’t be able to see those packets which have destination mac not owned by the ens38 in the tcpdump  without promiscuous mode . 

If config common provides us a way to enable and disable promiscuous mode on an interface. Here is how to enable it

root@DiscoveringSystems-ubuntu:/# ifconfig ens38 promisc
root@DiscoveringSystems-ubuntu:/# ifconfig ens38
ens38: flags=4419<UP,BROADCAST,RUNNING,PROMISC,MULTICAST>  mtu 1500
        inet 192.168.94.10  netmask 255.255.255.0  broadcast 192.168.94.255
        inet6 fe80::20c:29ff:fec0:a8c1  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:c0:a8:c1  txqueuelen 1000  (Ethernet)
        RX packets 53  bytes 33908 (33.9 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 176  bytes 64054 (64.0 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Disabling the promisc config for a port 

root@DiscoveringSystems-ubuntu:/# ifconfig ens38 -promisc
root@DiscoveringSystems-ubuntu:/# ifconfig ens38
ens38: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.94.10  netmask 255.255.255.0  broadcast 192.168.94.255
        inet6 fe80::20c:29ff:fec0:a8c1  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:c0:a8:c1  txqueuelen 1000  (Ethernet)
        RX packets 53  bytes 33908 (33.9 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 181  bytes 65492 (65.4 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Please note that packet capture tools such as tcpdump when ran on a port , will put those port temporarily on the promiscuous mode automatically and will remove the ports from promiscuous mode right after stopping the tcpdump 

How to change the HW ethernet mac address of the port using ifconfig

Ifconfig command provides ways to change the ethernet hardware mac address for an interface , just like changing the IP address 

root@DiscoveringSystems-ubuntu:/# ifconfig ens38 hw ether 00:00:ab:cd:ab:cd
root@DiscoveringSystems-ubuntu:/#

root@DiscoveringSystems-ubuntu:/# ifconfig ens38
ens38: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.94.10  netmask 255.255.255.0  broadcast 192.168.94.255
        inet6 fe80::20c:29ff:fec0:a8c1  prefixlen 64  scopeid 0x20<link>
        ether 00:00:ab:cd:ab:cd  txqueuelen 1000  (Ethernet)
        RX packets 349  bytes 63195 (63.1 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 387  bytes 100852 (100.8 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

How to create alias for a interface using ifconfig

We can see that a alias network interface is created with the Ip address from the same subnet if you use the following syntax 

root@DiscoveringSystems-ubuntu:/# ifconfig ens38:0
ens38:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 00:00:ab:cd:ab:cd  txqueuelen 1000  (Ethernet)

Likewise we can also assign an Ip address for the same

root@DiscoveringSystems-ubuntu:/# ifconfig ens38:0 192.168.94.200
root@DiscoveringSystems-ubuntu:/# ifconfig ens38:0
ens38:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.94.200  netmask 255.255.255.0  broadcast 192.168.94.255
        ether 00:00:ab:cd:ab:cd  txqueuelen 1000  (Ethernet)

To view both  parent and alias interface , you can use the regular ifconfig command without any option

root@DiscoveringSystems-ubuntu:/# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.44.151  netmask 255.255.255.0  broadcast 192.168.44.255
        inet6 fe80::20c:29ff:fec0:a8b7  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:c0:a8:b7  txqueuelen 1000  (Ethernet)
        RX packets 3782  bytes 309965 (309.9 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2118  bytes 250723 (250.7 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens38: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.94.10  netmask 255.255.255.0  broadcast 192.168.94.255
        inet6 fe80::200:abff:fecd:abcd  prefixlen 64  scopeid 0x20<link>
        ether 00:00:ab:cd:ab:cd  txqueuelen 1000  (Ethernet)
        RX packets 353  bytes 63511 (63.5 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 403  bytes 102144 (102.1 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens38:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.94.200  netmask 255.255.255.0  broadcast 192.168.94.255
        ether 00:00:ab:cd:ab:cd  txqueuelen 1000  (Ethernet)

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 378  bytes 29356 (29.3 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 378  bytes 29356 (29.3 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

We can also create an alias interface with a different subnet IP address. By this method we can assign many different subnet Ip address to a single port 

root@DiscoveringSystems-ubuntu:/# ifconfig ens38:0 192.168.85.200
root@DiscoveringSystems-ubuntu:/# ifconfig ens38:0
ens38:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.85.200  netmask 255.255.255.0  broadcast 192.168.85.255
        ether 00:00:ab:cd:ab:cd  txqueuelen 1000  (Ethernet)

Inorder to remove already created alias interface , we can use the following ifconfig command syntax 

root@DiscoveringSystems-ubuntu:/# ifconfig ens38:0 down

Now we can confirm that that alias interface is deleted 

root@DiscoveringSystems-ubuntu:/# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.44.151  netmask 255.255.255.0  broadcast 192.168.44.255
        inet6 fe80::20c:29ff:fec0:a8b7  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:c0:a8:b7  txqueuelen 1000  (Ethernet)
        RX packets 4251  bytes 355559 (355.5 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2373  bytes 282962 (282.9 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens38: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.94.10  netmask 255.255.255.0  broadcast 192.168.94.255
        inet6 fe80::200:abff:fecd:abcd  prefixlen 64  scopeid 0x20<link>
        ether 00:00:ab:cd:ab:cd  txqueuelen 1000  (Ethernet)
        RX packets 880  bytes 109653 (109.6 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 806  bytes 211016 (211.0 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 404  bytes 32336 (32.3 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 404  bytes 32336 (32.3 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Leave a Comment

Your email address will not be published. Required fields are marked *