Top 5 Commands to Find IP address in Linux

These are the commands that can be used to find the private or public IP address in your interface(s).

If your IP is NAT’ed and want to find the public IP, refer to this tutorial –> Quick Tip: Get or Find your Public IP Address using curl

  1. ip a

Sample Output: (You can use “ip addr or “ip a”)

[root@localhost ~]# ip addr
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 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever

ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 52:54:00:df:cf:03 brd ff:ff:ff:ff:ff:ff
inet brd scope global noprefixroute ens3
valid_lft forever preferred_lft forever
inet6 fe80::704f:b774:7ae7:53cc/64 scope link noprefixroute
valid_lft forever preferred_lft forever

2. ifconfig

Sample Output: (ifconfig has been officially deprecated, but still can be use and still a favorite for old school server admins)

[root@localhost ~]# ifconfig
ens3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet netmask broadcast
inet6 fe80::704f:b774:7ae7:53cc prefixlen 64 scopeid 0x20<link>
ether 52:54:00:df:cf:03 txqueuelen 1000 (Ethernet)
RX packets 316882 bytes 428387985 (408.5 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 140212 bytes 17341534 (16.5 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

 3.  hostname -I

I = Display all network addresses of the host. This option enumerates all configured addresses on all network interfaces. The loopback interface and IPv6 link-local addresses are omitted.

Sample Output:
[root@localhost ~]# hostname -I

4. nmcli

Sample Output:

[root@localhost ~]# nmcli
ens3: connected to ens3
“Red Hat Virtio”
ethernet (virtio_net), 52:54:00:DF:CF:03, hw, mtu 1500
ip4 default
inet6 fe80::704f:b774:7ae7:53cc/64
route6 fe80::/64
route6 ff00::/8

You can use some options and combinations like
-p = Output is pretty. This causes nmcli to produce easily readable outputs for humans, i.e. values are aligned, headers are printed, etc.
“device show”  = to get complete information about known devices

nmcli -p device show

5.  ip route get [ADDRESS]

where:(ip route get command gets a single route to a destination)
ADDRESS = any valid IP address

Sample Output: ( If get e.g. 1, it will automatically means IP address]

[root@localhost ~]# ip route get via dev ens3 src uid 0

Note: The 7th output will be the IP address. You can add “awk ‘{print $7}’ to print only the IP address, something like this

Sample Output:

[root@localhost ~]# ip route get | awk ‘{print $7}’

About the author

Free Linux

View all posts

Leave a Reply