Linux

Linux IfConfig Tutorial

The network configuration is one of the first topics a system administrator gets involved with. For Linux, the command to configure this is ifconfig (contraction of interface and configuration), which allows to make almost any configuration in a network, as we will see in this tutorial.

Basic knowledge about networking (what IP addresses are, their format, subnetting, etc.) is assumed in order to follow this tutorial.

For this tutorial, Linux Mint 17.3 has been used.

1. Show network interface information

The most simple way to use ifconfig is typing the command in the terminal, with no options or arguments:

ifconfig

If you try it, you will receive and output similar to the following:

eth0      Link encap:Ethernet  HWaddr XX:XX:XX:XX:XX:XX  
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:3124 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3124 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:376288 (376.2 KB)  TX bytes:376288 (376.2 KB)

wlan0     Link encap:Ethernet  HWaddr XX:XX:XX:XX:XX:XX  
          inet addr:192.168.1.154  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::6627:37ff:feb3:592d/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:72347 errors:0 dropped:0 overruns:0 frame:0
          TX packets:55487 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:82986485 (82.9 MB)  TX bytes:7833391 (7.8 MB)

We can see three main information segments. Each of this segment is the information about each network interface. In this case, there are three: eth0, lo, and wlan0.

You will probably be familiar to ethernet and WLAN interfaces, which are are for wired and unwired connections, respectively. But the loopback interface, lo, is quite special. The loopback interface is a virtual interface (does not rely in hardware) that the machines use to communicate with themselves. This interface has two main purposes:

  • Troubleshooting.
  • Running a local server.

Now that we have identified the devices, let’s understand their properties and configurations:

  • HWAddr: hardware address, commonly known as MAC address. An unique identifier assigned to each network interface.
  • inet addr: the IP address of the interface.
  • Bcast: the broadcast address of the network.
  • Mask: the mask of the IP address. That is, the mask that indicates which part of the IP address corresponds to the network, and which to the hosts.
  • inet6 addr: the IPv6 address of the interface.
  • UP: the interface has been loaded.
  • BROADCAST: the interface supports broadcasting, i.e., sending/receiving frames from/to every host in the network, in a single transmission, by broadcast address.
  • RUNNING: the interface is ready to transmit and receive data.
  • MULTICAST: he interface supports multicasting, i.e., sending/receiving from/to interested hosts in the network. Note the difference with broadcasting.
  • MTU: he Maximum Transmission Unit. We will see this later in the tutorial.
  • Metric: the priority of the device. The lower number, the greater priority.
  • RX packets, TX packets: received and transmitted packages, respectively.
  • RX bytes, TX bytes: received and transmitted bytes, respectively.

For seeing the information about a specific interface, we can specify it:

ifconfig eth0

2. Enabling and disabling interfaces

We can enable and disable the interfaces pretty easily. The syntax is the following:

ifconfig [interface] [up|down]

So, the following commands would disable the eth0 interface, for then enabling it again:

sudo ifconfig eth0 down
sudo ifconfig eth0 up

Note that for this operations, we need superuser privileges.

3. Changing IP addresses

3.1. IPv4

The assigned IP (and netmask and broadcast) can be changed executing ifconfig in the following way:

ifconfig [interface] [ip address] netmask [netmask] broadcast [broadcast ip]

For example, to change wlan0 interface’s IP to, for instance, 192.168.1.30 address:

sudo ifconfig wlan0 192.168.1.30

Now, if we check the configuration, we would see:

wlan0     Link encap:Ethernet  HWaddr 64:27:37:b3:59:2d  
          inet addr:192.168.1.30  Bcast:192.168.1.255  Mask:255.255.255.0

3.1.1 Considerations

Before changing this configurations, we must ensure several things:

  • The new IP has to be inside the network. In this case, we knew that the IP was 192.168.1.154 and the mask 255.255.255.0, so, the IP must be in the 192.168.1.2 – 192.168.1.254 range, inclusive. We discard the first and the last since they are the network and the broadcast addresses.
  • The new IP has to be free. If we assign an IP that is already assigned to another host, we won’t have connectivity.

3.2. IPv6

In the previous section we have seen how to configure the IP addresses. As you probably already know, that corresponded to the fourth version of IP (IPv4).

Progressively, the IP addresses will be migrated from IPv4 to IPv6 (even if they will coexist). So, a system administration must also master the IPv6 protocol.

Returning to the ifconfig output, for both loopback and WLAN interfaces (the ethernet interface, in this case, is disabled):

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host

wlan0     Link encap:Ethernet  HWaddr 64:27:37:b3:59:2d  
          inet addr:192.168.1.154  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::6627:37ff:feb3:592d/64 Scope:Link

We can notice that our interface have already assigned an IPv6 address (lines 3, 9). The first, the one of the loopback interface is, as you probably has guessed, the equivalent of 127.0.0.1 for IPv6.

For pinging IPv6 addresses, we have to use ping6 command, instead of ping:

ping6 ::1

And the packets should be correctly transmitted:

PING ::1(::1) 56 data bytes
64 bytes from ::1: icmp_seq=1 ttl=64 time=0.028 ms

We can add IPv6 addresses to interfaces:

sudo ifconfig wlan0 inet6 add fe80::6627:37ff:feb3:592e/64

As same as modifying, but adding the inet6 (for IPv6) and add options.

If we check now the interface…

wlan0     Link encap:Ethernet  HWaddr 64:27:37:b3:59:2d  
          inet addr:192.168.1.155  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::6627:37ff:feb3:592e/64 Scope:Link
          inet6 addr: fe80::6627:37ff:feb3:592d/64 Scope:Link

We can see that our WLAN interface has now two IPv6 addresses.

To delete a IPv6 address, we have to do the same, but with del option instead of add:

sudo ifconfig wlan0 inet6 del fe80::6627:37ff:feb3:592e/64

3.3. Change from static IP to DHCP

This is not exactly an ifconfig function. But is something we would have to do if we entered an incorrect IP, and we want to forget it, and delegate the IP assignment to the DHCP protocol.

For that, we just have to type:

sudo dhclient <interface>

4. Using virtual IP addresses

Also called aliases, these IP addresses are additional addresses to the interfaces. So, an interface could listen to several IP addresses.

If the IP address of the wlan0 interface was 192.168.1.154, we could add, for example, the 192.168.1.155 address:

sudo ifconfig wlan0:0 192.168.1.155

Which would generate another output for ifconfig, just as another interface more:

wlan0:0   Link encap:Ethernet  HWaddr XX:XX:XX:XX:XX:XX
          inet addr:192.168.1.155  Bcast:192.168.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

Each virtual interface must follow the <interface>:n format, where n is any positive number (including 0), and unique to each virtual interface.

For example, if we have running a web server in the machine, now would be accessible from both 192.168.1.154 and 192.168.1.155 addresses. Using virtual IP addresses is specially useful when we want to server several services in the same machine, but for different IP addresses.

To remove a virtual interface, we have to set it down:

sudo ifconfig wlan0:0 down

5. Changing MTU

By default, for ethernet, the maximum transmission unit (i.e., the size in bytes of the largest data unit that can be transmitted), is 1500. But this value can be easily changed:

sudo ifconfig eth0 mtu 2000

5.1. Finding best MTU size

This is not a likely option to change. But there are mechanisms to find the best MTU values.

The problem is that, if the size is too small, the packets are smaller and they are transmitted faster, but this can suppose an overhead due to the acknowledgment and handling of each packet. And, if the size is too large, a router in the packet route may drop the packet.

To find the equilibrium, we can make some tests manually with ping. We can start making pings with the default value, 1500:

ping -M do -c 1 -s 1472 www.systemcodegeeks.com

The size specified here is 1472, but because the the IP/ICMP header of 28 bytes is not included.

We can increase this value while we success response:

1472 bytes from 104.27.130.144: icmp_seq=1 ttl=56 time=38.0 ms

--- systemcodegeeks.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms

And, until we get an error message:

ping: local error: Message too long, mtu=1600

--- systemcodegeeks.com ping statistics ---
1 packets transmitted, 0 received, +1 errors, 100% packet loss, time 0ms

The best MTU size would be the larger one that does not return an error, plus 28 (the previous mentioned header size).

6. Changing MAC address

Yes, ifconfig allows to change the physical address of a network interface.

Well, that’s not exactly true. The physical addresses of devices cannot be changed, but what it can be changed is the address that is loaded into the memory.

This is pretty easy to do with ifconfig:

sudo ifconfig eth0 down
sudo ifconfig eth0 hw ether XX:XX:XX:XX:XX:XX
sudo ifconfig eth0 up
sudo ifconfig eth0

(Placing a real MAC address where XX:XX:XX:XX:XX:XX is placed).

We can check that effectively the physical address has been changed:

eth0      Link encap:Ethernet  HWaddr XX:XX:XX:XX:XX:XX

Take into account that what we are doing is to change the loaded address into memory, so, this is only temporary: if the device is rebooted, the real physical address will be loaded.

Changing the MAC address can be useful, for example, for privacy when connected to public networks, not to show the real physical address; or to bypass MAC security filters.

7. Promiscuous mode

The promiscuous mode is a mode where a device connected to a network captures all the traffic traveling through that network.

This is not as obvious as it can appear. If you have ever used a packet sniffer such us Wireshark, in a “simple” LAN (multiple devices connected to a router, for instance) you may have successfully sniffed other devices’ packets. But, if you have used it in a switched network (not hubed), you will probably have noticed that is not that easy to sniff packets. This is because the switches forward the frames only to the devices that need that frames, instead of sending broadcasts.

To put an interface (wlan0, for example) in promiscuous mode, we only have to type:

sudo ifconfig wlan0 promisc

The ifconfig output will say if an interface is in promiscuous mode:

wlan0     Link encap:Ethernet  HWaddr 64:27:37:b3:59:2d  
          inet addr:192.168.1.154  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::6627:37ff:feb3:592d/64 Scope:Link
          UP BROADCAST RUNNING PROMISC MULTICAST  MTU:1500  Metric:1

To put the interface in normal mode, we have to enter:

sudo ifconfig wlan0 -promisc

8. ARP mode

ARP acronym stands for Address Resolution Protocol, a network layer protocol, used for finding physical addresses corresponding to certain IPs, in order to populate ARP tables, where the IPs and MAC addresses are stored in key-value format.

The use of this protocol can cause some trouble, including DDOS attacks or spoofing. So, in some cases may be wanted to disable it.

The mechanism to enable/disable the ARP mode is the same as with promiscuous mode:

sudo ifconfig wlan0 arp  # Enables ARP for the wlan0 interface.
sudo ifconfig wlan0 -apr # Disables ARP for the wlan0 interface.

9. Broadcast and multicast modes

As same as with ARP mode, we may be interested in disabling broadcast and/or multicast to avoid possible floods.

Again, the mechanism is the same:

sudo ifconfig wlan0 -multicast # Disables multicast for wlan0 interface.
sudo ifconfig wlan0 -broadcast # Disables broadcast for wlan0 interface.
sudo ifconfig wlan0 multicast  # Enables multicast for wlan0 interface.
sudo ifconfig wlan0 broadcast  # Enables broadcast for wlan0 interface.

10. IP tunneling

The IP tunnel creation is a feature of ifconfig that must be mentioned.

An IP tunnel is a network communication channel where the data frames are transformed to pass networks without a routing path, or even incompatible protocols. The most common uses of IP tunnels are for creating Virtual Private Networks (along with IPSec procotol), and to connect IPv4 installations with IPv6 ones.

First, we have to create the IP tunnel, which will be like another network interface, with ip command:

sudo ip tunnel add tun0 mode ipip remote <remote_ip> local <local_ip> dev <device>

Let’s see it carefully:

  • tun0 is be the name the tunnel will have.
  • ipip is the tunnel mode. The IPIP kind tunnels are the simplest ones: they can only handle IPv4 multicast frames. The other option is GRE, which is the one used when tunneling routing protocols such us OSPF.
  • The remote and local IPs would be the two endpoints of the tunnel.
  • The device is the interface of the local IP, where the tunnel will be created.

So, for creating a tunnel between X.X.X.X and Y.Y.Y.Y IPs, we could do:

sudo ip tunnel add tun0 mode ipip remote X.X.X.X local Y.Y.Y.Y dev wlan0

This will create a tun0 interface. Now, we have to assign an IP address to the interface:

sudo ifconfig tun0 172.0.0.1 netmask 255.255.255.252 pointopoint 172.0.0.2 dev eth0

The pointopoint option is for enabling the point-to-point mode, i.e., a direct link between two devices.

We can see the tunnel configuration as any other network device, typing ifconfig tun0:

tun0      Link encap:IPIP Tunnel  HWaddr   
          inet addr:172.0.0.1  P-t-P:172.0.0.2  Mask:255.255.255.252
          UP POINTOPOINT RUNNING NOARP  MTU:1480  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

Note that the MTU is set to 1480. Let’s change it to ethernet default value, 1500:

sudo ifconfig tun0 mtu 1500

Finally, we have to bring up the interface:

sudo ifconfig tun0 up

And that’s it. To delete that tunnel, we would just have to type:

sudo ip tunnel del tun0

11. Summary

This tutorial has shown how to use the essential ifconfig command, from showing the most basic information about the network interfaces and changing IP addresses, to advanced topics such as creating IP tunnels or changing MAC addresses and packets’ maximum sizes.

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Gabriel A. Cánepa
Gabriel A. Cánepa
8 years ago

Who is using ifconfig these days? It has long been deprecated in favor of the ip suite and only maintained for backwards compatibility. It is not even present in CentOS 7 OOTB.

Back to top button