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.

Previous:Bash History Size

Next: None

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

Bash History Size

Publish Date:2025/03/22 Views:189 Category:OPERATING SYSTEM

In this article, we will learn about Bash history, its size, and how we can change our history size and handle limits. Before getting into our topic, let us first understand why we need history in Bash shell and how we can get it. Most deve

Find Current Folder Name in Bash

Publish Date:2025/03/22 Views:106 Category:OPERATING SYSTEM

Finding a directory is very easy through Bash scripting. But finding the exact directory folder name you are in right now is a bit complicated. This article will introduce three methods to find the folder name from this article directory. I

Changing Directories in Bash

Publish Date:2025/03/22 Views:75 Category:OPERATING SYSTEM

In this article, we will learn how to change directories in Bash. The term directory is used to refer to a folder. Because you frequently move between folders when using Bash and when using the Git version control system, it is essential to

Running Regular Expressions in Case Statements in Bash

Publish Date:2025/03/22 Views:75 Category:OPERATING SYSTEM

This article explores regular expressions, their basic syntax, and how to run them in Bash using case and if-else constructs. Introduction to Regular Expressions A regular expression, also called regex or regexp, is a sequence of characters

Sending a message to logged in users in Bash

Publish Date:2025/03/22 Views:168 Category:OPERATING SYSTEM

This article explores methods to send data to another logged in user in Bash. This article discusses methods to find active users and send them messages. Finding Online Users in Bash Before sending data to an online user, you must verify wh

Bash copy all files from remote directory

Publish Date:2025/03/22 Views:156 Category:OPERATING SYSTEM

This article describes methods for copying files between different hosts on a network. It also explores the scp command and how to use it to copy files from a remote location. scp command Secure Copy Protocol (SCP) is a secure copy network

Batch file to loop through files in subdirectories

Publish Date:2025/03/22 Views:96 Category:OPERATING SYSTEM

This article explains how we can write a batch script to loop through files in subdirectories. We will take an example to explain the concept. Batch file to loop through files in subdirectories Assume we have the directory structure shown b

Batch file to remove X characters from a file name

Publish Date:2025/03/22 Views:153 Category:OPERATING SYSTEM

This article explains how we can remove specific characters from the filename of a file using a batch script. We will cover several methods below to rename files on Windows. File Explorer Renaming on Windows File Explorer offers the least f

Declaring variables in batch script

Publish Date:2025/03/22 Views:65 Category:OPERATING SYSTEM

This article will demonstrate how to declare and define variables in a batch script. Declaring variables in batch script In Batch, you do not need to use any additional keywords to declare integer, float, double, or string type variables. T

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial