How to Get the Primary IP Address in Linux

Naila Saad Siddiqui Feb 02, 2024
  1. What is IP Address
  2. Find the Primary IP Address
  3. Get the Primary IP Address in Linux Using the ip Command
  4. Get the Primary IP Address in Linux Using the ifconfig Command
  5. Get the Primary IP Address in Linux Using the nmcli Command
  6. Conclusion
How to Get the Primary IP Address in Linux

There are multiple ways to get the network details in Linux. We will learn some of them in this article.

This trivial guide is about the use of different commands that can be used to get the primary IP address in the Linux operating system using Bash commands.

What is IP Address

Any hardware component in a network that employs the Internet Protocol can be identified by its Internet Protocol (IP) address. All messages sent between devices in a communication network contain these IP addresses as a necessary component.

This IP tagging is crucial for the routers and intermediate devices to route the message to the correct location. An IP address can be compared to our inland pin code, which the postal service uses to route our mail.

Let’s find out in this article how to verify a Linux machine’s primary IP address.

Types of IP Address

Let’s first examine the many categories of IP addresses.

IPv4 and IPv6 are the two IP address variants. The most common IP addressing version at the moment is IPv4.

Four numbers from 0 to 255, separated by periods, make up its format. An example of an IPv4 address is 10.106.146.25.

Despite the widespread adoption of IPv4 addresses, there is a current demand for a larger pool of addresses due to the abundance of end network devices.

Adding more bits and using alphanumeric letters, IPv6 addresses attempt to compensate for the small number of IPv4 addresses available. An IPv6 address has a length of 128 bits and is made up of 8 groups of 16-bit long hexadecimal digits.

In addition to the two IP versions, there are also two main categories of IP addresses:

  1. Private IP Addresses (Internal IP): Devices on the local network can be identified using private IP addresses.
  2. Public IP Addresses (External IP): A device on the internet can be identified by its public IP address.

Find the Primary IP Address

There can only be one primary active connection to the network on a Linux system. There will only be one primary interface as a result.

We can locate the main interface with the help of the kernel routing table.

Since the primary interface is the one with the default route, as we already know, it should be able to communicate with the default gateway.

We can use the route command to determine the primary interface. This command will show the Kernel routing table in which we can see our primary active interfaces that are connected to the default gateway:

kernel routing table

In this table, we can see that the device name that is the primary active device is wifi0. We will use this device name for the rest of the tutorial to get the primary IP address.

Four methods exist to find the IP address using the command line interface (CLI).

Get the Primary IP Address in Linux Using the ip Command

We require information about the primary active interface to obtain the primary IP address. We can obtain specific information about a certain interface by passing the interface name as an input to the ip command.

We have already identified wifi0 as the main active interface in our system. Let’s now examine every aspect of our main active interface:

using ip command

We can see from the output above that it shows the complete network information of the device wifi0. We can combine this command with the grep command to get only the IP address.

The grep command helps extract the inet address from this data. Then, we will use the awk command to get the second index of the inet details, as you can observe in the third line of the output, which shows the inet details that the second index shows the IP address.

get primary address using ip command

In this way, we can extract the primary IP address of our active interface on the network.

Get the Primary IP Address in Linux Using the ifconfig Command

Another frequently used tool for network configuration is the ifconfig command. Although the ip command has replaced this outdated utility, several Linux distributions still support it.

The primary active interface name must be given as an input, like the ip command:

ifconfig command to get ip address

This also shows the same device information. We can extract the IP address only using grep and awk commands, just like in the previous example.

ip address using ifconfig

Get the Primary IP Address in Linux Using the nmcli Command

The nmcli command is used on computers with a network manager installed to configure and display network settings. All current Linux distributions often include it.

This command will provide information on all of the system’s interfaces. Therefore, the user must search and identify the primary IP address of the main active network interface:

nmcli command

This shows the network details of all the devices. To show the IP address of the currently active interface, we will use the following command:

get primary ip address using nmcli

Conclusion

In conclusion, we now understand what an IP address is and the various versions and types of IP addresses. Additionally, we learned how to get the primary IP address of a Linux system using the ifconfig, ip, and nmcli commands and identify the primary active interface.