Every network administrator and developer encounters the need to verify a machine's public-facing address. Whether you are debugging a connection issue, setting up a port forward, or confirming a dynamic DNS configuration, knowing how to get public IP from command line is an essential skill. The process is straightforward, and the tools are readily available on almost every operating system.
Understanding Public vs. Private Addresses
Before diving into the commands, it is important to understand what a public IP address represents. Your router uses a private IP address, such as 192.168.1.10, to communicate with devices inside your local network. The public IP is the unique identifier assigned by your Internet Service Provider (ISP) that allows your router to communicate with the rest of the internet. The command you run locally only sees the internal network interface, so to retrieve the public address, you must query an external server that sees your traffic exiting the router.
Using cURL with Web Services
The most common method involves using cURL to query a dedicated IP echo service. These lightweight websites return the IP address they receive in the request headers, effectively showing you what the internet sees. This method works on Linux, macOS, and Windows Command Prompt if cURL is installed.
Basic cURL Command
Open your terminal or command line interface and run the following command:
curl ifconfig.me
This command contacts the ifconfig.me server and returns just the IP address with no extra formatting, making it ideal for scripts. Other popular endpoints include ipinfo.io/ip and checkip.amazonaws.com , which serve the same purpose with slightly different backend data.
Formatting the Output
Sometimes you need the IP address embedded in a script or want to suppress progress meters that cURL adds by default. You can refine the command to make the output cleaner and more suitable for automation.
Silent and Clean Output
To remove the progress bar and error messages, adding the -s (silent) flag is standard practice. Here are a few robust examples:
curl -s ifconfig.me
curl -s ipinfo.io/ip
curl -s checkip.amazonaws.com
These commands return the address as a plain string, which is perfect for storing in a variable or logging to a file without manual cleanup.
Alternative Tools: wget and Dig
If cURL is not available on your system, you can often use wget to achieve the same result. This tool is standard on many Unix-like systems and can scrape the text content of a page.
Using wget
To use wget, you direct the output to standard output and quieten the verbose output:
wget -qO- ifconfig.me
In this command, -q means quiet, and -O- tells wget to send the output to the standard console. For users working with DNS utilities, dig can also be used to query specific text records provided by certain IP services, though this method is less common for simple checks.
PowerShell on Windows
Windows users who prefer a native solution can utilize PowerShell without installing third-party software. The Invoke-WebRequest cmdlet functions similarly to cURL and can fetch the external IP directly.
Native Windows Commands
To retrieve your public IP address in a Windows environment, use the following command: