Linux Command to Show IP Address
Knowing how to display a machine’s IP address is a fundamental skill for anyone working with Linux systems, whether you are troubleshooting network connectivity, configuring services, or simply verifying that a device has obtained the correct address from a DHCP server. The linux command to show ip address can vary depending on the tools installed and the level of detail you need, but most modern distributions provide a handful of reliable options that work across both IPv4 and IPv6 environments. This guide walks you through the most common commands, explains what each part of the output means, and offers practical tips for extracting just the address you need in scripts or one‑liners Small thing, real impact..
Honestly, this part trips people up more than it should.
How to Show IP Address in Linux
Using the ip Command
The ip utility, part of the iproute2 package, has largely replaced the older ifconfig tool and is the preferred way to query network interfaces. To list all addresses assigned to each interface, run:
ip addr show
You will see output similar to:
2: eth0: mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:1a:2b:3c:4d:5e brd ff:ff:ff:ff:ff:ff
inet 192.168.1.42/24 brd 192.168.1.255 scope global dynamic eth0
valid_lft 86234sec preferred_lft 86234sec
inet6 fe80::21a:2bff:fe3c:4d5e/64 scope link
valid_lft forever preferred_lft forever
Key points to notice:
- The line beginning with
inetdisplays the IPv4 address (192.168.1.But 42) together with its prefix length (/24). - The line beginning withinet6shows the IPv6 address (fe80::21a:2bff:fe3c:4d5e/64). - Additional information such as the interface state (
UP), MTU, and link‑layer address (link/ether) is also present.
If you only need the IPv4 address of a specific interface, you can filter the output:
ip -4 addr show eth0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}'
The -4 flag limits the query to IPv4, while the grep pattern extracts the dotted‑decimal address.
Using ifconfig (Legacy)
Although ifconfig is considered deprecated on many modern distributions, it is still present on some systems and may be familiar to longtime administrators. The command:
ifconfig -a
produces output that includes each interface’s hardware address, MTU, and both IPv4 and IPv6 addresses. An example snippet:
eth0: flags=4163 mtu 1500
inet 192.168.1.42 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::21a:2bff:fe3c:4d5e prefixlen 64 scopeid 0x20
ether 00:1a:2b:3c:4d:5e txqueuelen 1000 (Ethernet)
To isolate just the IPv4 address, you might use:
ifconfig eth0 | grep 'inet ' | awk '{print $2}'
Using hostname Command
The hostname utility can also report the IP address(es) associated with the host name. The -I flag prints all network addresses assigned to the machine:
hostname -I
Sample output:
192.168.1.42 2001:db8::1 fe80::21a:2bff:fe3c:4d5e
This command is handy when you need a quick, space‑separated list without any extra interface details.
Using ip route get
Another clever trick involves querying the kernel’s routing table for a dummy destination. The command:
ip route get 8.8.8.8 | awk '{print $
```bash
ip route get 8.8.8.8 | awk '{print $NF}'
The last field ($NF) contains the destination address that the routing decision was made for—often the target of an external service such as Google DNS. By printing this value you can verify that the system actually knows how to reach a given host, even if the direct ARP lookup fails or the interface has been down.
Combining multiple approaches
In practice, you rarely rely on a single command. A typical workflow looks like this:
- Confirm the local address – Use
ip -4 addr show eth0(or the equivalentip -4 addr show eth0 | grep -oP '(?<=inet\s)\d+\.\d+\.\d+\.\d+') to ensure the device has a valid IPv4 lease. - Check the route to a remote host – Run
ip route get <remote_ip>to see whether the appropriate gateway or policy‑based rule exists. If the command returns nothing, the packet would be dropped before ever reaching the interface’s NIC. - Validate connectivity – After confirming the route, test actual reachability with
ping -c 3 <remote_ip>ortraceroute <remote_ip>. This step verifies that the network stack is functioning end‑to‑end rather than merely consulting the routing tables. - Inspect the full set of interfaces – When troubleshooting multicast or link‑local traffic, the legacy
ifconfig -aview can reveal hidden links (e.g.,link/ether … scope link). Modern tools likeip -6 addr showgive comparable insight into IPv6 neighbors.
Choosing the right tool for the job
| Goal | Recommended command(s) |
|---|---|
| List all active interfaces and their IPs | ip -br addr |
| Extract only IPv4 addresses | ip -4 addr show eth0 | grep -oP '\s+(?<=[0-9]+)(\.[0-9]+){3}\s*' |
| Extract only IPv6 addresses | ip -6 addr show eth0 | grep -oP '^([0-9a-f:]+)' |
| Query the routing table for a specific destination | ip route get <dest> | awk '{print $NF}' |
| Find out why a particular interface is down | cat /proc/net/dev or ip -s link |
| Simulate a missing route for testing | sudo ip route del <gateway>/<mask> |
Understanding these nuances helps you diagnose problems quickly and decide whether a manual configuration change (adding a static route, adjusting the MTU, or enabling features such as nmcli for NetworkManager) is necessary Simple, but easy to overlook..
Best‑practice checklist
- Keep the routing table lean. Unnecessary entries increase memory usage and can mask real issues.
- Prefer the modern
ipsuite overifconfig. It provides consistent output formatting across distributions and integrates better with systemd‑networkd and container networking. - Use
--showinstead of invoking interactive mode:
avoids the need for root privileges and makes scripting easier.ip -4 addr show eth0 --comment="primary" - When automating checks, capture the exit status. For example:
if ip -4 addr show eth0 | grep -q 192.168.1.42; then echo "Interface eth0 has a valid IPv4 address." else echo "No IPv4 address found on eth0 – investigate." fi
By mastering these commands you gain a powerful, low‑overhead toolkit for network diagnostics, automation, and even security auditing (detecting unexpected routes or stale leases). But the key takeaway is that the choice of tool should match the specific question you are asking: address enumeration, route verification, or live connectivity testing. With ip, ifconfig, and hostname at your disposal—and the additional insights from ip route get—you can confidently manage and troubleshoot virtually any LAN scenario.
Beyond basic address and route inspection, a complete diagnostic workflow often requires verifying name resolution and examining real-time connection state. Also, tools like dig, nslookup, and systemd-resolve --status let you confirm that DNS queries are being handled correctly, while ss -tunap (the modern successor to netstat) provides a granular view of every socket currently open on the host. For deeper performance analysis, ethtool -S <interface> exposes per-interface counters—drops, overruns, and CRC errors—that can pinpoint hardware or driver issues long before a link goes completely down.
When intermittent problems arise, continuous monitoring becomes essential. Utilities such as mtr (My TraceRoute) combine the functionality of ping and traceroute into a single streaming display, making it easy to spot where packets are being delayed or lost along a path. Pairing mtr with a long-running tcpdump capture on the suspected bottleneck interface gives you both the macroscopic view (where packets are dying) and the microscopic view (what the actual payload looks like at the point of failure) And that's really what it comes down to..
Logging and post-mortem analysis
Most modern distributions route network-related kernel messages through journalctl -k or /var/log/syslog. After an outage, searching for entries containing neighbor, route, or ipv6 can reveal patterns that are invisible during live troubleshooting. For example:
journalctl -k | grep -iE "neigh|route|ipv6" --since "1 hour ago"
This one-liner can surface gratuitous ARP storms, failed DHCP handshakes, or IPv6 duplicate-address detections that occurred minutes before users reported connectivity problems.
Wrapping up
Linux network diagnostics is not a single skill but a layered discipline. At the transport and application layers, ss, dig, and mtr extend your visibility into connections and resolution paths. At the Layer 2/3 boundary, ip, ifconfig, and hostname give you the foundational awareness of addresses, routes, and host identity. And at the observability layer, structured logging and interface counters turn transient symptoms into actionable data Easy to understand, harder to ignore..
The most effective engineers are the ones who know which layer to inspect for a given symptom and who combine multiple tools rather than relying on a single silver bullet. By building a personal toolkit that spans address enumeration, route verification, name resolution, socket inspection, and log analysis—and by automating routine checks with exit-status–aware scripts—you transform reactive firefighting into proactive infrastructure management. That shift, in turn, means faster resolution times, fewer repeat incidents, and a network you can trust under pressure Worth keeping that in mind..