How to Find Your IP Address in Linux: Private & Public Methods
Learn multiple command-line techniques to retrieve both private and public IP addresses on Linux, covering ip, ifconfig, hostname, dig, curl, and wget with practical examples for internal network ranges and external service queries.
When troubleshooting network issues, setting up new connections, or configuring firewalls, knowing your device's IP address is essential. IP addresses fall into two categories: public and private. Public IPs are globally unique and accessible from the internet, while private IPs are reserved for internal networks and not directly exposed to the internet. Additionally, there are two IP versions: IPv4 and IPv6.
Finding Your Private IP Address
Private IP addresses are not routable over the internet and only work within a local network. Typically, a router assigns private IPs to each device on the local network (phones, laptops, smart TVs, printers, etc.). Devices connect to the internet via NAT (Network Address Translation).
The following IPv4 ranges are reserved for private networks:
10.0.0.0/8
172.16.0.0/12
192.168.0.0/16
You can determine your system's private IP by querying the network stack with commands such as ip, ifconfig, or hostname. The standard command for displaying and configuring network interfaces in Linux is ip.
To list all network interfaces and their associated IP addresses, run: ip addr The output shows the private IP address highlighted.
You can also display the private IP with:
hostname -I ifconfigFinding Your Public IP Address
A public IP address is a globally routable address assigned by your ISP to your network device (router, web server, mail server, etc.). Each device has a unique public IP. Determining your public IP involves contacting a remote server via HTTP/HTTPS or DNS and extracting the IP from the response.
On a desktop, the easiest way is to search "what is my IP" in a browser. On a headless Linux server or for scripting, you can use command-line tools like dig, curl, and wget.
Most DNS providers (e.g., OpenDNS, Google) allow you to query their servers for your public IP. You can use any of the following commands:
dig ANY +short @resolver2.opendns.com myip.opendns.com dig ANY +short @resolver2.opendns.com myip.opendns.com dig ANY +short @ns1-1.akamaitech.net ANY whoami.akamai.netMany online HTTP/HTTPS services also return your public IP. Examples:
curl -s http://tnx.nl/ip curl -s https://checkip.amazonaws.com curl -s api.infoip.io/ip curl -s ip.appspot.com wget -O - -q https://icanhazip.com/If any command fails, the online service may be experiencing issues.
You can create an alias in your ~/.bashrc or ~/.zshrc to avoid typing long commands. For example:
alias pubip='dig ANY +short @resolver2.opendns.com myip.opendns.com'Now, whenever you need your public IP, simply type pubip in your terminal.
Conclusion
We have shown several different commands and online services you can use to find your private and public IP addresses.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Linux Tech Enthusiast
Focused on sharing practical Linux technology content, covering Linux fundamentals, applications, tools, as well as databases, operating systems, network security, and other technical knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
