Jeffimgcls Hi, I'm Jeff! Resume Linkedin Mail

How To Find Your Public IP Address From Linux CLI

A public IP address is an address that can be accessed over the Internet. Public IP addresses are provided by Internet Service Providers (ISPs) and are globally unique. These addresses are used to identify and locate devices on the Internet.

On the other hand, a private IP address is an address assigned to devices within a private network, such as a home or business network. Private IP addresses are not accessible from the Internet. Instead, they are used for communication within the private network and are typically assigned by a router.

For example, in a typical home network setup, the devices connected to the local network (like smartphones, computers, printers) are assigned private IP addresses (e.g., 192.168.x.x). The router in the home network then uses its public IP address, provided by the ISP, to communicate with devices on the Internet.

There are instances where you may need to determine what the public IP address is that your machine is using and you can use these commands from the CLI to figure it out.

dig +short myip.opendns.com @resolver1.opendns.com
dig TXT +short o-o.myaddr.l.google.com @ns1.google.com
dig +short txt ch whoami.cloudflare @1.0.0.1
host myip.opendns.com resolver1.opendns.com
dig TXT +short o-o.myaddr.l.google.com @ns1.google.com | awk -F'"' '{ print $2}'
curl checkip.amazonaws.com
curl ifconfig.me
curl icanhazip.com
curl ipecho.net/plain
curl ifconfig.co
server_ip="$(curl ifconfig.co)"
printf "Server public ip4 %s\n" $server_ip