Finding the server IP address on Linux is a fundamental skill for system administrators, developers, and anyone managing infrastructure. Whether you are troubleshooting a network issue, setting up a new service, or securing your environment, knowing how to quickly retrieve this information is essential. This guide provides a detailed look at the commands and methods available to identify your server's IP configuration on any Linux distribution.
Understanding IP Addresses in Linux
Before diving into the commands, it is important to understand the two primary types of IP addresses you will encounter. The first is the private IP address, used internally within your local network to communicate with other devices. The second is the public IP address, which is assigned by your Internet Service Provider and represents your server to the wider internet. The methods below will help you locate both, depending on your specific needs.
Using the ip Command
The modern standard for network management on Linux is the ip command, which has largely replaced the older ifconfig utility. To display all network interfaces and their associated IP addresses, you can use the addr subcommand. This provides a clear and detailed view of your configuration.
Basic IP Command Syntax
ip addr show – Lists all interfaces and their IPs.
ip a – A concise shorthand for the same command.
ip route get 1.2.3.4 – Shows the specific route used to reach an external address, revealing the active interface and IP.
Utilizing the hostname Command
For a quick check of the system's configured hostname and IP address, the hostname command offers a fast solution. While it primarily deals with the hostname, adding specific flags allows you to bypass DNS lookups and display the numeric IP address directly.
Hostname Flags for IP Display
hostname -i : Returns the IP address associated with the hostname, but may fail if the system is not properly configured in DNS.
hostname -I : Outputs all IP addresses assigned to the host, excluding loopback addresses. This is often the fastest way to get the primary LAN IP.
Checking the ifconfig Command
Although deprecated on many modern distributions, ifconfig remains a familiar tool for legacy scripts and environments. If the command is not found, you may need to install the net-tools package. It provides a straightforward interface view that is easy to read for beginners.
Ifconfig Output Structure
Interface | IP Address | Mask | Broadcast
eth0 | 192.168.1.100 | 255.255.255.0 | 192.168.1.255
Finding the Public IP Address
The local commands listed above show your private network address. To find the public IP address seen by the internet, you must query an external service. This is useful for configuring firewalls, port forwarding, or verifying connectivity from outside the network.
Command Line Utilities for Public IP
curl ifconfig.me : A simple text response containing your public IP.
curl ipinfo.io/ip : Returns the IP with minimal formatting.