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 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 to either 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
192.99.246.33

Method-2 : Using /etc/hosts file

hosts /etc/hosts is an operating system file that maps hostnames 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 hostnames.

# cat /etc/hosts
192.99.246.33   server.2daygeek.com	server
127.0.0.1       localhost
127.0.0.1       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

;; QUESTION SECTION:
;server.2daygeek.com.          IN      A

;; ANSWER SECTION:
server.2daygeek.com.   850     IN      A       192.99.246.33

;; AUTHORITY SECTION:
vps.ovh.ca.             1101    IN      NS      dns10.ovh.ca.
vps.ovh.ca.             1101    IN      NS      ns10.ovh.ca.

;; ADDITIONAL SECTION:
ns10.ovh.ca.            2665    IN      A       198.245.48.5
dns10.ovh.ca.           1004    IN      A       8.33.137.161

;; Query time: 0 msec
;; SERVER: 213.186.33.99#53(213.186.33.99)
;; 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       192.99.246.33

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 192.99.246.33

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
192.99.246.33

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
Server:         213.186.33.99
Address:        213.186.33.99#53

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

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

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:  mtu 65536 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:  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 192.99.246.33/32 brd 192.99.246.33 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'/'
192.99.246.33

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:192.99.246.33  Bcast:192.99.246.33  Mask:255.255.255.255
          inet6 addr: fe80::f816:3eff:fe16:b82a/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          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:127.0.0.1  Mask:255.0.0.0
          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:
192.99.246.33

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 (192.99.246.33) 56(84) bytes of data.
64 bytes from server.2daygeek.com (192.99.246.33): icmp_seq=1 ttl=64 time=0.022 ms
64 bytes from server.2daygeek.com (192.99.246.33): icmp_seq=2 ttl=64 time=0.036 ms
64 bytes from server.2daygeek.com (192.99.246.33): icmp_seq=3 ttl=64 time=0.026 ms
64 bytes from server.2daygeek.com (192.99.246.33): 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
192.99.246.33

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

# lynx bot.whatismyipaddress.com
192.99.246.33

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

Magesh Maruthamuthu

Love to play with all Linux distribution

You may also like...

Shares
Close
Please support the site
By clicking any of these buttons you help our site to get better