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 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

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 192.168.15.22/29 brd 192.168.15.23 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 192.168.15.22 netmask 255.255.255.248 broadcast 192.168.15.23
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

where:
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
192.168.15.22

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
inet4 192.168.15.22/29
route4 192.168.15.16/29
route4 0.0.0.0/0
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 1.0.0.0 IP address]

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

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 1.1.1.1 | awk ‘{print $7}’

192.168.15.22

About the author

Free Linux

View all posts

Leave a Reply