Linux ifconfig Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we will explore the Linux ifconfig command and learn how to use it to configure and manage network interfaces on a Linux system. We will start by understanding the purpose of the ifconfig command, which is used to display network interface information, configure network settings, and troubleshoot network-related issues. Then, we will dive into the basic syntax and available options of the ifconfig command, allowing us to effectively interact with and control our network interfaces. Finally, we will put our knowledge into practice by configuring network interfaces using the ifconfig command.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/RemoteAccessandNetworkingGroup(["`Remote Access and Networking`"]) linux/RemoteAccessandNetworkingGroup -.-> linux/ifconfig("`Network Configuring`") linux/RemoteAccessandNetworkingGroup -.-> linux/netstat("`Network Monitoring`") linux/RemoteAccessandNetworkingGroup -.-> linux/ip("`IP Managing`") subgraph Lab Skills linux/ifconfig -.-> lab-422730{{"`Linux ifconfig Command with Practical Examples`"}} linux/netstat -.-> lab-422730{{"`Linux ifconfig Command with Practical Examples`"}} linux/ip -.-> lab-422730{{"`Linux ifconfig Command with Practical Examples`"}} end

Understand the Purpose of the ifconfig Command

In this step, we will explore the purpose and importance of the ifconfig command in Linux. The ifconfig command is a powerful tool used to configure and manage network interfaces on a Linux system.

The primary purpose of the ifconfig command is to:

  1. Display Network Interface Information: The ifconfig command allows you to view the current status and configuration of your network interfaces, such as the IP address, subnet mask, and MAC address.

  2. Configure Network Interfaces: You can use ifconfig to configure various network interface settings, such as assigning an IP address, setting the subnet mask, and enabling or disabling an interface.

  3. Troubleshoot Network Issues: The ifconfig command can be helpful in troubleshooting network-related problems, as it provides detailed information about the network interfaces on your system.

Let's start by running the ifconfig command to see the current network interface configuration:

sudo ifconfig

Example output:

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.2  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)
        RX packets 8  bytes 648 (648.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8  bytes 648 (648.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

This output shows the configuration of the eth0 network interface, including its IP address, subnet mask, MAC address, and various packet statistics.

Explore the Basic Syntax and Options of ifconfig

In this step, we will dive deeper into the basic syntax and available options of the ifconfig command. Understanding the command's structure and options will help you effectively configure and manage your network interfaces.

The basic syntax of the ifconfig command is as follows:

sudo ifconfig [interface] [options]

Here's a breakdown of the common options you can use with ifconfig:

  1. Display Interface Information:

    • sudo ifconfig [interface]: Display the configuration details of the specified network interface.
    • sudo ifconfig: Display the configuration details of all network interfaces.
  2. Assign IP Address:

    • sudo ifconfig [interface] [IP_address] netmask [netmask]: Assign an IP address and subnet mask to the specified interface.
  3. Enable/Disable Interface:

    • sudo ifconfig [interface] up: Enable the specified network interface.
    • sudo ifconfig [interface] down: Disable the specified network interface.
  4. Set MTU (Maximum Transmission Unit):

    • sudo ifconfig [interface] mtu [value]: Set the MTU value for the specified interface.
  5. Set MAC Address:

    • sudo ifconfig [interface] hw ether [MAC_address]: Set the MAC address for the specified interface.

Let's try some of these options:

## Display information about the eth0 interface
sudo ifconfig eth0

Example output:

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.2  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)
        RX packets 8  bytes 648 (648.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8  bytes 648 (648.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
## Disable the eth0 interface
sudo ifconfig eth0 down
## Enable the eth0 interface
sudo ifconfig eth0 up
## Assign a new IP address and subnet mask to the eth0 interface
sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0

Configure Network Interfaces Using ifconfig

In this final step, we will learn how to configure network interfaces using the ifconfig command. This includes assigning IP addresses, setting the subnet mask, and enabling/disabling interfaces.

Let's start by creating a new network interface:

## Create a new virtual network interface
sudo ifconfig enp0s8 192.168.2.100 netmask 255.255.255.0 up

This command creates a new network interface named enp0s8 and assigns it the IP address 192.168.2.100 with a subnet mask of 255.255.255.0. The up option enables the interface.

You can verify the new interface configuration using the ifconfig command:

sudo ifconfig enp0s8

Example output:

enp0s8: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.2.100  netmask 255.255.255.0  broadcast 192.168.2.255
        ether 02:42:ac:11:00:03  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8  bytes 648 (648.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Now, let's disable the interface:

## Disable the enp0s8 interface
sudo ifconfig enp0s8 down

To re-enable the interface, use the up option:

## Enable the enp0s8 interface
sudo ifconfig enp0s8 up

You can also change the MAC address of an interface using the hw ether option:

## Change the MAC address of the enp0s8 interface
sudo ifconfig enp0s8 hw ether 00:11:22:33:44:55

Finally, let's remove the interface:

## Remove the enp0s8 interface
sudo ifconfig enp0s8 down
sudo ip link delete enp0s8

This command first disables the interface and then deletes it using the ip link delete command.

Summary

In this lab, we learned about the purpose and usage of the ifconfig command in Linux. The ifconfig command is a powerful tool used to configure and manage network interfaces on a Linux system. We explored its primary purposes, which include displaying network interface information, configuring network interfaces, and troubleshooting network issues. We also learned about the basic syntax and available options of the ifconfig command, which allow us to effectively manage our network interfaces.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like