Skip to main content
SysAdmin Shell Scripting Essentials

Find IP Address in Linux – CLI Commands Reference

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.

See also  Linux mv Command Reference – Syntax, Exit Codes, Troubleshooting

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

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.