JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Get the Primary IP Address in Linux

Author:JIYIK Last Updated:2025/03/22 Views:

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

This simple guide is all about using different commands that can be used to get the primary IP address in Linux operating system using Bash command.


What is an IP address

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

This IP tag is essential for routers and intermediary equipment to route the message to the correct location. The IP address can be compared to our inland pin number, which the postal service uses to route our mail.

Let us learn how to verify the primary IP address of a Linux machine in this article.

Types of IP Addresses

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

IPv4 and IPv6 are two IP address variations. The most common version of IP addressing currently 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.

Although IPv4 addresses are widely adopted, a larger address pool is currently needed due to the abundance of terminal network devices.

By adding more bits and using alphanumeric letters, IPv6 addresses attempt to make up for the small number of available IPv4 addresses. IPv6 addresses are 128 bits long and consist of 8 groups of 16-bit long hexadecimal numbers.

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

  1. Private IP Address (Internal IP): You can use a private IP address to identify a device on your local network.
  2. Public IP Address (External IP): A device on the Internet can be identified by its public IP address.

Finding the Primary IP Address

On a Linux system there can only be one primary active connection to the network. As a result there will be only one primary interface.

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 know, it should be able to communicate with the default gateway.

We can determine the primary interface using the route command. This command will display the kernel routing table, where we can see the primary active interface 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 throughout the rest of this tutorial to get the primary IP address.

There are four ways to find your IP address using the command line interface (CLI).


Get the primary IP address using ip command in Linux

We need information about the primary active interface to get the primary IP address. We can get specific information about an interface by passing the interface name as input to the ip command.

We have already identified wifi0 as the primary active interface in our system. Now let's examine various aspects of the primary active interface:

Using the ip command

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

The grep command helps to 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 see in the third line of the output, it shows that the second index shows the inet details of the IP address.

Use the ip command to obtain the primary address

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


Get the Primary IP Address using ifconfig Command in Linux

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

The primary active interface name must be given as input to the ip command:

ifconfig command to obtain IP address

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

ip address using ifconfig


Get the Primary IP Address in Linux using nmcli Command

The nmcli command is used to configure and display network settings on a computer that has a network manager installed. All current Linux distributions usually include it.

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

nmcli command

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

Use nmcli to get the primary IP address


Summarize

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

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

How to decompress x.tar.xz format files under Linux

Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM

A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr

Summary of vim common commands

Publish Date:2025/04/08 Views:115 Category:OPERATING SYSTEM

In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can reme

Detailed explanation of command return value $? in Linux

Publish Date:2025/04/08 Views:58 Category:OPERATING SYSTEM

? is a special variable. This variable represents the return value of the previous command. That is to say, when we run certain commands, these commands will return a code after running. Generally, if the command is successfully run, the re

Common judgment formulas for Linux script shell

Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM

In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –

How to use the Linux file remote copy command scp

Publish Date:2025/04/08 Views:151 Category:OPERATING SYSTEM

Scp copies files between two hosts over the network, and the data is encrypted during transmission. Its underlying layer uses ssh for data transmission. And it has the same authentication mechanism and the same security level as ssh. When u

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial