Blog / Linux/ Complete Guide to Viewing IPv4 and IPv6 Addresses (Public/Private) on a Linux Server

Complete Guide to Viewing IPv4 and IPv6 Addresses (Public/Private) on a Linux Server

Linux 服务器查看 IPv4 与 IPv6 地址(公网/私网)完全指南

How to View IPv4 and IPv6 Addresses on a Linux Server

Quickly and accurately obtaining a server's IPv4 and IPv6 addresses (both public and private) is a fundamental skill for Linux server administration and network troubleshooting. This guide covers several reliable methods.

1. Using the ip Command (Recommended)

The ip command is the most powerful network configuration tool on modern Linux distributions, replacing the older ifconfig.

View IP Addresses for All Network Interfaces

ip addr show

Or the shorthand:

ip a

The output lists all network interfaces (e.g., eth0, ens33, lo) with details. Lines starting with inet show IPv4 addresses; lines starting with inet6 show IPv6 addresses.

View IP Addresses for a Specific Interface (e.g., eth0)

# View IPv4 address for eth0
ip -4 addr show eth0 | grep inet | awk '{print $2}' | cut -d'/' -f1

# View IPv6 address for eth0
ip -6 addr show eth0 | grep inet6 | awk '{print $2}' | cut -d'/' -f1
  • ip -4 / ip -6: Restricts output to IPv4 or IPv6.
  • grep inet / grep inet6: Filters the relevant address lines.
  • awk '{print $2}': Extracts the second field (the address with CIDR mask).
  • cut -d'/' -f1: Removes the CIDR mask, leaving the pure IP address.

2. Using the hostname Command

The hostname command provides a quick way to view the host's IP addresses.

# Display all IPv4 addresses
hostname -I

# Display IPv6 addresses (not supported on all systems)
hostname -i

hostname -I (uppercase I) lists all IPv4 addresses for network interfaces, separated by spaces.

3. Using curl to Get the Public IP

The previous commands show locally configured IPs. To see the public IPv4 or IPv6 address your server uses for outbound connections, query an external service.

# Query public IPv4 address
curl -4 ifconfig.me

# Query public IPv6 address (requires IPv6 connectivity)
curl -6 ifconfig.me

The -4 or -6 flag forces curl to use the corresponding IP protocol stack.

Distinguishing Public vs. Private IP Addresses

  • Private IP Addresses: Used within local networks. IPv4 ranges: 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16. IPv6 range: fd00::/8 (ULA). These are not routable on the public internet.
  • Public IP Addresses: Assigned by your ISP, globally unique and routable. Addresses from ip addr show outside the private ranges are likely public. The most accurate method is using the curl command above.

Summary

Knowing how to check your server's IP addresses is essential. Recommendations:

  1. Use ip addr show to see all locally configured IPs.
  2. Use hostname -I for a quick list of all IPv4 addresses.
  3. Use curl to query the public IP address used for outbound connections.

Choose the command or combination that best fits your needs.

Post a Comment

Your email will not be published. Required fields are marked with *.