Skip to content
Education & Software

Methods to Show IP Addresses in Ubuntu via Command Line

In this article

To show your local IP address in Ubuntu, open the terminal and execute ip address or hostname -I. To retrieve your public IP address as seen by external websites, execute curl ifconfig.me or curl icanhazip.com. These terminal utilities return accurate internal interface data and external routing addresses efficiently.

Understanding Local vs. Public Network Addresses in Ubuntu

Every Ubuntu system connected to a local network or the internet uses at least one Internet Protocol (IP) address. Understanding the structural difference between private (local) addresses and public (external) addresses is essential before selecting the appropriate command line method.

  • THE INTERNET
    • Public IP Address (e.g., 203.0.113.45 via ISP Router)
    • LOCAL AREA NETWORK (LAN)
      • Ubuntu Server/PC
        • Interface: eth0
        • Private IP: 192.168.1.50/24
      • Ubuntu Desktop (Wi-Fi)
        • Interface: wlp2s0
        • Private IP: 192.168.1.105/24

Private (Local) IP Addresses

A local IP address is assigned by your local network’s router or Dynamic Host Configuration Protocol (DHCP) server. Local IP addresses allow devices within the same local area network (LAN)—such as computers, printers, and home servers—to communicate with each other. Private IP addresses fall under standardized ranges established by RFC 1918:

  • 10.0.0.0 to 10.255.255.255
  • 172.16.0.0 to 172.31.255.255
  • 192.168.0.0 to 192.168.255.255

Local addresses cannot be accessed directly from the public internet without specific network configurations such as port forwarding or Virtual Private Networks (VPNs).

Public (External) IP Addresses

A public IP address is assigned to your network edge device (typically your Internet Service Provider's router or modem) or directly to a Cloud Virtual Private Server (VPS) hosted on platforms like DigitalOcean, AWS, or Linode. Every device on a private network usually shares a single public IP address when accessing outbound internet resources through Network Address Translation (NAT).

Linux Network Interfaces

Ubuntu identifies hardware and virtual network devices using standardized interface names:

  • lo: Loopback interface (typically 127.0.0.1 for IPv4 or ::1 for IPv6), used for internal communication on the local machine.
  • eth0 or enp3s0: Ethernet interfaces connected via physical network cables. Modern Ubuntu versions use predictable network interface names (enpXsY or ensX).
  • wlp2s0 or wlan0: Wireless network interfaces connected to Wi-Fi networks.
  • docker0 or veth*: Virtual network bridges created by container environments like Docker or Podman.
  • tun0 or wg0: Virtual tunnel interfaces established by OpenVPN or WireGuard connections.

Modern CLI Commands for Retrieving Your Local IP Address

Modern Ubuntu releases rely on the iproute2 software suite to handle network configurations. The commands below provide native, dependency-free ways to examine local network addresses.

1. The ip address Command Suite

The primary tool for viewing interface details in Ubuntu is ip address (which can be abbreviated as ip addr or simply ip a).

Executing the base command displays detailed configuration statistics for every active and inactive network interface on your system:

ip address

Detailed Output Interpretation

Executing ip a yields output formatted similarly to the following example:

1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: enp3s0: mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 52:54:00:12:34:56 brd ff:ff:ff:ff:ff:ff inet 192.168.1.150/24 brd 192.168.1.255 scope global dynamic noprefixroute enp3s0 valid_lft 85400sec preferred_lft 85400sec inet6 fe80::5054:ff:fe12:3456/64 scope link valid_lft forever preferred_lft forever

Key fields in this output include:

  • inet 192.168.1.150/24: The IPv4 address assigned to the enp3s0 interface alongside its CIDR subnet mask (/24 corresponds to 255.255.255.0).
  • inet6 fe80::5054:ff:fe12:3456/64: The IPv6 link-local address assigned to the interface.
  • state UP: Indicates that the network interface hardware is powered on and connected.

Useful Flags for ip address

To restrict output strictly to IPv4 addresses:

ip -4 address

To restrict output strictly to IPv6 addresses:

ip -6 address

To format output into a brief, clean table:

ip -br address show

Example output of ip -br address show:

lo UNKNOWN 127.0.0.1/8 ::1/128 enp3s0 UP 192.168.1.150/24 fe80::5054:ff:fe12:3456/64

To display details for a single specific network interface (e.g., enp3s0):

ip address show dev enp3s0

2. The hostname -I Command

If you require a fast, clean method to retrieve local IP addresses without additional interface metadata, hostname -I is an ideal command.

hostname -I

Output Characteristics

192.168.1.150 172.17.0.1

  • Returns all active network addresses assigned to all network interfaces.
  • Omits loopback addresses (127.0.0.1).
  • Outputs space-separated values, making it ideal for inclusion in shell scripts.

3. Determining the Active Outbound IP via ip route

Systems with multiple active network interfaces (e.g., active Wi-Fi, Ethernet, and VPN connections) have multiple local IP addresses. To determine which local IP address is actively used to route outbound internet traffic, query the kernel routing table:

ip route get 1.1.1.1

Example output:

1.1.1.1 via 192.168.1.1 dev enp3s0 src 192.168.1.150 uid 1000 cache

The value immediately following src (192.168.1.150) is the exact internal IP address used for outbound internet connections.

Alternative and Legacy Commands for Local IP Address Retrieval

Depending on your system setup and installed software packages, alternative CLI utilities can be used to query local network settings.

1. NetworkManager Command Line Tool (nmcli)

On Ubuntu Desktop installations and Ubuntu Server instances configured with NetworkManager, nmcli offers structured interface inspection tools.

To view concise device address assignments:

nmcli device show

To inspect a specific interface (e.g., enp3s0):

nmcli -g IP4.ADDRESS device show enp3s0

Output:

192.168.1.150/24

nmcli is particularly helpful on machines managed by NetworkManager because it directly queries the active network configuration daemon rather than reading low-level kernel interface states.

2. The Legacy ifconfig Command (net-tools)

Historically, ifconfig was the default command for reviewing network interface configurations across Unix and Linux distributions. However, ifconfig was deprecated in favor of iproute2 (ip) tools.

If you execute ifconfig on a fresh Ubuntu installation, you may encounter an error indicating that the command was not found. If your workflow specifically requires ifconfig, it can be installed via the net-tools package:

sudo apt update && sudo apt install net-tools -y

Once installed, running ifconfig yields:

enp3s0: flags=4163 mtu 1500 inet 192.168.1.150 netmask 255.255.255.0 broadcast 192.168.1.255 inet6 fe80::5054:ff:fe12:3456 prefixlen 64 scopeid 0x20 ether 52:54:00:12:34:56 txqueuelen 1000 (Ethernet) RX packets 14205 bytes 18452011 (18.4 MB) TX packets 8912 bytes 1024982 (1.0 MB)

Note: While ifconfig remains widely recognized, relying on ip address is strongly recommended for standard scripting and modern Linux administration.

Commands for Finding Your External (Public) IP Address

Because local network commands only read internal interface configurations, discovering your public IP address requires querying an external internet-facing server.

1. HTTP-Based IP Lookup Services via curl or wget

Command-line HTTP utilities can query third-party plain-text APIs that echo back the requesting client's public IP address.

Using curl

  • ifconfig.me:
    curl ifconfig.me
    
  • icanhazip.com:
    curl icanhazip.com
    
  • api.ipify.org:
    curl https://api.ipify.org
    

Restricting to IPv4 or IPv6 Forced Queries

If your network supports dual-stack IPv4/IPv6 setups, external curl requests may default to returning your IPv6 address. To explicitly force an IPv4 lookup, pass the -4 flag:

curl -4 ifconfig.me

To explicitly force an IPv6 lookup, pass the -6 flag:

curl -6 icanhazip.com

Using wget

If curl is not installed on your system, wget provides identical functionality:

wget -qO- https://icanhazip.com

  • -q: Suppresses standard download log output.
  • -O-: Redirects the downloaded content directly to stdout (the terminal window).

2. DNS-Based Public IP Queries via dig

HTTP queries rely on web services that can occasionally undergo rate limiting or experience downtime. Querying public DNS resolvers using the dig tool is often faster and less prone to web application throttle limits.

If dig is not present, install it via the bind9-utils package:

sudo apt update && sudo apt install bind9-utils -y

Querying OpenDNS

dig +short myip.opendns.com @resolver1.opendns.com

Querying Google Public DNS

dig +short -4 o-o.myaddr.l.google.com @ns1.google.com TXT

Explanation of Flags

  • +short: Strips out verbose DNS header information, returning only the requested IP record string.
  • @resolver1.opendns.com: Directs the DNS query specifically to OpenDNS infrastructure rather than your local system's default DNS resolver.

Ubuntu Command Line Network Address Retrieval Methods Compared

The table below summarizes the key operational characteristics of Ubuntu IP address retrieval commands:

Command Address Type Retrieved Default Installed Package Output Format Complexity Scripting Friendliness Primary Best Use Case
ip address / ip a Local (IPv4 & IPv6) iproute2 (Default) High (Verbose text) Moderate Full interface state and subnet verification
ip -br address Local (IPv4 & IPv6) iproute2 (Default) Low (Clean table) High Quick human review of all local interfaces
hostname -I Local (IPv4 & IPv6) hostname (Default) Very Low (Space-delimited) Excellent Fast local IP extraction for shell scripts
ip route get 1.1.1.1 Local (Active Outbound) iproute2 (Default) Low (Single route line) High Identifying primary active LAN IP
nmcli device show Local (Detailed) network-manager High (Key-value pairs) High NetworkManager-managed environments
ifconfig Local (Legacy) net-tools (Optional) High (Verbose text) Moderate Compatibility with legacy system documentation
curl ifconfig.me Public (External) curl Very Low (Single string) Excellent Determining outbound NAT / Public IP
dig +short myip... Public (External) bind9-utils (Optional) Very Low (Single string) Excellent Fast public IP lookup via DNS protocols

Filtering and Parsing IP Output for Bash Automation

When writing automation scripts, extracting exact IP address strings without extra text formatting or network interface labels is necessary.

1. Extracting Local IP Address with ip and Standard Utilities

Using ip, grep, and awk, you can isolate specific local IPv4 addresses:

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

  • grep -oP: Enables Perl-compatible regular expressions (PCRE) and prints only matching strings.
  • (?<=inet\\s): Lookbehind assertion matching text directly following inet .
  • \\d+(\\.\\d+){3}: Matches valid IPv4 octet structures.

2. Using JSON Output with jq

Modern versions of iproute2 support structured JSON output using the -j or --json flag. Combined with jq, this offers reliable output parsing without brittle regular expressions.

Install jq if needed:

sudo apt install jq -y

To extract the IPv4 address of interface enp3s0:

ip -j address show enp3s0 | jq -r '.[0].addr_info[] | select(.family=="inet") | .local'

3. Assigning Public IP to a Shell Variable

To store your public IP address in a Bash variable for deployment scripts or dynamic DNS tools:

PUBLIC_IP=$(curl -s -4 https://icanhazip.com) echo "The system public IP is: ${PUBLIC_IP}"

  • -s: Runs curl in silent mode to prevent progress meters from cluttering script outputs.

Troubleshooting Common CLI IP Retrieval Issues

Problem 1: No IP Address Assigned to Interface

If ip address lists an interface, but no inet address is displayed:

  1. Check Physical Connection: Ensure the physical cable is connected or Wi-Fi is within range.
  2. Interface State: Verify whether the interface is marked DOWN. Bring it up using:
    sudo ip link set dev enp3s0 up
    
  3. DHCP Renewal: Request a fresh IP address assignment from your DHCP server:
    sudo dhclient enp3s0
    

Problem 2: curl Commands Timeout on Public IP Query

If querying public services like ifconfig.me hangs or times out:

  1. Verify Gateway Reachability: Ensure your system can reach local gateway routers (ping -c 3 192.168.1.1).
  2. Verify DNS Resolution: Test standard domain resolution (ping -c 3 gnu.org).
  3. Firewall Restrictions: Ensure local ufw rules or upstream firewalls do not block outbound HTTP/HTTPS connections on ports 80 and 443.

Problem 3: Virtual Adapters Masking Main Interface IPs

Systems running container technologies (Docker, LXC, Podman) generate multiple virtual interfaces, causing commands like hostname -I to list multiple local IP addresses. To target your physical adapter explicitly, always pass the interface identifier directly to ip address show dev .

Choosing the Right IP Retrieval Command for Your Workflow

Select your command line method based on your target network scope and automation requirements:

  • For Instant Manual Review of Local IPs: Use ip -br a. It formats interface states, IPv4, and IPv6 details clearly without overwhelming terminal screens.
  • For Shell Scripting Local IPs: Use hostname -I or ip -j address show. These methods eliminate complex regular expression parsing and reduce script brittleness across Linux distributions.
  • For Identifying Outbound Interface IPs: Use ip route get 1.1.1.1. This command pinpoints the exact internal address used for external routing on multi-interface devices.
  • For External Public IP Verification: Use curl -s -4 icanhazip.com or dig +short myip.opendns.com @resolver1.opendns.com. These tools efficiently query external services over standard web or DNS protocols.

Frequently Asked Questions

How do I check my IP address in Ubuntu without installing extra packages?

Use ip address or hostname -I for local IP address retrieval. Both utilities are included natively in Ubuntu installations via the default iproute2 and system core packages.

Why does the ifconfig command fail on modern Ubuntu installations?

ifconfig was deprecated in favor of the iproute2 networking suite (ip command). While ifconfig can still be installed manually via the net-tools package, modern Linux distributions rely on ip for improved performance and extended feature support.

What is the difference between IPv4 and IPv6 addresses shown in terminal outputs?

IPv4 addresses use a 32-bit numeric format (e.g., 192.168.1.150), whereas IPv6 addresses use a 128-bit hexadecimal format (e.g., fe80::5054:ff:fe12:3456). IPv6 provides a vastly larger address space to accommodate modern globally connected devices.

How can I locate my default gateway router IP alongside my local IP address?

Run ip route show or ip route | grep default. The IP address immediately following the keyword via represents your local network's default gateway router.

Is querying external public services like ifconfig.me safe?

Yes. Standard public IP lookup APIs only record the incoming IP header of your HTTP request to echo your network's external address back to your terminal. However, avoid sending sensitive parameters or authentication credentials when executing external curl commands.