Displaying private IP addresses
You can determine the IP address or addresses of your Linux system by using the hostname
, ifconfig
, or ip
commands. To display the IP addresses using the hostname
command, use the -I
option. In this example the IP address is 192.168.122.236.
$ hostname -I
192.168.122.236
The ifconfig
command can also be used to display the IP addresses being used by the system. By default, ifconfig
will display information on all the network interfaces that are currently up, including the loopback interface.
$ /sbin/ifconfig
eth0 Link encap:Ethernet HWaddr 52:54:00:43:84:6c
inet addr:192.168.122.236 Bcast:192.168.122.255 Mask:255.255.255.0
inet6 addr: fe80::5054:ff:fe43:846c/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:304863 errors:0 dropped:0 overruns:0 frame:0
TX packets:129646 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:53349734 (53.3 MB) TX bytes:130802964 (130.8 MB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:2059079 errors:0 dropped:0 overruns:0 frame:0
TX packets:2059079 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:383294965 (383.2 MB) TX bytes:383294965 (383.2 MB)
To gather information on just one interface, supply that interface as an argument to ifconfig
. You can even take this a step further and extract the line that contains the IP address or even extract the IP address itself.
$ /sbin/ifconfig eth0 | grep -i mask
inet addr:192.168.122.236 Bcast:192.168.122.255 Mask:255.255.255.0
$ /sbin/ifconfig eth0 | grep -i mask | awk '{print $2}'| cut -f2 -d:
192.168.122.236
Let’s do the same thing with the ip
command. To view IP addresses, use ip addr show
. Like ifconfig
, you can specify an interface.
$ ip addr show
1: lo: <loopback,up,lower_up> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <broadcast,multicast,up,lower_up> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 52:54:00:43:84:6c brd ff:ff:ff:ff:ff:ff
inet 192.168.122.236/24 brd 192.168.122.255 scope global eth0
inet6 fe80::5054:ff:fe43:846c/64 scope link
valid_lft forever preferred_lft forever
$ ip addr show eth0
2: eth0: <broadcast,multicast,up,lower_up> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 52:54:00:43:84:6c brd ff:ff:ff:ff:ff:ff
inet 192.168.122.236/24 brd 192.168.122.255 scope global eth0
inet6 fe80::5054:ff:fe43:846c/64 scope link
valid_lft forever preferred_lft forever
$ ip addr show eth0 | grep 'inet ' | awk '{print $2}' | cut -f1 -d'/'
192.168.122.236
</broadcast,multicast,up,lower_up></broadcast,multicast,up,lower_up></loopback,up,lower_up>
Displaying the public IP address
If you want to know the public IP address of a Linux server, you can send an HTTP request to one of the following web servers.
- http://ifconfig.me
- http://www.icanhazip.com
- http://ipecho.net/plain
- http://ident.me
- https://myip.dnsomatic.com
- https://checkip.amazonaws.com
- http://whatismyip.akamai.com
- https://myipv4.p1.opendns.com/get_my_ip
- https://ipinfo.io/ip
- https://ip.tyk.nu
- https://l2.io/ip
- https://wtfismyip.com/text
- https://ifconfig.co
- https://api.ipify.org
- https://ipapi.co/ip
If the Linux system is connected directly to the internet, the public and private IP addresses will be the same. However, in most cases they differ. Here is an example using the curl
command.
$ curl ifconfig.me
216.239.32.10
$ curl icanhazip.com
216.239.32.10
$ curl ipecho.net/plain
216.239.32.10
$ curl ident.me
216.239.32.10
$ curl https://myip.dnsomatic.com
216.239.32.10
$ curl https://checkip.amazonaws.com
216.239.32.10
$ curl http://whatismyip.akamai.com
216.239.32.10
$ curl https://ipinfo.io/ip
216.239.32.10
$ curl https://ip.tyk.nu
216.239.32.10
$ curl https://l2.io/ip
216.239.32.10
$ curl https://wtfismyip.com/text
216.239.32.10
$ curl https://ifconfig.co
216.239.32.10
$ curl https://api.ipify.org
216.239.32.10
$ curl https://ipapi.co/ip
216.239.32.10
You can also achieve the same result with wget
by enabling quiet mode with -q
and sending the output to standard output (STDOUT) with -O-
.
$ wget -qO- ifconfig.me
216.239.32.10
There’s yet another way to determine your public IP address. Perform a DNS lookup against myip.opendns.com. It’s a service provided by OpenDNS and it’s lightening fast!
$ dig +short myip.opendns.com @resolver1.opendns.com
216.239.32.10
Here are some similarly fast alternatives:
$ dig +short ANY whoami.akamai.net @ns1-1.akamaitech.net
216.239.32.10
$ dig +short ANY o-o.myaddr.l.google.com @ns1.google.com
216.239.32.10