Friday, November 15, 2024
Google search engine
HomeGuest BlogsLinux ifconfig Command With Examples

Linux ifconfig Command With Examples

Introduction

The ifconfig command is a part of net-tools, a legacy Linux tool for configuring a network interface. Modern distributions use the IP command, which works in a similar manner.

Even though ifconfig has limited capabilities compared to IP, the command is still commonly used to configure a network interface in Linux.

This tutorial will teach you how to use the Linux ifconfig command through practical examples.

Linux ifconfig Command With ExamplesLinux ifconfig Command With Examples

Prerequisites

  • A Linux system.
  • Access to the terminal.
  • Sudo privileges when making updates to network interface settings.

Important: ifconfig used to come preinstalled on all major Linux distributions. However, the tool was deprecated due to lack of maintenance and support for IPv6. The IP command is the preferred alternative.

Linux ifconfig Command Syntax

The basic ifconfig syntax looks like this:

ifconfig [interface_name_optional] [arguments_optional]

The ifconfig command prints an output when executed without arguments or with the interface name only. If used with arguments, there is no output.

Linux ifconfig Command Options

The ifconfig command works with arguments to achieve different results. Below is a list of common ifconfig command options:

Option Description
-a Prints the configuration for all interfaces, not just the active ones.
-s Displays only a shortlist of all interfaces.
-v Prints a more detailed configuration for all interfaces.
interface_name Represents the interface’s name and is accompanied by a unit number. Prints the configuration for a specific interface.
down Deactivates the interface.
up Activates the interface.
[IP_address_number] Assigns an IP address to a specified interface.
netmask [address] Changes the network mask for a specific interface.
broadcast [address] Sets the broadcast address for a specific interface.
mtu [number] Sets the Maximum Transfer Unit (MTU) of an interface.
promisc Enables promiscuous mode in which the interface receives all packets.
[-]promisc Disables promiscuous mode.
arp Enables the ARP protocol on a specific interface.
[-]arp Disables the ARP protocol on a specific interface.
allmulti Enables the all-multicast mode in which the interface receives all multicast packets.
[-]allmulti Disables the all-multicast mode.

Linux ifconfig Command Examples

The ifconfig command manages network interface parameters. Hence, the command has extensive practical usage. The sections below list common examples.

Note: Note that sudo is not required for view operations. However, if you are modifying the current configuration, sudo privileges are required.

1. View Active Network Interface Settings

Run ifconfig with no arguments to print essential information about all active network interfaces:

ifconfig
ifconfig terminal outputifconfig terminal output

The output shows crucial data about the network interfaces, like:

  • The names of the active network interfaces. This system includes enp0s3 and lo (the loopback interface). Active network interfaces differ from system to system.
  • The hardware MAC address.
  • The IP address (inet), netmask, and broadcast address.
  • The MTU (Maximum Transmission Unit) value.

The same command prints a different output when run on a different Linux distribution. For instance, on CentOS ifconfig shows settings for the vibr0 interface as well.

ifconfig CentOS terminal outputifconfig CentOS terminal output

2. Display a Shortlist of Active Interfaces

The ifconfig command without any arguments prints a rather detailed output about all active interfaces.

Use the -s flag with ifconfig to display a concise summary of every active interface:

ifconfig -s
ifconfig -s terminal outputifconfig -s terminal output

3. Print the Verbose Output

As opposed to the -s flag, the verbose option (-v) prints a more detailed output. Depending on the system, the outcome is either the same as ifconfig without arguments or slightly more in-depth.

ifconfig -v
ifconfig -v terminal outputifconfig -v terminal output

In this case, the output is the same as when running ifconfig without arguments.

4. Show Every Network Interface Configuration

Without options, ifconfig displays active interfaces only. To get info about both active and inactive network interfaces, run ifconfig with the -a option:

ifconfig -a
ifconfig -a Ubuntu terminal outputifconfig -a Ubuntu terminal output

The system has no inactive networks, so the output is the same as without the -a argument. The output from CentOS, however, shows three active and one inactive interfaces:

ifconfig -a CentOS terminal outputifconfig -a CentOS terminal output

The output shows the vibr0-nic interface as an inactive one.

5. Get Information About a Specific Network Interface

To get information about a single specific network interface, use :

ifconfig [interface_name]

For instance, to display enp0s3‘s settings, execute:

ifconfig enp0s3
ifconfig enp0s3 terminal outputifconfig enp0s3 terminal output

6. Disable a Network Interface

An active network interface transmits and receives data. To deactivate a specific network interface, run the ifconfig command with the down flag and use sudo:

sudo ifconfig [interface_name] down

For example, to disable enp0s3 run:

sudo ifconfig enp0s3 down

The command doesn’t print any output. Run ifconfig without any arguments to verify that enp0s3 is not active anymore:

sudo ifconfig enp0s3 down verification terminal outputsudo ifconfig enp0s3 down verification terminal output

7. Enable a Network Interface

To reactivate a network interface, use the up flag with sudo. For example, to enable the enp0s3 interface run:

sudo ifconfig enp0s3 up

Verify that the enp0s3 interface is active again:

ifconfig
sudo ifconfig enp0s3 up verification terminal outputsudo ifconfig enp0s3 up verification terminal output

The output shows that both interfaces are now active and running.

8. View IP Addresses

List all IP addresses related to the interfaces on the system by piping ifconfig with grep.

ifconfig | grep inet
ifconfig | grep inet terminal outputifconfig | grep inet terminal output

The output prints all lines that contain IP addresses.

9. Assign an IP Address to an Interface

To define an IP address for a specific interface, run the ifconfig command with the interface name and the IP address.

sudo ifconfig [interface_name] [IP_address]

For instance, to assign 10.0.2.20 to enp0s3, run:

sudo ifconfig enp0s3 10.0.2.20

The command doesn’t have any output. Run ifconfig with no arguments to confirm the IP address change:

Assigning and verifying IP address with ifconfig terminal outputAssigning and verifying IP address with ifconfig terminal output

10. View Network Interface Masks

Each interface has its assigned netmask. A netmask (or a subnet mask) refers to ranges of IP addresses divided into classes ( A, B, and C being the most common). Each class belongs to a default subnet mask. For instance:

  • Class A IPs belong to the 255.0.0.0 netmask.
  • Class B networks belong to the 255.255.0.0 netmask.
  • Class C networks use the 255.255.255.0 netmask.

Note that the IP class doesn’t fit into its default netmask in some cases.

To list netmasks used by active network interfaces, pipe the results with grep:

ifconfig | grep netmask
ifconfig | grep netmask terminal outputifconfig | grep netmask terminal output

The output prints a list of IPs and netmasks they belong to. The netmask, in this case, is 255.0.0.0

11. Assign a Netmask to an Interface

To assign a netmask address to an interface, specify the interface and use the netmask option with sudo.

sudo ifconfig [interface_name] netmask [netmask_IP]

For instance, assign the netmask 255.255.255.0 to enp0s3:

sudo ifconfig enp0s3 netmask 255.255.255.0

Verify the results with:

ifconfig
Assigning and verifying netmask with ifconfig terminal outputAssigning and verifying netmask with ifconfig terminal output

The output shows that the netmask is changed to 255.255.255.0

12. View the Broadcast Address of Interfaces

The broadcast setting is the address that sends messages to all hosts in a network. It is assigned automatically by the system.

Use grep to list the broadcast addresses of the interfaces:

ifconfig | grep broadcast
ifconfig | grep netmask terminal outputifconfig | grep netmask terminal output

The command lists the lines in which the broadcast address is present.

13. Assign a New Broadcast IP to a Network Interface

To assign a new broadcast IP, specify the network interface and use the broadcast argument with sudo.

sudo ifconfig [interface_name] broadcast [broadcast_IP]

For example, assign 10.0.2.250 as the new broadcast IP to enp0s3 with:

sudo ifconfig enp0s3 broadcast 10.0.2.250

Since there is no output, verify the change by running ifconfig:

Assigning and verifying broadcast with ifconfig terminal outputAssigning and verifying broadcast with ifconfig terminal output

14. Configure Multiple Network Parameters with One Command

The IP address, netmask, and broadcast of an interface can all be modified with one command. The syntax is:

sudo ifconfig [interface_name] [IP] netmask [netmask_addresss] broadcast [broadcast_address]

For example, for enp0s3 we’ll set:

  • The IP to 10.0.2.15
  • The netmask to 255.0.0.0
  • The broadcast to 10.0.2.255
sudo ifconfig enp0s3 10.0.2.15 netmask 255.0.0.0 broadcast 10.0.2.255

Verify the results:

ifconfig
Assigning and verifying IP, netmask, and broadcast with ifconfig terminal outputAssigning and verifying IP, netmask, and broadcast with ifconfig terminal output

The output shows that all three parameters are changed.

15. Set MTU for an Interface

The MTU stands for the maximum transmission unit and allows users to limit the size of packets transmitted on an interface. Use the mtu argument with the ifconfig command, specify it’s value, and use sudo:

sudo ifconfig [interface_name] mtu [mtu_value]

For example, set the MTU for enp0s3 to 2000 with:

sudo ifconfig enp0s3 mtu 2000
setting MTU with ifconfig terminal outputsetting MTU with ifconfig terminal output

Note: Please note that not all network interfaces allow the MTU to be changed.

16. Set an Alias IP

The ifconfig command allows users to associate additional IP addresses with an interface using an IP alias.

Create an alias IP with the following command:

sudo ifconfig [interface_name]:[alias_number] [alias_IP]

Note that the alias IP must belong to the same netmask.

For example, the IP address for enp0s3 is 10.0.2.15. This is a type A IP and belongs to the 255.0.0.0 netmask. To create an alias IP under the 255.0.0.0 netmask, do not change the first two numbers. For instance, run the following command to create an alias enp0s3:0:

sudo ifconfig enp0s3:0 10.0.2.30

Verify the outcome:

ifconfig
Creating an alias with ifconfig terminal-outputCreating an alias with ifconfig terminal-output

The alias enp0s3:0 and the assigned IP are listed.

To remove an alias IP, run:

sudo ifconfig enp0s3:0 down

17. Enable Promiscuous Mode

Promiscuous mode allows a network device to accept all packets. The mode is often used to analyze network activity.

To enable promiscuous mode for a network interface, use the promisc argument:

sudo ifconfig [interface_name] promisc

For example,

Enabling the promiscuous mode terminal outputEnabling the promiscuous mode terminal output

18. Disable Promiscuous Mode

Disable promiscuous mode with the -promisc flag:

sudo ifconfig enp0s3 -promisc

Verify the results with ifconfig.

Disabling the promiscuous mode terminal outputDisabling the promiscuous mode terminal output

The PROMISC flag is not present in the output, meaning that the interface is in normal mode.

19. View Transmission Errors

Print any transmission errors in the interface by filtering the output with grep:

ifconfig | grep errors
ifconfig | grep errors terminal outputifconfig | grep errors terminal output

This command outputs lines with errors, which in this case is zero.

Conclusion

After reading this article, you know how to use ifconfig to configure network settings. While using the IP is still the popular choice, ifconfig does offer plenty of opportunities.

For more tutorials on ifconfig command, refer to our tutorial ifconfig command not found and learn how to fix this error.

Next, learn different ways to configure CentOS network settings.

Was this article helpful?
YesNo

RELATED ARTICLES

Most Popular

Recent Comments