Find IP address in Linux is the task of retrieving local network configuration via CLI commands like ip addr, hostname -I, ifconfig, and ip route get.
ip addr show
Syntax
# Modern iproute2
to view all interfaces and addresses
ip addr show
# Show IPv4 addresses only (via awk filter)
ip addr show | awk '/inet / {print $2}'
# Legacy net-tools (may need installation)
ifconfig
# Hostname command (all non-loopback IPs)
hostname -I
# NetworkManager CLI
nmcli device show
# Primary IP via default route (source address)
ip route get 8.8.8.8 | awk '{print $NF}'
ip route get 1.2.3.4 | awk '{print $7}'
Tested on Ubuntu 22.04 with iproute2 5.15, net-tools 2.10, and hostname 3.23.
Usage Examples
1. Get the Primary (Default Route) IPv4 Address
ip route get 8.8.8.8 | awk '{print $NF}'
# Output example: 192.168.0.5
Queries the routing table for a path to 8.8.8.8 and extracts the source IP of the local interface. This returns the IP used for outbound traffic, typically the default interface primary address.
2. List All Assigned IP Addresses (excluding loopback)
hostname -I
# Output example: 172.23.120.4 10.42.0.0 10.42.0.1
Outputs a space‑separated list of all assigned IPs. Fast and scriptable. The order is not guaranteed across implementations.
3. Retrieve Public IP Address via External Resource
curl -s ifconfig.me
# Output example: 203.0.113.42
Queries an external service to obtain your public IP as seen by the internet. Requires outbound HTTP access. Alternative: curl -s icanhazip.com.
4. Get IPv4 Address of a Specific Interface
ip addr show eth0 | awk '/inet / {print $2}' | cut -d'/' -f1
# Output example: 172.23.120.4
Parses the output of ip addr for interface eth0, extracts the IPv4 address, and removes the subnet prefix. Useful when multiple interfaces exist and you need the address of a known device.
Troubleshooting & Common Errors
| Error Message / Symptom | Root Cause | Resolution Command |
|---|---|---|
ip: command not found or ifconfig: command not found |
Required package not installed (iproute2 or net-tools). | sudo apt install iproute2 or sudo apt install net-tools |
hostname -I returns empty |
No interface has an assigned IP (network down or DHCP failure). | ip link show to check interface state; then reconfigure network. |
RTNETLINK answers: Network is unreachable (from ip route get) |
No default route configured or destination not reachable. | Check routing table with ip route and add default gateway if needed. |
curl: (6) Could not resolve host: ifconfig.me |
DNS resolution failure (no external network or misconfigured DNS). | Check /etc/resolv.conf or test with nslookup. |
Multiple IPs from hostname -I – which one is “primary”? |
No standard ordering depends on implementation. | Use ip route get 1.1.1.1 | awk '{print $7}' to get the source address for outbound traffic. |
Frequently Asked Questions
What is the difference between hostname -I and ip addr show for finding local IP?
Answer: hostname -I prints all non-loopback IPs space-separated; ip addr show outputs structured interface details with subnet masks and flags.
hostname -I is concise but lacks context—ideal for quick checks. ip addr show (part of iproute2) provides per-interface info, essential for scripting and network diagnostics.
How do I fix “ip: command not found” on a Linux system?
Answer: Install the iproute2 package. Alternatively use legacy ifconfig from net-tools as a temporary fallback:
which ifconfig || sudo apt install net-tools
Does ip addr work on all Linux distributions?
Answer: Yes, ip addr is part of iproute2, preinstalled on all modern distributions. For containerized or embedded environments without it, fall back to hostname -I or install iproute2.
What is the fastest way to get a machine’s public IP address from the command line?
Answer: Run curl -s ifconfig.me. Alternatives include curl -s icanhazip.com or dig +short myip.opendns.com @resolver1.opendns.com.

Command Line Expert & Software Engineer
Welcome! I’m Thomas Heinrich, a software engineer and system administrator with a deep passion for the Command Line Interface (CLI). With years of experience navigating the terminal, building backend architectures, and automating server deployments, I created this space to share practical, real-world terminal knowledge.
Whether you are a beginner taking your first steps in a Linux environment or a seasoned DevOps engineer looking to optimize your deployment scripts, you will find actionable solutions here. My goal is to help you ditch the mouse, speed up your workflow, and harness the full power of the command line.