Command To Know Ip Address In Linux

11 min read

Command to know IP address in Linux is a fundamental skill for system administrators, developers, and everyday users who need to troubleshoot networks, configure services, or simply verify connectivity. Knowing how to retrieve your machine’s IP address quickly can save time when setting up SSH, debugging firewalls, or configuring Docker containers. This guide walks you through the most reliable commands, explains what each output means, and offers tips for choosing the right tool depending on your Linux distribution and network setup.


Understanding IP Addresses in Linux

Before diving into the commands, it helps to recall what an IP address represents. An IP address (Internet Protocol address) is a numeric label assigned to each device connected to a computer network that uses the Internet Protocol for communication. In most modern networks you’ll encounter two versions:

  • IPv4 – a 32‑bit address written as four decimal octets (e.g., 192.168.1.10).
  • IPv6 – a 128‑bit address expressed in hexadecimal blocks separated by colons (e.g., fe80::1ff:fe23:4567:890a).

Linux systems can have multiple IP addresses simultaneously—one per network interface (e.Here's the thing — g. In practice, , eth0, wlan0, docker0). The commands below will show you all configured addresses or let you query a specific one But it adds up..


Basic Commands to Find Your IP Address

1. ip addr show (the modern, recommended way)

The ip utility from the iproute2 suite has largely replaced the older ifconfig. It works on virtually all current distributions and provides detailed information about each network interface.

ip addr show

Typical output snippet:

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
  • inet lines display IPv4 addresses; the /24 is the CIDR netmask.
  • inet6 lines show IPv6 addresses.
  • The interface name (eth0, wlan0, etc.) appears right after the numeric index.

If you only need the IPv4 address of a specific interface, you can filter:

ip -4 addr show eth0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}'

2. hostname -I (quick, all‑addresses list)

For a terse list of all IPv4 and IPv6 addresses assigned to the host, hostname -I is handy:

hostname -I

Example output:

192.168.1.42 10.0.0.5 fe80::21a:2bff:fe3c:4d5e 2001:db8::1

The command separates each address with a space, making it easy to capture in scripts Easy to understand, harder to ignore..

3. ifconfig (legacy but still present)

Although deprecated, ifconfig remains installed on many systems, especially minimal or older servers. Use it the same way as ip addr show:

ifconfig

Sample output:

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)

Note: If you get “command not found”, install the net-tools package (sudo apt install net-tools on Debian/Ubuntu, sudo yum install net-tools on RHEL/CentOS) Most people skip this — try not to..

4. nmcli (NetworkManager‑centric environments)

On desktops or laptops where NetworkManager manages connections, nmcli provides a concise view:

nmcli -t -f IP4.ADDRESS,IP6.ADDRESS device show

The -t flag gives terse output, while -f selects the fields you want. Example:

IP4.ADDRESS[1]: 192.168.1.42/24
IP6.ADDRESS[1]: fe80::21a:2bff:fe3c:4d5e/64
IP6.ADDRESS[2]: 2001:db8::1/64

5. ip route get (to discover the address used for a specific destination)

Sometimes you want to know which IP address your system would use to reach a particular host (e.g., the gateway or an external server) That's the whole idea..

ip route get 8.8.8.8

Output:

8.8.8.8 via 192.168.1.1 dev eth0 src 192.168.1.42 uid 1000
    cache

The src field shows the source IP address that will be used for packets destined to 8.On top of that, 8. Which means 8. 8. This is especially useful on multi‑homed machines Most people skip this — try not to..

6. ss (socket statistics, can reveal bound addresses)

While primarily for inspecting sockets, ss can list addresses that services are bound to:

ss -tuln | awk '{print $5}' | cut -d: -f1 | sort -u

This extracts unique IP addresses listening on TCP/UDP ports Not complicated — just consistent..

7. GUI Alternatives (for completeness)

Most desktop environments provide a network settings panel:

  • GNOME

  • GNOME: Open Settings → Network, click on the wired or wireless interface. The assigned IP addresses appear under "IPv4" and "IPv6" tabs Easy to understand, harder to ignore..

  • KDE Plasma: manage to System Settings → Network Connections, select your interface, and view the IP configuration in the appropriate tab.

  • Xfce / MATE / Cinnamon: These typically rely on the network applet in the system tray. Click the network icon, select "Connection Information" or "Network Settings" to view assigned addresses.

  • Cockpit (web-based): For server administrators, the web console provides a clean "Networking" page under the "System" menu, displaying all interfaces and their IP addresses in a browser-friendly format Easy to understand, harder to ignore..


Quick Reference Cheat Sheet

Command Purpose Best For
ip -4 addr show <iface> IPv4 of a specific interface Scripts & automation
hostname -I All addresses at once Quick checks
ifconfig Legacy detailed output Older systems
nmcli NetworkManager details Desktop/laptop environments
ip route get <host> Source IP for a destination Multi-homed servers
ss -tuln Bound listening addresses Troubleshooting services

Easier said than done, but still worth knowing.


Conclusion

Linux offers a rich set of tools for querying network interface addresses, ranging from the modern and powerful ip command to legacy utilities like ifconfig and user-friendly GUI panels. The ip command from iproute2 is the recommended standard for most use cases, offering precise filtering and scripting-friendly output. For automated workflows, combining ip with tools like grep, awk, or jq allows you to extract exactly the information you need without human interaction.

Understanding which tool to reach for depends on your context: use hostname -I for quick overviews, ip route get when routing context matters, nmcli in NetworkManager-managed environments, and GUI tools when working interactively on a desktop. Mastering these commands ensures you can always identify your system's networking configuration quickly and accurately, whether you are debugging connectivity issues, writing deployment scripts, or simply verifying your network setup.

8. Common Pitfalls & Edge Cases

Even with the right commands, interpreting output can trip up administrators. Here are frequent scenarios where IP discovery behaves unexpectedly:

  • Multiple Addresses per Interface: A single interface (e.g., eth0) often holds an IPv4 address, an IPv6 link-local address (fe80::...), a global IPv6 address, and potentially multiple IPv4 aliases (eth0:1). Commands like hostname -I return all of them space-separated. Always filter explicitly (ip -4 addr show dev eth0) if your script expects a single value.
  • The docker0 / veth / br-* Noise: Container runtimes (Docker, Podman) and virtualization (libvirt, Kubernetes) create virtual bridges and peer interfaces. These clutter ip addr output with private subnets (e.g., 172.17.0.1/16). Use ip -br addr show type veth or filter by physical interface names (en*, eth*) to isolate hardware addresses.
  • Temporary vs. Stable IPv6 (Privacy Extensions): Modern distributions enable IPv6 Privacy Extensions (RFC 4941/8981) by default. ip -6 addr will show two global addresses: a stable one (based on MAC/EUI-64 or stable token) and a temporary one (rotated periodically) marked tmpaddr. For server firewalling or DNS registration, target the stable address (scope global noprefixroute without tmpaddr).
  • Deprecated Addresses: During DHCP renewal or IPv6 reconfiguration, addresses enter a deprecated state (valid lifetime > 0, preferred lifetime = 0). They remain reachable but shouldn't be used for new connections. Check flags: ip -6 addr show tentative deprecated helps identify addresses in flux.
  • VPN/Tunnel Interfaces Precedence: When a VPN (WireGuard, OpenVPN, Tailscale) connects, it often creates a tun or wg interface and modifies the routing table. hostname -I will list the VPN tunnel IP. If you need the physical LAN IP, you must query the specific physical interface (ip -4 addr show dev eth0) rather than relying on global lists.

9. Scripting Best Practices: reliable Extraction

Parsing human-readable output (ip addr show) with awk/sed is brittle across locales and iproute2 versions. For automation, prefer machine-readable formats The details matter here..

Use JSON Output (Modern iproute2)

Most modern ip implementations support -j (JSON), parseable with jq:

# Get primary IPv4 for default route interface
ip -j route get 1.1.1.1 | jq -r '.[0].prefsrc'

# List all interfaces with IPv4 addresses as JSON objects
ip -j -4 addr show | jq -r '.[] | select(.addr_info | length > 0) | "\(.ifname): \(.addr_info[0].local)"'

YAML via nmcli (NetworkManager)

If NetworkManager is running, its structured output is stable:

# Get IPv4 address for the active connection on 'eth0'
nmcli -g IP4.ADDRESS device show eth0 | cut -d'/' -f1

Bash Native (No External Dependencies)

For minimal containers without jq or nmcli, read /proc/net directly (kernel ABI stable):

# Extract IPv4 addresses from /proc/net/fib_trie (kernel routing trie)
# Filters local scope (host) addresses
awk '/local/ { gsub(/[^0-9.]/,"",$2); print $2 }' /proc/net/fib_trie | sort -u

10. IPv6-Specific Consider

10. IPv6-Specific Considerations

IPv6 introduces complexities that often trip up even experienced administrators. The protocol’s autoconfiguration, address diversity, and expanded scope demand deliberate handling.

  • Link-Local Addressing: Every IPv6-enabled interface automatically configures a link-local address (fe80::/10) via Stateless Address Autoconfiguration (SLAAC). These addresses are essential for neighbor discovery and routing protocols but are often overlooked in monitoring scripts. When querying an interface, remember that link-local addresses are only valid on the local network segment and require a zone identifier (e.g., fe80::1%eth0) in commands like ping or ssh.
  • Multiple Global Addresses: A single interface may possess multiple global unicast addresses (GUAs) due to multiple prefix delegations, temporary privacy addresses, or dual-stack deployments. Relying on the first address returned by hostname -I is unreliable. Instead, use ip -6 addr show dev eth0 scope global to list all GUAs and apply filters (e.g., grep -v tmpaddr) to isolate stable addresses for configuration management.
  • DNS and AAAA Records: IPv6’s address rotation (via privacy extensions) can break DNS-based services if dynamic updates are not configured. see to it that DNS registration uses stable addresses (the non-temporary GUA) or that clients update their DNS records upon address change. For static assignments, manually create AAAA records corresponding to the interface’s stable IPv6 address.
  • Firewall and Security: IPv6 is often enabled by default, but firewalls may not have rules for it, leaving services exposed. Audit your firewall (e.g., iptables with ip6tables or nft) to see to it that only necessary IPv6 traffic is permitted. Remember that IPv6’s larger address space makes network scanning more difficult, but misconfigured services remain vulnerable.

11. Troubleshooting Common Pitfalls

Even with the right tools, configuration errors can persist. Here are frequent issues and their remedies:

  • "Device eth0 does not exist": This often indicates that the physical interface is not present (e.g., on a VM with a different NIC name) or that the expected interface name is not configured. Use ip -br link to list all available interfaces and their states. On cloud instances, interface names may follow patterns like ens3 or enp0s3.
  • "Network is unreachable" with a valid IP: Verify the default gateway is reachable (ip route show default) and that the gateway’s MAC address is in the ARP cache (ip neigh show). If the gateway is unreachable, check the physical connectivity and VLAN configuration.
  • DNS Resolution Fails but Ping Works: This points to a DNS configuration issue. Check /etc/resolv.conf for correct nameservers and check that the DNS server is reachable (dig @<nameserver-ip> example.com). For IPv6, verify that the DNS server has a working AAAA record and that your resolver supports IPv6 queries.
  • IP Address Conflicts: Use arping -c 3 -I eth0 <ip> to check if an IP is already in use on the local network. If a conflict is detected, investigate DHCP lease conflicts or static IP assignments that overlap with the DHCP pool.

12. Conclusion

Mastering IP address retrieval in Linux is not merely about memorizing commands but understanding the underlying architecture. Remember that the network is dynamic—interfaces may be renamed, addresses may change, and protocols may evolve. By adopting machine-readable outputs for automation, accounting for IPv6’s autoconfiguration and privacy extensions, and systematically troubleshooting common pitfalls, administrators can ensure reliable connectivity. The ip family of tools, when combined with careful parsing and an awareness of IPv4/IPv6 nuances, provides a reliable foundation for network management. Continuous learning and adaptation remain the ultimate tools in your arsenal But it adds up..

Fresh Picks

Brand New Stories

If You're Into This

Keep the Momentum

Thank you for reading about Command To Know Ip Address In Linux. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home