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.
The.nix commands curl and wget are useful for accessing URLs without resorting to a browser. Both commands allow you to transfer data from a network server, with curl being the more robust of the two. You could use either of them to automate downloads from various servers. Or to catch on the curl server response command, try $ wget –server-response url. To download something in the background, like large files, simply use $ wget -bcq full-url-to-file. This will download in a separate pid (process id), automatically resume on broken connection and quit when it finishes. 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.
Curl Curling Iron
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.
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.
Wget Curl Difference
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
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
Curl Wget Tar
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
Curl Wget Extension
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 = Curl_init()
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.