This Linux quick tip will show you many different way to get your public IP address from the command line using different tools. Since not all Linux distributions have the same set of packages (programs) installed, some of these example may or may not work on your system. For example, default Red Hat and CentOS installations do not have the dig tool installed.
All of these options will depend on external sources. We will try to use as many different sources as possible in the examples to ensure reliability.
This website will tell you your IPv4 v6 addresses. Your IPv6 address is: None. Your IPv4 address is: None. Your IPv6 PTR is: None. Your IPv4 PTR is: None. The curl command transfers data to or from a network server, using one of the supported protocols (HTTP, HTTPS, FTP, FTPS, SCP, SFTP, TFTP, DICT, TELNET, LDAP or FILE). It is designed to work without user interaction, so it is ideal for use in a shell script. But I want to specify the IP manually, so I run curl -H 'Host: DOMAIN.TLD'. Now I get a cURL error: curl: (51) SSL: certificate subject name 'DOMAIN.TLD' does not match target host name 'IPADDRESS'. I can of course get around this by telling cURL not to care about the certificate (the '-k' option) but it's not ideal. Typically, curl will automatically extract the public key from the private key file, but in cases where curl does not have the proper library support, a matching public key file must be specified using the -pubkey option. You can change in /etc/hosts to make the server think that the domain is located at a certain IP. This is the syntax: 192.168.10.20 www.domain.tld This will make cURL use the IP-address you want without the SSL-certificate to break.
Using the curl Command
Curl is a tool used to transfer data to and from a server using many different supported protocols. Here we will use the HTTPS protocol to pull a webpage and grep to extract our public IP address. Here are some examples of how to get your public IP address from the command line using curl.
ipaddr.pub
The ipaddr.pub service can also provide additional information from the command line. You can find a complete list of options on the ipaddr.pub website.
ifconfig.io
WhatismyIP.com
Google.com
ipecho.net
akamai.com
Using the wget Command
Curl Whatsmyip
The wget command is a command line utility for non-interactive download of files from the web. It supports most HTTP, HTTPS, and FTP as well as connecting through a HTTP Proxy server. Here are some examples of how to get your public IP address from the command line using wget.
ipaddr.pub
ipecho.net
icanhazip.com
Using the dig Command
The dig command is a command line tool for querying DNS servers. This utility is not always available. If you want to install dig, it is usually packaged in bind-utils on Red Hat based distros and dnsutils on Debian based distros. Here are some examples of how to get your public IP address from the command line using dig.
google.com
opendns.com
Using the host Command
The host command is a simple command line utility for performing DNS queries. Here are some examples of how to get your public IP address from the command line using the host command.
opendns.com
Using the nslookup Command
The nslookup command is tool that queries DNS Servers, much like dig. This command is available on many operating systems including Linux, UNIX and Windows. Here are some examples of how to get your public IP address from the command line using nslookup.
google.com
opendns.com
Curl Up Test
Conclusion
There are many different ways to get your public IP address from the command line. Which you use will mostly depend on what is installed on your system. Our preferred method would be from a DNS server using the dig command, but as we stated, dig isn’t always available.