How to Retrieve Your IP Address Using Linux Commands
Knowing how to find your IP address is a fundamental skill for anyone using Linux — whether you’re troubleshooting network issues, configuring a server, setting up firewall rules, or working with self-hosted services.
In this article, we’ll walk through multiple reliable ways to retrieve your IP address using Linux commands, explain what each command does, and clarify when to use each method.
Understanding IP addresses (quick overview)
Before diving into commands, it helps to understand that you may have more than one IP address:
-
Local (private) IP address
Assigned to your device on your local network (LAN) -
Public IP address
Assigned by your ISP and visible to the internet
Linux provides tools to retrieve both, depending on what you need.
Finding your local (private) IP address
Method 1: Using ip addr (recommended)
The ip command is the modern and preferred networking tool on Linux.
ip addr
Look for an interface such as eth0, ens18, wlan0, or similar. Your IP address appears next to inet.
Example output:
inet 192.168.1.42/24
This means your local IP address is:
192.168.1.42
Method 2: Short and clean output
If you only want the IP without extra information:
ip -4 addr show | grep inet
This limits the output to IPv4 addresses and removes unnecessary noise.
Method 3: Using hostname -I
A very simple and fast method:
hostname -I
Example output:
192.168.1.42
This command is ideal for scripts or quick checks.
Method 4: Using ifconfig (legacy systems)
Some older systems still include ifconfig:
Look for inet under your active interface.
⚠️ Note: ifconfig is deprecated on many modern distributions and may not be installed by default.
Finding your public (external) IP address
Your public IP address is the one visible to websites and remote servers.
- Method 1: Using curl (most common)
curl ifconfig.meExample output:
203.0.113.45
- Method 2: Using alternative services
If one service is unavailable, try:
curl ipinfo.io/ip
or
curl api.ipify.org
All return your public IP as plain text.
- Method 3: Using wget
If curl isn’t installed:
wget -qO- ifconfig.me
Determining which interface is active
To see which network interface is currently in use:
ip route
Example output:
default via 192.168.1.1 dev eth0
This tells you:
your gateway (192.168.1.1)
the active interface (eth0)
IPv4 vs IPv6 addresses
Many Linux systems support both.
View only IPv4 addresses:
ip -4 addr
View only IPv6 addresses:
ip -6 addr
Public IPv6 addresses can also be retrieved with:
curl -6 ifconfig.me
Common use cases
Knowing your IP address is useful for:
-
Configuring firewalls (UFW, iptables, nftables)
-
Setting up SSH access
-
Debugging Docker or container networking
-
Configuring reverse proxies
-
Troubleshooting VPN connections
-
Verifying ISP changes or dynamic IPs
Security note
Never casually share your public IP address in public forums unless you understand the risk.
While an IP alone isn’t a password, it can expose:
-
approximate location
-
ISP details
-
attack surface for scanning
Summary
Linux gives you multiple ways to retrieve your IP address, depending on your needs:
ip addr → most detailed and reliable
hostname -I → fast and simple
curl ifconfig.me → public IP
ip route → active interface insight
Understanding these tools gives you better control over your system and your network.
Mastering small commands like these is how Linux users build real confidence — one terminal at a time.