Finding the server IP address on Linux is a fundamental task for system administrators, developers, and anyone managing infrastructure. Whether you are troubleshooting a network issue, configuring a web server, or setting up secure shell access, knowing your machine's IP is the first step. This guide provides a detailed look at the commands and concepts used to retrieve this critical information directly from the terminal.
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, which is used internally within your local network, such as 192.168.1.10 or 10.0.0.5. The second is the public IP address, which is assigned by your internet service provider and represents your server's location on the global internet. Depending on your goal, you might need to retrieve one or the other, and the tools available in Linux cater to both scenarios. Using the ip Command The ip command is the modern, preferred utility for network management in most distributions. It replaces the older ifconfig command and provides a more consistent output. To view all network interfaces and their assigned addresses, you can use the addr subcommand. This will display both the IPv4 and IPv6 addresses along with the interface names, such as eth0 or ens33.
Using the ip Command
ip addr show For a more concise output that filters specifically for the IPv4 address, you can combine ip with grep and awk . This pipeline searches for the "inet" keyword, which denotes an IPv4 address, and extracts the relevant text. This method is particularly useful in scripts where you need to store the IP in a variable for later use.
Utilizing the hostname Command
hostname -I If you are looking for the address associated with a specific connection, the --ip-address flag provides a similar result. This command is valued for its simplicity and speed, making it one of the quickest ways to get the necessary information without parsing complex output.
Querying External Services
Sometimes, you need to know the public IP address that the outside world sees, rather than the internal network configuration. In these situations, querying an external web service is the most reliable method. The Linux command-line tools curl and wget allow you to fetch data from these APIs and display just the IP address.
curl ifconfig.me
curl ipinfo.io/ip
wget -qO- icanhazip.com
These commands are incredibly simple but effective. They return the public IP as plain text, which is easy to read and integrate into other processes. Note that this method requires an active internet connection to reach the external server.