Introduction
One of the most common ways to configure a network interface in Linux is using the ifconfig
command. The command comes preinstalled on many Linux distributions. However, certain distributions like CentOS 7 have deprecated the command and do not include it by default anymore.
In this tutorial, you will learn how to install and use ifconfig
on CentOS 7.
Prerequisites
- CentOS 7 installed
- Access to the command line/terminal window
- Access to root or user with sudo privileges
How to Install the ifconfig Command
The ifconfig
command is part of the net-tools
package available in the YUM repository.
1. Open the terminal window and update the repositories:
sudo yum update
2. Next, download and install net-tools using yum:
sudo yum install net-tools -y
The output confirms the installation is complete.
Note: Distributions that have deprecated ifconfig
encourage users to use the ip
command instead. To read more about ip
, refer to How to Use IP Command in Linux with Examples.
Using the ifconfig Command
The ifconfig
command is versatile. It can:
- Display information about the system’s network configuration.
- Enable or disable a network interface.
- Assign IP addresses, netmasks, and broadcasts to network interfaces.
- Change the MAC address.
- Change the maximum transmission unit (MTU).
- Create network interface aliases.
- Enable or disable promiscuous mode.
Display Network Configuration Information
When used without arguments, ifconfig
displays information about the current network interfaces.
The output shows all the essential information for the active network interfaces, including:
- the hardware MAC address
- IP addresses (inet)
- netmasks
- broadcast addresses
- and whether the interface is up or down
To see all the interfaces, including the inactive ones, add the -a
argument:
ifconfig -a
In the example below, the output shows two active and one inactive interface:
To see information about a specific interface, add the interface name to the command:
ifconfig [interface-name]
The output displays information for the specified interface:
Enable or Disable a Network Interface
Enable a network interface by using the following syntax:
sudo ifconfig [interface-name] up
The command does not produce any output. However, running ifconfig
shows that the list of active connections now includes enp0s8
:
The syntax for disabling an interface is:
sudo ifconfig [interface-name] down
Assign the IP Address, Netmask, and Broadcast
Assign an IP address with ifconfig
using the command:
sudo ifconfig [interface-name] [IP-address]
To assign a new netmask value, type:
sudo ifconfig [interface-name] netmask [netmask-value]
Assign a new broadcast by running:
sudo ifconfig [interface-name] broadcast [broadcast-value]
Run the ifconfig
command to verify the output displays the new values:
Alternatively, you can assign all the values in one line:
sudo ifconfig [interface-name] [IP-address] netmask [netmask-value] broadcast [broadcast-value]
Change the Network Interface MAC Address
The MAC address is a unique identifier of a device on a network. To change the MAC address of a network interface, type:
sudo ifconfig [interface-name] hw ether [MAC-address]
The output from the ifconfig
command shows the change in the MAC configuration:
Change the Network Interface MTU
The maximum transmission unit (MTU) is the largest size of a packet or a frame that can be sent over the network. The default MTU is 1500.
Increasing the MTU of the network increases the data transfer rate.
To change the MTU value with ifconfig
, use the following syntax:
sudo ifconfig [interface-name] mtu [MTU-value]
Check the ifconfig
output to confirm the change:
Create Network Interface Aliases
To associate more than one IP address with a single network interface, use IP aliases. The ifconfig
command allows aliases, with the condition that their IP addresses belong to the same netmask.
An alias name consists of the main interface name and the number of the alias, separated by a colon. To create an alias, type:
sudo ifconfig [alias-name] [alias-address]
Check whether the alias was created successfully with ifconfig
:
To remove an alias, run the following command:
sudo ifconfig [alias-name] down
Enable or Disable Promiscuous Mode
Promiscuous mode allows a network device to intercept and read in entirety each network packet that arrives. It is often used to monitor network activity.
Enable promiscuous mode by typing:
sudo ifconfig [interface-name] promisc
Check if promiscuous mode is active with ifconfig
:
To disable promiscuous mode, use the command:
sudo ifconfig [interface-name] -promisc
Note: For a complete list of ifconfig
commands, type man ifconfig
.
Conclusion
This article provided instructions for installing and using the ifconfig
command on CentOS. Furthermore, it presented a list of common ifconfig
commands for network interface management.