1. Overview

What’s an IP address? The Internet Protocol (IP) address is a label for identifying any hardware device in a network that uses the Internet Protocol for communication. These IP addresses are an integral part of all the communication messages between devices in any network.

This IP tagging is essential so that the routers and intermediate devices will route the message to the appropriate destination. In simple terms, an IP address is analogous to our inland pin-code used for routing our mails by the postal department.

In this article, let’s learn how to check the primary IP address of a Linux machine.

2. IP Address Types

Firstly, let’s learn about the IP address types. There are two IP address versions, IPv4 and IPv6. The IPv4 address is the most used IP addressing version currently. Its format is a series of four numbers ranging from 0 to 255, separated by periods. For example, 10.106.146.25 is an IPv4 address.

Though the IPv4 IP addresses are widely used, there’s a need for a bigger pool of addresses due to the proliferation of end network devices today.

IPv6 addresses try to mitigate the limited pool of IPv4 addresses by increasing the number of bits and including alphanumeric characters in the address. An IPv6 address consists of 128 bits, with 8 groups of hexadecimal numbers that are 16 bits in length, and separated by a colon. As an example, FE80:CD00:0000:0CDE:1257:0000:211E:729C is an IPv6 address.

Secondly, apart from the two IP versions, there are two broad types of IP addresses:

  • Private IP addresses (internal IP)
  • Public IP addresses (external IP)

Private IP addresses are for identifying a device within the local network. All the addresses in the following ranges are private IP addresses:

  • 10.0.0.0 – 10.255.255.255
  • 172.16.0.0 – 173.31.255.255
  • 192.168.0.0 – 192.168.255.255

Public IP addresses are for identifying a device on the internet. All the non-private addresses belong to the public address range.

Generally, an IP address can be either a static address or a dynamic address. A static IP address is simply an address that doesn’t change with time. On the other hand, the dynamic IP addresses change from time to time as they’re allocated by the DHCP server in the network.

3. How Many IP Addresses Can a System Have?

Now, let’s understand how any Linux system connects itself with the network. There’s special hardware called NIC (network interface card), which does this magic of connecting any device to the network. Network interface controller, network adapter, or LAN adapter are the other names for the NIC hardware.

A system can have one or more NICs for network connectivity. Each NIC will identify itself with a unique IP address within a network. In short, any system can have one or multiple IP addresses (due to many NICs attached).

Even if the system has many NICs associated, there will be only one primary IP address at any point in time.

4. Network Interface Overview

Firstly, what’s a network interface? A network interface is the software representation of the network interface card. The network interface is a medium to communicate with the network hardware (NIC card) to configure it and to get information regarding the hardware like IP address, MAC address, link status, and packet transfer. Let’s check the network interface details of our system using the ip utility:

$ ip address show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:d5:cd:03 brd ff:ff:ff:ff:ff:ff
    altname enp2s1
    inet 192.168.207.128/24 brd 192.168.207.255 scope global dynamic noprefixroute ens33
       valid_lft 1773sec preferred_lft 1773sec
    inet6 fe80::37c:eba3:2365:23b7/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

Here, we see that our system has two network interfaces, ens33 and lo (loopback interface), along with their details listed.

4.1. The Primary Network Interface

In a Linux system, there can be only one primary active connection to the network. As a result, only a single primary interface will be present. The kernel routing table will help us identify the primary interface.

As we already know, the primary interface is the one with the default route, which means the primary interface should be able to reach the default gateway. That is to say, the kernel routing table’s gateway entry should be linked to the primary active interface of the system.

Now, let’s use the route utility to check the kernel routing table:

$ route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         _gateway        0.0.0.0         UG    100    0        0 ens33
link-local      0.0.0.0         255.255.0.0     U     1000   0        0 ens33
192.168.207.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33

In the above snippet, we see that the ens33 interface is the one connected to the default gateway, hence, it’s the primary active interface of the system.

5. Checking the IP Address of the Primary Active Interface

Subsequently, after identifying the primary active interface, the final step is checking the primary IP address — that is, the IP address of the primary active interface. There are many Linux utilities to find the primary IP address:

In this section, let’s check all three utilities to find out the primary IP address of a Linux system.

5.1. The ip Utility

We’ve already learned about the ip utility to check the details of all network interfaces in the system. Meanwhile, let’s see how to get the primary IP address of the system using the same utility.

To get the primary IP address, we need details of the primary active interface. We can get details of one particular interface by providing the interface name as a parameter to the ip utility. In our system, we’ve already found that ens33 is our primary active interface. Now, let’s get all the details of our primary active interface:

$ ip address show dev ens33 
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:d5:cd:03 brd ff:ff:ff:ff:ff:ff
    altname enp2s1
    inet 192.168.207.128/24 brd 192.168.207.255 scope global dynamic noprefixroute ens33
       valid_lft 1086sec preferred_lft 1086sec
    inet6 fe80::37c:eba3:2365:23b7/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

In the above code snippet, using the ip utility, we got all the details like IP address, MAC address, and traffic details regarding the primary active interface, ens33.

Subsequently, to display only the primary IP address, we use the snipping tools like grep and awk over the fetched primary interface details:

$ ip address show dev ens33 | grep -w inet | awk '{print $2}'
192.168.207.128/24

Here, the ip utility displays the netmask along with the IP address.

In addition to the above method, we can find the primary IP address using the ip utility. The ip utility is capable of finding the route to any destination. We can use this feature to find the primary IP address because it’s the interface that uses the default route.

Let’s find out the route to reach any random IP address (1.1.1.1) using the ip utility:

$ ip route get 1.1.1.1
1.1.1.1 via 192.168.207.2 dev ens33 src 192.168.207.128 uid 1000 
    cache 

Here, the above snippet reveals that our system can reach 1.1.1.1 via the interface ens33, whose IP address is 192.168.207.128 — our primary IP address.

5.2. The ifconfig Utility

The ifconfig utility is another widely used network configuration tool. This legacy utility is deprecated in favor of the ip utility, yet many Linux distributions still support this utility. Similar to the ip utility, the user has to pass the primary active interface name as an argument:

$ ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.207.128  netmask 255.255.255.0  broadcast 192.168.207.255
        inet6 fe80::37c:eba3:2365:23b7  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:d5:cd:03  txqueuelen 1000  (Ethernet)
        RX packets 11265  bytes 11392205 (11.3 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2591  bytes 397297 (397.2 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Subsequently, to display only the primary IP address, we’ll use the snipping tools over the fetched primary interface details:

$ ifconfig ens33 | grep -w inet | awk '{print $2}'
192.168.207.128

5.3. The nmcli Utility

The nmcli utility is for configuring and displaying network settings on systems that have network-manager installed. It’s usually present in all modern-day Linux distributions. This utility will get details regarding all interfaces present in the system, so the user has to peruse and find the primary IP address of the primary active network interface:

$ nmcli 
ens33: connected to Wired connection 1
        "Intel 82545EM"
        ethernet (e1000), 00:0C:29:D5:CD:03, hw, mtu 1500
        ip4 default
        inet4 192.168.207.128/24
        route4 0.0.0.0/0
        route4 192.168.207.0/24
        route4 169.254.0.0/16
        inet6 fe80::37c:eba3:2365:23b7/64
        route6 fe80::/64

lo: unmanaged
        "lo"
        loopback (unknown), 00:00:00:00:00:00, sw, mtu 65536

DNS configuration:
        servers: 192.168.207.2
        domains: localdomain
        interface: ens33

Use "nmcli device show" to get complete information about known devices and
"nmcli connection show" to get an overview of active connection profiles.

Consult nmcli(1) and nmcli-examples(7) manual pages for complete usage details.

Finally, let’s peruse the ens33 section (our primary active interface) in the nmcli utility’s output to find our primary IPv4 address (192.168.207.128/24).

6. External IP Address

External and local IP addresses serve the same purpose of providing a system with a unique identification. The only difference between them is the scope. The local IP address is the identity of a system in the local network. On the contrary, the external IP address is the identity of a system across the entire internet. So, every system connected to the internet will have a local IP address (for its local identity) and an external IP address (for its global identity).

So far in this article, we’ve discussed many ways to check the system’s local IP address. To check the external IP address of a system:

$ curl ifconfig.me
220.158.158.132

Alternatively, we can check the external IP address using website services like ipchicken.com.

7. Conclusion

To sum up, we’ve learned what an IP address is, along with the different versions and types of IP addresses. We’ve also learned how to find the primary active interface and how to use the ifconfig, ip, and nmcli utilities to check the primary IP address of a Linux system.

Authors Bottom

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

Comments are closed on this article!