Finding your machine's IP address is a fundamental task for anyone working with Linux, whether you are troubleshooting a network issue, setting up a server, or simply verifying your connection. The operating system provides several straightforward commands to display this information, ranging from the classic ifconfig to the more modern ip utility. This guide walks through the methods to check IP addresses in Linux, explaining when to use each tool.
Understanding IP Addresses in Linux
Before diving into the commands, it is helpful to understand the types of IP addresses you might need to check. A Linux machine typically has a local or private IP address, which is used within your internal network, and a public IP address, which is the address the outside world sees via your router. You can check the local address directly from your terminal, while the public address requires querying an external service.
Using the ip Command
The ip command is the modern standard for network configuration and is part of the iproute2 package. It replaces the older ifconfig tool and provides a more consistent output. To check your local IP address, you can use the addr or a subcommand.
Command: ip a
Running this command will list all network interfaces on your system, including loopback and virtual interfaces. Look for the line containing "inet" under your primary network interface, which is usually named eth0 (Ethernet) or ens33 (modern naming convention).
Using the hostname Command
A quick way to get the IP address associated with the machine's hostname is to use the hostname command with the -I flag (capital i). This option displays all IP addresses assigned to the host, which is useful if the machine has multiple network interfaces or IPs.
Checking the Public IP Address
To find your public IP address, you need to ask an external server, as this information is not available locally on your machine. You can use command-line tools like curl or wget to query a web service that returns your public IP as plain text.
Command Examples:
curl ifconfig.me
curl ipinfo.io/ip
wget -qO- ifconfig.co
These commands contact a website and return the IP address of the interface making the request. This is the address used for outbound connections to the internet.
Using the Legacy ifconfig Command
While largely deprecated, ifconfig might still be available on older systems or distributions. If the command is not found, you may need to install the net-tools package. When run, it displays information similar to the ip command, showing the interface name, IP address, and network mask.
Summary of Common Scenarios
Choosing the right command depends on your specific goal. Use ip a for detailed information about local network interfaces. Use hostname -I for a quick list of local IPs. When you need to know how the outside world sees your connection, use a curl command to check your public IP. Mastering these tools ensures you can always pinpoint your network configuration.