How to Use the ifconfig Command in Linux

The ifconfig command is a Linux tool used to configure and manage network interfaces. It enables viewing interface status, assigning IP addresses, setting netmasks, and controlling hardware addresses to assist in network setup and debugging.

Using the ifconfig command, you can do the following:

  • Management and configuration of network interfaces
  • Access to network configuration information
  • Hardware address setting
  • Netmask or broadcast address on a network interface
  • Initialization of network interfaces
  • Enable and disable network interfaces
  • IP address settings
  • Viewing the status of network interfaces

When using a Linux VPS, the ifconfig command is often helpful for managing network interfaces when setting up, debugging, and configuring a Linux system. Therefore, getting to know this command and how to use the ifconfig command will make it easier for you to manage and configure the network interface. Be with us to learn how to use the ifconfig command in Linux systems.

🤖AI Overview:

The ifconfig Command serves as a key network management utility in Unix and Linux systems. It is primarily used to configure network interfaces, assign IP addresses, and enable or disable connections. Despite being deprecated on many modern Linux distributions, it remains valuable for legacy system management and troubleshooting.

Installing  ifconfig

Newer Linux distributions may not support the ifconfig tool by default because the ifconfig command was recently deprecated and ip is used as its replacement.

Displaying the message “ifconfig: command not found” after executing the ifconfig command means that your system does not have the package containing the command, so you must install the package containing the ifconfig command on the system.

  • Installing ifconfig on Ubuntu/Debian

If you want to install ifconfig on Ubuntu/Debian-based distributions, enter the following command:

sudo apt install net-tools –y
  • Installing ifconfig on CentOS

If you want to install ifconfig on RHEL and CentOS-based distributions, run the following command:

sudo dnf install net-tools –y

How to use the ifconfig command

The main syntax of the ifconfigh command is as follows:

ifconfig [-a] [-v] [-s] <interface> [[<AF>] <address>]

Interface: Place the name of the network interface in this field.

Address: Specify the IP address you intend to assign.

Note: you must have root or sudo user privileges to configure network interfaces. Remember that the settings you make using the ifconfig command are unstable and will return to the initial state when the system is restarted. so if you want your changes to be saved, you need to add commands to the startup script or make changes to the distribution-specific configuration files.

  • Display the general settings of the network interface

If you run the “ifconfig” command without arguments, it will provide complete information about the active network interfaces, and you can also check the IP address associated with a server using the “ifconfig” command.

ifconfig

Output:

eth0      Link encap:Ethernet  HWaddr 00:0B:CD:1C:18:5A
          inet addr: 172.18.26.123   Bcast: 172.18.26.63   Mask:255.255.255.224
          inet6 addr: fe80::20b:cdff:fe1c:185a/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:2341604 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2217673 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:293460932 (279.8 MiB)  TX bytes:1042006549 (993.7 MiB)
          Interrupt:185 Memory:f7fe0000-f7ff0000
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:16436  Metric:1
          RX packets:5019066 errors:0 dropped:0 overruns:0 frame:0
          TX packets:5019066 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:2174522634 (2.0 GiB)  TX bytes:2174522634 (2.0 GiB)
tun0      Link encap:UNSPEC  HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
          inet addr:10.1.1.1  P-t-P:10.1.1.2  Mask:255.255.255.255
          UP POINTOPOINT RUNNING NOARP 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:100
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)
  • Display network interface information

To access complete information about network interfaces, both active and inactive on the server, invoke the “ifconfig” command with the -a argument:

ifconfig –a

Output:

eth0      Link encap:Ethernet  HWaddr 00:0B:CD:1C:18:5A
          inet addr:172.18.26.123  Bcast:172.18.26.63  Mask:255.255.255.224
          inet6 addr: fe80::20b:cdff:fe1c:185a/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:2344927 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2220777 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:293839516 (280.2 MiB)  TX bytes:1043722206 (995.3 MiB)
          Interrupt:185 Memory:f7fe0000-f7ff0000
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:16436  Metric:1
          RX packets:5022927 errors:0 dropped:0 overruns:0 frame:0
          TX packets:5022927 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:2175739488 (2.0 GiB)  TX bytes:2175739488 (2.0 GiB)
sit0      Link encap:IPv6-in-IPv4
          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)
tun0      Link encap:UNSPEC  HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
          inet addr:10.1.1.1  P-t-P:10.1.1.2  Mask:255.255.255.255
          UP POINTOPOINT RUNNING NOARP 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:100
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)
  • View the network settings of the specified interface

To have the output of the “ifconfig” command provide configuration information for each network interface, run the “ifconfig” command with the interface name (eg, eth0) as an argument:

ifconfig eth0

Output:

eth0      Link encap:Ethernet  HWaddr 00:0B:CD:1C:18:5A
          inet addr: 172.18.26.123   Bcast: 172.18.26.63   Mask:255.255.255.224
          inet6 addr: fe80::20b:cdff:fe1c:185a/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:2345583 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2221421 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:293912265 (280.2 MiB)  TX bytes:1044100408 (995.7 MiB)
          Interrupt:185 Memory:f7fe0000-f7ff0000
  • Enable and disable the network interface

Next to the name of the specified network interface, if it is inactive, execute flag “up” or “ifup”, the network interface will be activated and the activation of the network interface will enable data transfer. For example, running the following commands will enable the eth0 interface:

ifconfig eth0 up

Or

ifup eth0

And to disable the specified network interface, if the specified interface is active, you must execute the “down” or “ifdown” flag next to the interface name (For example,eth0) as follows :

ifconfig eth0 down

Or

ifdown eth0

In the command we executed, the network interface eth0 is considered as an example.

  • Enable and disable Promiscuous mode

To allow a network interface to access all packets on the network, you must enable promiscuous mode. Promiscuous mode is very efficient for today’s network tools in analyzing the packets flowing through the network interface. The ifconfig command can enable and disable promiscuous mode on a specific network device.

To enable promiscuous mode for a network interface (for example, eth0), run the following command:

ifconfig eth0 promisc

Use the “-promisc” flag to return the specified network interface to normal mode and disable promiscuous mode:

ifconfig eth0 -promisc
  • Assigning an IP address and Netmask to the Network Interface

One of the functions of the ifconfig command is to assign an IP address and Netmask to a specific network interface.

You can assign the IP address and netmask to a specific network interface using the following syntax:

ifconfig [interface-name] [ip-address] netmask [subnet-mask]

For example, to assign an IP address (172.18.26.124) to the network interface (eth0), enter the following command :

ifconfig eth0 172.18.26.124

Or to define the netmask for a specific interface (eth0), consider the following pattern:

ifconfig eth0 netmask 255.255.255.224
  • Assigning a Broadcast to the network interface

To set a broadcast address for a specific network interface (for example, eth0) use the following pattern:

ifconfig eth0 broadcast 172.18.26.63   

If you want to assign an IP Address, Netmask and Broadcast to a network interface, use the following example:

ifconfig eth0172.18.26.124 netmask 255.255.255.224 broadcast 172.18.26.63  
  • Changing the MTU for a network interface

MTU, which stands for Maximum Transmission Unit, is used to limit the size of packets that are sent on a network interface, and also manages the maximum number of octets to an interface in a transaction.

To change the MTU value, use the following syntax:

ifconfig [interface-name] mtu [mtu-value]

For example, if you want to set the MTU value of the eth0 network interface to a specific set (for example, 1000), use the following command:

ifconfig eth0 mtu 1000

Note that it is not possible to set the MTU value for all network interfaces.

  • Changing the MAC address of the network interface

Media Access Control, or MAC for short, is a unique physical address that is assigned to devices connected to the network and identifies devices connected to the network. To change the MAC address to the new address of a network interface, run the following command:

ifconfig eth0 hw ether AA:BB:CC:DD:EE:FF
  • Add and Remove New Alias to the network interface

Using the ifconfig command, you can set additional network interfaces with the Alias feature.

For example, to add the alias network interface of eth0, run the following command:

ifconfig eth0:0 [alias ip address]

Note: the network address of the alias is in the subnet mask.

Then use the “ifconfig eth0:0 down” command to verify the address of the new alias network interface you created.

To remove the network interface alias, if it is not needed, run the following command:

ifconfig eth0:0 down

Conclusion

The ifconfig Command is a fundamental Linux networking tool that enables developers and administrators to view and configure network interface parameters efficiently. It provides essential functionalities such as viewing interface details, managing IP addressing, controlling interface states, and monitoring traffic statistics.

This guide has elaborated on the ifconfig Command’s syntax, principal options, practical examples, and best practices, offering developers a thorough foundation for managing network interfaces competently. By applying these insights, developers can ensure reliable network configurations, streamlined troubleshooting, and maintain robust Linux-based network systems.

FAQ

On Ubuntu or Debian systems, you can install ifconfig by running the "sudo apt install net-tools -y". This installs the package containing the ifconfig utility.

If you see the "ifconfig: command not found" error, it means the net-tools package is not installed. You need to install net-tools using the appropriate package manager for your distribution.

Use "ifconfig [interface] [ip-address] netmask [subnet-mask]". For example, to assign IP 192.168.1.10 with netmask 255.255.255.0 to eth0, run "ifconfig eth0 192.168.1.10 netmask 255.255.255.0".

To enable an interface, use "ifconfig [interface] up". To disable it, use "ifconfig [interface] down". For example, "ifconfig eth0 up" activates the eth0 interface.

Promiscuous mode allows a network interface to receive all network packets on the network. Enable it using "ifconfig [interface] promisc", and disable it with "ifconfig [interface] -promisc".

Use "ifconfig [interface] hw ether [new-mac-address]". For example, "ifconfig eth0 hw ether AA:BB:CC:DD:EE:FF" changes eth0's MAC address.

To create an alias, use "ifconfig [interface]:0 [alias-ip]". To remove an alias, use "ifconfig [interface]:0 down". Aliases allow multiple IPs on one physical interface.

Yes, running ifconfig with the -a option shows all interfaces, active and inactive: "ifconfig -a".

Use the syntax "ifconfig [interface] mtu [mtu-value]". For example, to set MTU 1500 on eth0 use "ifconfig eth0 mtu 1500". This sets the maximum transmission unit size for packets.

Leave a Reply

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