When managing a Linux server, understanding how to check IP address is a fundamental skill. Whether you are troubleshooting a network issue, configuring a new service, or securing your environment, knowing the exact IP configuration is critical. The Linux operating system provides several powerful command-line tools that deliver this information instantly, giving you full control over your network interface settings.
Using the ip Command
The modern standard for network management on Linux is the ip command. It replaces the older and deprecated ifconfig utility and offers a more consistent output format. To view the IP address for all active interfaces, you simply open a terminal and execute ip addr . This command lists every network interface, along with its associated IPv4 and IPv6 addresses, subnet masks, and state.
Interpreting ip addr Output
The output of ip addr is structured to provide maximum detail at a glance. Look for the line labeled inet under your active interface, such as eth0 or ens33 . Next to it, you will see the IP address in standard dotted-decimal notation, followed a slash and the prefix length (e.g., /24). This prefix length indicates the subnet mask, which is essential for routing traffic correctly within your network segment.
Utilizing the ifconfig Command
Although deprecated, many older systems and legacy scripts still rely on ifconfig . If the ip command is unavailable or you prefer a simpler, more visual output, you can use ifconfig . Running this command without arguments typically displays the configuration for the primary active interface, including the IP address, the hardware MAC address, and the data transmission statistics.
Installation if Required
On minimal server installations, the ifconfig utility might not be installed by default. If you encounter a "command not found" error, you can easily install it using your distribution’s package manager. On Debian-based systems like Ubuntu, the command is sudo apt install net-tools . For Red Hat-based distributions like CentOS, the package is usually called net-tools and can be installed via yum or dnf .
Checking the Default Gateway
Knowing your server's IP address is only part of the puzzle; understanding how it communicates outside its local network is equally important. The default gateway is the IP address of the router that forwards traffic from your server to other networks, such as the internet. You can quickly retrieve this information by using the command ip route and looking for the line that specifies default via .
Alternative Route Command
For a more straightforward presentation, you can use netstat -rn or route -n to view the kernel routing table. These commands display the IP addresses of gateways in a numerical format, which avoids the delay of DNS lookups. The "Gateway" column in the output shows the IP address your server uses to reach external destinations, which is vital for diagnosing connectivity problems.
Hostname and DNS Resolution
While not the IP address of the server itself, checking the hostname and ensuring proper DNS resolution is closely related to network configuration. You can verify the hostname of your server by running the hostname command. To see how the hostname resolves to an IP address from the perspective of the system, you can use the host or nslookup commands, followed by the hostname.