9 Methods to check your public IP address in Linux command line

IP address stands for Internet Protocol Address and each devices are assigned with specified IP Address to identify the device which are connected on internet. Public IP addresses are provided by an ISP Internet Service Provider which are accessible over the Internet.

There are multiple online websites are available to determine website & server public IP address via browser but when you have a headless server, what will be the solution? We have to look for command line methods.

In this tutorial we are going to show you, 9 Method to determine your Public IP addresses from the command line with help of below commands

  • dig command,
  • hostname command,
  • host command,
  • nslookup command,
  • ip addr command,
  • ifconfig command,
  • /etc/hosts,
  • ping command.

Method-1 : Using Hostname Command

Hostname is the program that is used either to set or display the current host, domain or node name of the system. These names are used by many of the networking programs to identify the machine. The domain name is also used by NIS/YP.

# hostname -I

Method-2 : Using /etc/hosts file

hosts /etc/hosts is an operating system file that maps host names to IP addresses before DNS can be referenced. It is a plain text file, one line per IP address. It’s a static table lookup for host names.

# cat /etc/hosts   server.2daygeek.com	server       localhost       server.2daygeek.com	server

Method-3 : Using Dig Command

dig stands for domain information groper is a flexible tool for interrogating DNS name servers. It performs DNS lookups and displays the answers that are returned from the name server(s) that were queried. Most DNS administrators use dig to troubleshoot DNS problems because of its flexibility, ease of use and clarity of output.

Suggested Read : Check DNS (Domain Name Server) Records On Linux Using Dig Command

# dig server.2daygeek.com

; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.62.rc1.el6_9.4 <<>> server.2daygeek.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 39766
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 2, ADDITIONAL: 2

;server.2daygeek.com.          IN      A

server.2daygeek.com.   850     IN      A

vps.ovh.ca.             1101    IN      NS      dns10.ovh.ca.
vps.ovh.ca.             1101    IN      NS      ns10.ovh.ca.

ns10.ovh.ca.            2665    IN      A
dns10.ovh.ca.           1004    IN      A

;; Query time: 0 msec
;; WHEN: Thu Jul 20 19:27:32 2017
;; MSG SIZE  rcvd: 125

To print only Ip address instead of full information, use the dig command with following format.

# dig server.2daygeek.com +noall +answer

; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.62.rc1.el6_9.4 <<>> server.2daygeek.com +noall +answer
;; global options: +cmd
server.2daygeek.com.   1097    IN      A

Method-4 : Using Host Command

host is a simple utility for performing DNS lookups. It is normally used to convert names to IP addresses and vice versa. When no arguments or options are given, host prints a short summary of its command line arguments and options.

Suggested Read : Check DNS (Domain Name Server) Records On Linux Using Host Command

# host server.2daygeek.com
server.2daygeek.com has address

To print only IP address instead of full information, use the host command with following format.

# host server.2daygeek.com | awk '{print $4}' | head -1

Method-5 : Using Nslookup Command

Nslookup is a program to query Internet domain name servers. Nslookup has two modes interactive and non-interactive. Interactive mode allows the user to query name servers for information about various hosts and domains or to print a list of hosts in a domain. Non-interactive mode is used to print just the name and requested information for a host or domain.

Suggested Read : Check DNS (Domain Name Server) Records On Linux Using Nslookup Command

# nslookup server.2daygeek.com

Non-authoritative answer:
Name:   server.2daygeek.com

To print only Ip address instead of full information, use the nslookup command with following format.

# nslookup server.2daygeek.com | grep Address | awk '{print $2}' | sed '1d'

Method-6 : Using IP Command

IP command is similar to ifconfig, which is very familiar for assigning Static IP Address, Route & Default Gateway, etc.,. ifconfig command was deprecated due to no maintenance since so many years, even though it is still available on most Linux distributions. ifconfig command has been replaced by IP command which is very powerful and performing several network administration tasks with one command.

Suggested Read : How To Use IP (Replacement For ifconfig) Command on Linux

# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 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 fa:16:3e:16:b8:2a brd ff:ff:ff:ff:ff:ff
    inet brd scope global eth0
    inet6 fe80::f816:3eff:fe16:b82a/64 scope link
       valid_lft forever preferred_lft forever

To print only IP address instead of full information, use the IP command with following format.

# ip addr show eth0 | grep 'inet ' | awk '{print $2}' | cut -f1 -d'/'

Method-7 : Using ifconfig Command

Ifconfig is used to configure the kernel-resident network interfaces. It is used at boot time to set up interfaces as necessary. After that, it is usually only needed when debugging or when system tuning is needed. If no arguments are given, ifconfig displays the status of the currently active interfaces.

# ifconfig
eth0      Link encap:Ethernet  HWaddr FA:16:3E:16:B8:2A
          inet addr:  Bcast:  Mask:
          inet6 addr: fe80::f816:3eff:fe16:b82a/64 Scope:Link
          RX packets:21002 errors:0 dropped:0 overruns:0 frame:0
          TX packets:15500 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:15092437 (14.3 MiB)  TX bytes:3024676 (2.8 MiB)

lo        Link encap:Local Loopback
          inet addr:  Mask:
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:6 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:298 (298.0 b)  TX bytes:298 (298.0 b)

To print only Ip address instead of full information, use the ifconfig command with following format.

# ifconfig eth0 | grep Mask | awk '{print $2}'| cut -f2 -d:

Method-8 : Using ping Command

Ping command uses the ICMP protocol’s mandatory ECHO_REQUEST datagram to elicit an ICMP ECHO_RESPONSE from a host or gateway. ECHO_REQUEST datagrams (‘‘pings’’) have an IP and ICMP header, followed by a struct timeval and then an arbitrary number of ‘‘pad’’ bytes used to fill out the packet.

# ping server.2daygeek.com -c 4
PING server.2daygeek.com ( 56(84) bytes of data.
64 bytes from server.2daygeek.com ( icmp_seq=1 ttl=64 time=0.022 ms
64 bytes from server.2daygeek.com ( icmp_seq=2 ttl=64 time=0.036 ms
64 bytes from server.2daygeek.com ( icmp_seq=3 ttl=64 time=0.026 ms
64 bytes from server.2daygeek.com ( icmp_seq=4 ttl=64 time=0.026 ms

--- server.2daygeek.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 2999ms
rtt min/avg/max/mdev = 0.022/0.027/0.036/0.007 ms

Method-9 : Using various website

With help of cURL, wget, and lynx commands, we can get the server public IP address by sending http request to following websites.

Suggested Read :
(#) curl – Command Line Download Manager Examples
(#) Wget (Command Line Downloader) Command Examples

# curl ifconfig.me

# wget -qO- http://ipecho.net/plain ; echo

# lynx bot.whatismyipaddress.com

Commands: Use arrow keys to move, '?' for help, 'q' to quit, '<-' to go back.
  Arrow keys: Up and Down to move.  Right to follow a link; Left to go back.
 H)elp O)ptions P)rint G)o M)ain screen Q)uit /=search [delete]=history list

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

3 Comments on “9 Methods to check your public IP address in Linux command line”

  1. It’s hard to find your articles in google. I found it on 20 spot, you should build quality backlinks ,
    it will help you to increase traffic. I know how to help you,
    just search in google – k2 seo tricks

Leave a Reply

Your email address will not be published. Required fields are marked *