Check IP address in Ubuntu command line
How To

How to Check IP Address of Ubuntu System

Pratham Patel
Pratham Patel

Table of Contents

There can be multiple reasons to know the IP address of your computer. Ranging from pure curiosity to necessity as a system administrator.

The easiest way of finding out the IP address of your Ubuntu system is to use the hostname command like this:

hostname -I

This will give you IP addresses for all network interfaces (which are connected), except for the Loopback interface.

learn@ubuntu:~$ hostname -I
192.168.31.197 

There are other ways of knowing the IP address in Ubuntu Linux. There is a dedicated ip command also for this purpose.

Let me share the methods in detail.

Method 1: Show only IP address

If all you want to see is the private IP address of your computer, the most appropriate command for that is the hostname command.

Using the --all-ip-address option will show all the IP addresses if the network interface is connected. As a shorter alternative, you can use the -I option.

Below is the output of hostname command on my computer:

$ hostname -I
10.0.0.13 192.168.122.1

Now, I can see all the IP addresses that my computer is assigned. At the moment, I have two IP addresses that are assigned to my computer.

⚠️
The hostname command may not be available in all Linux distributions. If you are using it in a script, it's better to use the ip command mentioned next.

Show IP address along with more details

If like me, you got more than one IP address in the output of hostname command, you might be wondering "Which IP address belongs to which network interface?"

To know that, we have to use the ip command. The ip command gives us the details that we require.

To show all network interfaces, use the following command:

ip addr

Running the above command will give you details like MAC address, DHCP lifetime, IP address, name of interface, speed, and much more.

For demonstration purposes, let's see what the output looks like on my computer:

$ ip addr
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: enp5s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether b4:2e:99:98:03:4e brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.13/24 brd 10.0.0.255 scope global dynamic noprefixroute enp5s0
       valid_lft 78871sec preferred_lft 78871sec
    inet6 fe80::7495:f97d:6134:dc22/64 scope link noprefixroute
       valid_lft forever preferred_lft forever
3: virbr0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 52:54:00:a6:22:59 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever

The network interfaces are enumerated using numbers. In my case, I have the following network interfaces:

  • lo
  • enp5s0 (Ethernet)
  • virbr0 (KVM network bridge)

The two important network interfaces are my Ethernet and my KVM network bridge.

To know which IP address is assigned to which network interface, under the interface name, look at the line that says 'inet' and/or 'inet6'.

The keyword 'inet' represents things to do with your Internet Protocol v4 (IPv4). Similarly, 'inet6' represents things to do with IPv6.

Let's assume I want to see what my IPv4 address for Ethernet is. To do that, I will look at the line that says 'inet' under the 'enp5s0' interface. That gives me '10.0.0.13'.

So, I can say that the IP address '10.0.0.13' belongs to the Ethernet interface on my computer.

UptimeRobot: Free Website Monitoring Service
Start monitoring in 30 seconds. Use advanced SSL, keyword and cron monitoring. Get notified by email, SMS, Slack and more. Get 50 monitors for FREE!

Get public IP address (for system's behind NAT)

I hope you know the difference between private and public IP addresses. Most personal computers these days have a private IP (usually starting like 192.168.X.X) behind the router.

You may check the public IP address (of your network/router) using external methods.

Method 1: Using cURL

The easiest method is to curl a website curated to give you the IP it sees while you connect to it. Which is basically your public IP address.

You may have to install curl command in Ubuntu first and then use one of the following:

curl https://icanhazip.com
curl https://ipinfo.io/ip
curl https://ifconfig.me
🙌
My ISP does not provide IPv6 (yet), so I can not confirm if this method also shows your public IPv6 address.

Method 2: dig out your IP address (IPv4 and IPv6)

Usually, the dig command is used to lookup DNS. But you can also use it to see your public IP address.

Below is the command to check your IP address using dig command:

dig +short myip.opendns.com @resolver1.opendns.com

In case you have IPv6 enabled and want to see it, use the following command:

dig -6 TXT +short o-o.myaddr.l.google.com @ns1.google.com

That's it. Now you know how to get your IP address in Ubuntu command line.