How to Get the IP Address on Linux: Local and Public IP Commands

Finding a server’s IP address is one of the first things you do after logging into a Linux machine—whether you’re configuring a firewall, setting up DNS, troubleshooting connectivity, or pointing a domain at your box. The challenge is that “the IP address” is rarely a single number. A Linux system can have a local (private) IP assigned to its network interface and a separate public IP that the rest of the internet actually sees. Knowing which command reveals which address saves you hours of confusion.

This guide walks through the modern tools (`ip a`, `hostname -I`), the legacy ones you’ll still encounter (`ifconfig`), and the external-query trick for finding your true public IP from inside a server.

Key Takeaways
• Use `ip a` (or `ip addr show`) to list every interface and its assigned local IP—this is the modern standard.
`hostname -I` prints your local IP addresses quickly, with no parsing required.
• On a cloud/VPS server, `ip a` usually shows a private/internal IP, not your public one. Query an external service with `curl ifconfig.me` to see the address the internet sees.
• `ifconfig` still works on many systems but is deprecated—prefer the `ip` suite.
• A private IP (like `192.168.x.x` or `10.x.x.x`) identifies your machine on a local network; a public IP is globally routable.

What Is the Modern Command to Find a Linux IP Address?

The modern, recommended tool is `ip`, part of the `iproute2` package that ships with virtually every current Linux distribution. To list all interfaces and their addresses:

“`bash ip a

ip addr show “`

A typical output looks like this:

“` 2: eth0: mtu 1500 … inet 192.168.1.42/24 brd 192.168.1.255 scope global eth0 inet6 fe80::a00:27ff:fe4e:66a1/64 scope link “`

The value after `inet` (here `192.168.1.42`) is your IPv4 address for that interface. The `inet6` line shows the IPv6 address. The `/24` is the subnet prefix length.

How Do I Show Only the IPv4 Address?

If the full dump is noisy, filter by address family:

“`bash ip -4 a # IPv4 addresses only ip -6 a # IPv6 addresses only ip -br a # brief, one-line-per-interface summary “`

The `-br` (brief) flag is especially handy on servers with several interfaces—it collapses each one to a single readable row.

How Do I Find the Primary Interface IP?

When a machine has multiple interfaces and you want the IP the system uses for outbound traffic, query the route to an external address:

“`bash ip route get 1.1.1.1 “`

The output includes `src 192.168.1.42`, which is the source IP the kernel would use to reach the internet. This is the most reliable way to identify your *primary* local address programmatically, because it reflects actual routing decisions rather than just listing every configured address.

How Do I Get My Local IP Address Quickly?

For a fast answer without reading through interface details, use `hostname`:

“`bash hostname -I “`

This prints all assigned local IP addresses on one line, separated by spaces—for example `192.168.1.42 10.8.0.3`. The flag is a capital `I` (for “all IP addresses”). It’s perfect for scripts or a quick glance, though note it can return multiple addresses if you have VPNs, Docker bridges, or several NICs.

The trap most people hit: on a cloud or VPS server, `ip a` and `hostname -I` very often show a *private* IP (something like `10.0.0.5` or `172.31.x.x`), not the public address you connect to over SSH. That’s because most cloud instances sit behind NAT—the provider maps a public IP to your instance’s private interface at the network edge, and the operating system genuinely doesn’t “know” its own public address. The interface only ever sees the private side. If you trust `ip a` blindly on such a box, you’ll hand out an unroutable IP and wonder why nothing reaches it. The fix is to ask an outside observer, covered next.

How Do I Find My Public IP From a Linux Server?

Because the server itself often only sees its private/NAT IP, you find the public IP by querying an external service that reports the address it received your request from:

“`bash curl ifconfig.me curl -4 ifconfig.me # force IPv4 if you have dual-stack curl ipinfo.io/ip curl icanhazip.com curl api.ipify.org “`

Each returns a single line: the public IP the internet associates with your outbound connection. You can also use DNS instead of HTTP, which is fast and avoids any web dependency:

“`bash dig +short myip.opendns.com @resolver1.opendns.com “`

This asks OpenDNS’s resolver “what IP did this query come from?” and returns it directly. If `dig` isn’t installed, the `curl` options above achieve the same result.

When Do Private and Public IPs Differ?

Scenario `ip a` shows Public IP (`curl ifconfig.me`)
Home/office machine behind a router Private (`192.168.x.x`) Your ISP’s shared public IP
Cloud VPS behind NAT Private (`10.x` / `172.31.x`) The assigned floating/elastic IP
Bare-metal / dedicated server with direct IP The public IP itself Same public IP
VPN-connected host VPN tunnel IP + real IP The VPN exit node’s IP

On a dedicated server or VPS configured with a directly attached public IP, the two can match—but you should never assume that. Always verify.

Which Commands Should I Know? A Quick Reference

Command What it shows Notes
`ip a` / `ip addr show` All interfaces and their local IPs Modern standard
`ip -4 a` IPv4 addresses only Filters noise
`ip -6 a` IPv6 addresses only For dual-stack hosts
`ip -br a` One-line brief per interface Readable summary
`ip route get 1.1.1.1` Primary outbound source IP Reflects routing
`hostname -I` All local IPs on one line Quick, script-friendly
`ifconfig` Interface addresses (legacy) Deprecated; `net-tools`
`curl ifconfig.me` Public IP (external view) Needs internet
`curl ipinfo.io/ip` Public IP Alternative service
`dig +short myip.opendns.com @resolver1.opendns.com` Public IP via DNS No HTTP needed

Is ifconfig Still Usable?

You’ll still see `ifconfig` in tutorials and on older systems. It belongs to the legacy `net-tools` package and is deprecated in favor of the `ip` suite, but it remains functional where installed:

“`bash ifconfig # all interfaces ifconfig eth0 # one specific interface “`

The address appears on the `inet` line, just like `ip a`. On minimal modern images, `ifconfig` may not be present at all—you’d install it with `sudo apt install net-tools` (Debian/Ubuntu) or `sudo dnf install net-tools` (RHEL/Fedora). For new work, learn `ip`; treat `ifconfig` as read-only knowledge for maintaining older boxes.

How Do I Check a Specific Interface or IPv6?

To inspect a single interface by name—useful when you have `eth0`, `ens3`, `wlan0`, `docker0`, and more:

“`bash ip a show eth0 ip -br a show eth0 “`

For IPv6, the address appears on the `inet6` line. Addresses beginning with `fe80::` are link-local (valid only on the local segment and not routable across the internet), while a global IPv6 address typically starts with `2000::/3` (commonly `2`, `3`, or a provider-assigned prefix). To list only globally scoped IPv6 addresses:

“`bash ip -6 a show scope global “`

Many cloud providers now assign IPv6 alongside IPv4, so checking both matters when you configure services that should be reachable over either protocol.

DarazHost: Clear IP Assignment and Full Network Control

Understanding these commands is far simpler when your hosting environment gives you a dedicated public IP and genuine root access. On shared or heavily NATed environments, the gap between what `ip a` reports and what the world sees becomes a constant source of misconfiguration.

DarazHost VPS and dedicated servers are built for exactly this kind of control:

  • A dedicated public IP with clear, documented assignment—so the address you configure is the address the internet reaches.
  • Full root access to inspect and manage your network with `ip`, `hostname`, `ip route`, and the full toolkit—no restrictions on what you can query or configure.
  • Reliable, well-provisioned networking with predictable routing, so `ip route get` and your public IP behave consistently.
  • 24/7 expert support to help you confirm IP assignment, configure firewalls, and resolve connectivity questions whenever they arise.

Whether you’re hosting a website, running APIs, or building infrastructure that depends on a stable, routable address, a server with transparent IP assignment removes an entire category of networking headaches.

Frequently Asked Questions

Why does `ip a` show a different IP than the one I use to SSH in?

Because your server most likely sits behind NAT. The interface holds a private IP, while the provider maps a public IP to it at the network edge. The OS only sees the private side, so `ip a` reports that. Run `curl ifconfig.me` to see your actual public IP.

What is the difference between a private and a public IP address?

A private IP (ranges like `10.0.0.0/8`, `172.16.0.0/12`, `192.168.0.0/16`) is used inside a local network and is not routable on the public internet. A public IP is globally unique and routable—it’s how external systems find and reach your server.

How do I get just the IP address into a script or variable?

Use the route-source method for the primary IP:

“`bash ip route get 1.1.1.1 | awk ‘{print $7; exit}’ “`

For the public IP in a script, `PUBLIC_IP=$(curl -s ifconfig.me)` captures it cleanly.

Is `ifconfig` gone for good on Linux?

Not entirely, but it is deprecated. Most modern distributions ship without `net-tools` by default and recommend the `ip` command instead. `ifconfig` still works where installed, so you’ll encounter it on legacy systems—just don’t build new tooling around it.

How do I find my IPv6 address on Linux?

Run `ip -6 a` to list all IPv6 addresses, or `ip -6 a show scope global` for routable ones only. Addresses starting with `fe80::` are link-local and not usable across the internet.


Knowing exactly which command reveals which address—local vs. public, IPv4 vs. IPv6, primary vs. all interfaces—turns IP lookups from guesswork into a two-second task. Bookmark the reference table, remember that `ip a` shows the *local* view while `curl ifconfig.me` shows the *internet’s* view, and you’ll never hand out the wrong address again.

About the Author

Leave a Reply