Find the Right IP Address on Linux (Without Guessing)
If hostname -I prints three addresses and you’re not sure which one to use, the problem isn’t finding an IP—it’s identifying what each address is for. Linux can have several addresses at once: loopback, a LAN or cloud address, a VPN address, and addresses on Docker or virtual-machine interfaces. Here’s how to find the one you need. Start with the quick answer To list the machine’s configured IP…
When troubleshooting IP addresses on a Linux system without resorting to guesswork, it's essential to understand the various types of addresses that may be configured. The hostname -I command provides a quick overview of all configured IP addresses but doesn't distinguish between interfaces. For a more detailed breakdown, use ip -br addr show to list IPv4 addresses alongside their respective interfaces.
If you only need IPv4 addresses, utilize ip -4 -br addr show. The output will display loopback addresses like lo with the address 127.0.0.1/8, followed by interface-specific addresses such as eth0 with 192.168.1.42/24. The "/24" indicates the network prefix length and doesn't form part of the actual address used in SSH connections.
To gain a comprehensive view of all interface details including state, scope, and IPv6 information, run ip addr show. If unfamiliar names like enp0s3, docker0, or wg0 appear, refer to an extensive guide on Linux network interfaces to interpret them correctly. While ip route get 8.8.8.8 reveals which address Linux would use to reach a specific destination, remember it only pertains to that route and not necessarily every situation.
Be cautious of private IPv4 ranges like 192.168.0.0/16, 10.0.0.0/8, and 172.16.0.0/12, often utilized within local networks or cloud environments. Also, note that IPv6 addresses starting with fe80:: are link-local and cannot be routed across different networks. Determining the public IP accessible from the internet requires external means.
Services like curl ifconfig.me or curl ipinfo.io/ip can disclose the public IP address used by your Linux machine, although it may be subject to NAT or shared among multiple devices. While ifconfig, a legacy tool, is sometimes used in older tutorials, it's advisable to opt for ip addr or ip -br addr show for current Linux networking tasks, as they offer the necessary interface and address information without relying on outdated commands.
Ultimately, the decision guide suggests using hostname -I for a quick display of all IP addresses, ip -br addr show to see addresses associated with specific interfaces, ip -4 -br addr show for a compact IPv4-only list, ip addr show to inspect full details of each interface, ip route get 8.8.8.8 to ascertain the source address Linux would employ for a particular route, and curl ifconfig.me to uncover the public IP visible externally.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.