DEV Community

dpm_bush
dpm_bush

Posted on Originally published at sshflow.com

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 addresses, run:

hostname -I
Enter fullscreen mode Exit fullscreen mode

It prints addresses on one line, without showing which interface each belongs to. That’s handy on a machine with one active network connection, but less helpful when several addresses appear.

For a compact list that associates addresses with interfaces, use:

ip -br addr show
Enter fullscreen mode Exit fullscreen mode

To show only IPv4 addresses:

ip -4 -br addr show
Enter fullscreen mode Exit fullscreen mode

A result might look like this:

lo               UNKNOWN        127.0.0.1/8
eth0             UP             192.168.1.42/24
Enter fullscreen mode Exit fullscreen mode

Here, lo is loopback, and eth0 has the address 192.168.1.42. The /24 is the network prefix length, not part of the address you normally type into an SSH client.

If you want a complete view—including interface state, address scope, and IPv6—run:

ip addr show
Enter fullscreen mode Exit fullscreen mode

Each inet line is an IPv4 address; each inet6 line is IPv6. For help identifying unfamiliar names like enp0s3, docker0, or wg0, this overview of Linux network interfaces explains how to inspect them.

Ask Linux which address it would use

An interface listing tells you what addresses exist. It doesn’t always tell you which one Linux will use for a particular connection.

Ask the kernel to look up a route to a destination:

ip route get 8.8.8.8
Enter fullscreen mode Exit fullscreen mode

Example output:

8.8.8.8 via 192.168.1.1 dev eth0 src 192.168.1.42 uid 1000
Enter fullscreen mode Exit fullscreen mode

The src value is the source IP Linux would use to reach that destination; dev shows the outgoing interface. This command checks the routing table—it does not send traffic to 8.8.8.8.

The result depends on the destination. A VPN, multiple network connections, or policy routing can mean Linux chooses a different source address for another destination. So treat the output as “the address for this route,” not necessarily the one address for every purpose.

Recognize addresses you probably don’t want

When scanning ip addr output, these common cases help narrow things down:

  • 127.0.0.1 is loopback: the machine talking to itself. It isn’t an address for another device to connect to.
  • 192.168.0.0/16, 10.0.0.0/8, and 172.16.0.0/12 are private IPv4 ranges, often used on a LAN or in a cloud VPC.
  • 172.17.0.0/16 is commonly used by Docker’s default bridge network. Check the interface name before mistaking it for the host’s LAN address.
  • IPv6 addresses beginning with fe80:: are link-local. They’re limited to the local network segment, not generally routable across networks.

For IPv6, look for an inet6 address with scope global when you need a routable address. A scope link address is link-local.

Need the public IP instead?

Commands such as ip addr show addresses configured on your Linux machine. They usually cannot show the public address used by your router or cloud network when traffic goes through NAT.

To ask an external service what public IP your request comes from, use:

curl ifconfig.me
Enter fullscreen mode Exit fullscreen mode

Or:

curl ipinfo.io/ip
Enter fullscreen mode Exit fullscreen mode

That address may be shared by multiple devices or change over time. Finding it doesn’t automatically make a server reachable from the internet: routing, firewalls, and NAT still matter. If that’s the goal, see how port forwarding makes a private-network service reachable.

What about ifconfig?

Older tutorials often use ifconfig, but it belongs to the net-tools package and may not be installed. For current Linux networking tasks, prefer ip addr or ip -br addr show; they provide the interface and address information you need without relying on the legacy command.

A quick decision guide

  • Show all addresses quickly: hostname -I
  • See addresses by interface: ip -br addr show
  • Get a readable IPv4-only list: ip -4 -br addr show
  • Inspect full interface details: ip addr show
  • Find the source address for a route: ip route get 8.8.8.8
  • Check the public IP seen by an external service: curl ifconfig.me

For a single-interface machine, hostname -I is often enough. When there are several candidates, ip -br addr show helps identify where each belongs, and ip route get answers which source address Linux would use for a specific destination.

I originally published a more detailed version of this guide on the SSHFlow blog.

I'm also building SSHFlow — an SSH client where every server gets its own workspace for terminals, SFTP, code, and databases.

Top comments (0)