Finding your internet protocol address on a Unix-like operating system takes seconds once you know the right commands. Whether you manage a remote server over SSH or configure a local home network on an Ubuntu desktop, knowing how to retrieve both local and public IP addresses is an essential day-to-day administrative skill.
Modern distributions deprecate legacy tools like ifconfig in favor of the unified iproute2 suite. This guide walks you through the exact syntax needed to display local network interfaces, query your public-facing address, troubleshoot connectivity issues, and automate your workflow without guesswork.
Understanding IP Addresses in Linux
Before running commands, it helps to distinguish between the two primary types of addresses your machine encounters:
- Local (Private) IP Address: Assigned by your local router via DHCP (or set statically), allowing your computer to communicate with other devices on your local area network (LAN). Common ranges include
192.168.x.xand10.x.x.x. - Public IP Address: Assigned by your Internet Service Provider (ISP), representing your device or router to the wider internet. Web services and remote servers see this address when you make outbound connections.
Different commands target different scopes. Local interface tools query your network card drivers directly, while external queries use HTTP requests to check how external servers view your connection.
Using the Modern IP Route Suite
The ip command is the standard utility for managing network interfaces, routing, and tunnels across modern distributions. It replaces older utilities found in the net-tools package.
Show All Interfaces and Addresses
To view every network interface configured on your system along with its assigned IPv4 and IPv6 addresses, run:
ip addr show
You can also use the shorthand version:
ip a
The output breaks down each interface—such as the loopback interface (lo) and your physical Ethernet or Wi-Fi adapter (eth0 or wlan0). Look for the inet keyword under your active interface to find your IPv4 address, and inet6 for your IPv6 address.
Show Only IPv4 Addresses
If the standard output contains too much clutter, you can filter for IPv4 addresses specifically:
ip -4 addr show
To target a single interface—such as checking only your wireless card—append the device name:
ip addr show dev wlan0
Legacy Commands: Ifconfig and Hostname
While iproute2 is the modern standard, many legacy systems or older server configurations still rely on traditional utilities.
Using Ifconfig
The ifconfig command comes from the legacy net-tools package. On many modern minimal installs, you might need to install it manually (sudo apt install net-tools), though it's worth knowing for older environments:
ifconfig
To narrow down the output to a specific interface like eth0:
ifconfig eth0
Using Hostname for Quick Lookups
If you need a quick way to find the primary local address assigned to your machine without parsing interface lists, the hostname command offers a shortcut:
hostname -I
This prints a space-separated list of all local IP addresses assigned to active interfaces on the host, making it useful in shell scripts.
Finding Your Public IP Address from the Terminal
Local commands only reveal your LAN address behind a router. When you need to find your public-facing address from the command line—such as when configuring remote SSH access or firewall whitelists—you must query an external service.
Using Curl or Wget
You can query public IP lookup APIs using command-line HTTP clients. Several reliable services provide plain-text responses:
curl ifconfig.me
Or using wget:
wget -qO- https://icanhazip.com
These commands fetch your public IPv4 address directly into your terminal output without returning unnecessary HTML code, making them ideal for scripting.
Practical Troubleshooting Scenarios
Knowing your IP address is only half the battle. Here is how network administrators use these details to diagnose everyday connection problems.
Identifying Default Gateways
If your machine has an IP address but cannot reach the internet, you need to check your routing table to find the default gateway—the router that forwards your traffic:
ip route show
Look for the line starting with default via. The IP address following via is your router's local address. You can test your connection to it using the ping utility:
ping -c 4 192.168.1.1
Checking DNS Resolution
Sometimes network issues stem from DNS failures rather than IP configuration problems. You can verify network connectivity by pinging a known public IP address directly, such as the Cloudflare public DNS resolver:
ping -c 4 1.1.1.1
If this succeeds while domain names fail to resolve, your network interface has a valid IP address, but your DNS server settings require adjustment in /etc/resolv.conf or your network manager configuration.
Frequently Asked Questions
Why does my local IP address start with 169.254?
An IP address starting with 169.254.x.x is an APIPA (Automatic Private IP Addressing) address. This indicates that your Linux machine failed to receive an IP address from a DHCP server. Check your physical cable connection or Wi-Fi credentials.
How do I find my IP address in a Bash script?
You can assign the output of hostname -I to a variable inside your script:
LOCAL_IP=$(hostname -I | awk '{print $1}')
This extracts the first available local address cleanly.
Is ifconfig deprecated?
Yes. The net-tools package containing ifconfig and route has been deprecated in most Linux distributions for over a decade in favor of the iproute2 suite (ip command). While still available via package managers, learning the ip command ensures compatibility with modern systems.
Can I set a static IP address using these commands?
The ip command allows you to add temporary addresses to an interface instantly (e.g., ip addr add 192.168.1.50/24 dev eth0), but these changes do not persist across reboots. For permanent static IP configuration, modify your distribution's network manager configuration files or Netplan yaml settings.