As a Linux user, it’s often necessary to check your computer’s IP address, either for system troubleshooting or network configuration. The IP address, or Internet Protocol address, is a numerical label assigned to each device participating in a computer network. It serves two main functions: identifying the host or network interface and providing the location of the host in the network.
In Linux, there are various methods to retrieve this information, but this article will focus on using the command line interface (CLI), the most powerful and versatile tool at your disposal. By the end of this article, you’ll have learned how to use commands like ifconfig, ip, and hostname to reveal your computer’s IP address.
Understanding IP Addresses
Before we delve into the practical aspect, let’s understand what an IP address is. There are two versions of IP addresses in use today – IPv4 and IPv6. IPv4 is a numerical address like 192.168.0.1, and IPv6 is a more complex alphanumeric address like 2001:0db8:85a3:0000:0000:8a2e:0370:7334. Both serve the same purpose, to uniquely identify a device on a network, but IPv6 was introduced to deal with the long-anticipated problem of IPv4 address exhaustion.
The ifconfig Command
The ifconfig (interface configuration) command in Linux is used to display or configure a network interface. Although this command has been deprecated in favor of the ip command, it is still widely used and you’ll find it present in most systems.
To display your IP address using ifconfig, open your terminal and type the following:
This command will display the details of all network interfaces on your system. The ‘inet’ entry refers to your IPv4 address, and ‘inet6’ refers to your IPv6 address.
The ip Command
The ip command is a more powerful and modern replacement for ifconfig. It can perform several other tasks that ifconfig can’t.
To display your IP address using the ip command, type the following in your terminal:
ip addr show
Again, ‘inet’ refers to the IPv4 address and ‘inet6’ to the IPv6 address.
The hostname Command
The hostname command is used to display the system’s DNS name, and the -I option will display the IP address of your network interfaces.
To use this command, type the following in your terminal:
This command will display all the network IP addresses assigned to your system.
Getting the Public IP Address
While the aforementioned commands provide information about your private IP address, which is used within your local network, there may be occasions where you need to know your public IP address. This is the address that your Internet Service Provider (ISP) assigns to you, and it’s what the outside world sees when you’re connecting to websites and services on the internet.
In Linux, there’s no built-in command to retrieve the public IP address directly from the command line interface, but you can leverage some external services like dig command with OpenDNS, wget or curl with ifconfig.me service.
Using dig with OpenDNS
The dig command is a tool for querying DNS nameservers for information about host addresses, mail exchanges, nameservers, and related information. To use dig to find your public IP address, type the following in your terminal:
dig +short myip.opendns.com @resolver1.opendns.com
This command tells dig to query the special address myip.opendns.com on the DNS server resolver1.opendns.com. The DNS server will return your public IP address.
Using wget or curl with ifconfig.me service
ifconfig.me is a web service that returns your public IP address. You can access it using either wget or curl.
- Using wget:
wget -qO- ifconfig.me/ip
- Using curl:
Both commands will retrieve your public IP address from the ifconfig.me web service.
Understanding how to find your IP address in Linux is a crucial skill for managing and troubleshooting your system and network. Whether you’re a system administrator, a developer, or an enthusiastic Linux user, the command line offers powerful tools for you to unlock the full potential of your Linux system.
Remember, the Linux command line is a powerful tool, but with great power comes great responsibility. Always ensure you understand the commands you are running and the potential impact they may have on your system.
These are just a few of the commands available in Linux for working with IP addresses. As you delve deeper into the world of Linux, you’ll discover many more commands and options that can help you master your system and network.
How I can know the IP address to test APACHE server after installing it ?
Use “ip addr show” command
Could You explain how to add an additional virtual ip address to the interface using ip command?
Thank you very much in advance.
Thank you sir, Good see your hardwork. finally got your website first in search from Qatar.