In this tutorial, we will learn how to find your IP address in Linux. Before going further though, we should know what an IP address is, it’s types and the representation. Let’s discuss more about this.
What is an IP address?
IP is short for Internet Protocol. You might know about the OSI model which consists of the 7 layers. The OSI model gives a basic understanding of how computers communicate with each other using standard protocols. Check the OSI Official documentation for more details.
For more details, check the IP Official Documentation.
The IP address is represented using 4 or 16 octets. It depends whether it is IPv4 version or IPv6 version. Let’s see more about this.
IPv4 & IPv6 version
Let’s discuss more about this. The IP address belonging to IPv4 version consists of 4 octets represented as x.x.x.x. Each octet consists of a byte or 8 bits making it a 32-bit IP address. Note that, the octets are separated by a dot(.). They are represented in the decimal format or also known as dotted decimal notation.
For more details, check the IPv4 Official Documentation.
The IP address belonging to the IPv6 version consists of 16 octets. It consists of 128 bits. The IPv6 is an alphanumeric address while IPv4 is a numeric address. Note that, the binary bits in the IP address are separated by the colon (:). Due to an increase in demand for IP addresses, the IPv6 version of IP came into existence.
Check the IPv6 Official documentation for more details.
What is public and private IP address?
We know that every host requires a unique IP address. The IANA i.e. Internet Assigned Numbers Authority is responsible for managing and distributing the IP addresses. There are 5 classes in a network i.e. A, B, C, D, and E. The IANA designated a range of addresses in class A, B and C for use in private networks. Such addresses can be used by anyone but they only work in Local Area Networks. This is known as a Private IP address. The range of private IP address is:
- Class A: 10.0.0.0 to 10.255.255.255 (1 sub-network)
- Class B: 172.16.0.0 to 172.31.255.255 (16 sub-networks)
- Class C: 192.168.0.0 to 192.168.255.255 ( 256 sub-networks)
The remaining addresses come under the category of public IP addresses. It allows communication outside the network. A public IP address can be accessed over the internet. The public IP address is assigned by the ISP i.e. Internet Service Provider.
Let’s see how to find your IP address in Linux.
How to find your Public IP address in Linux?
As discussed there are two categories of IP address i.e. public and private. Here we will discuss how to find the public IP address. We can use curl command, wget command, and dig command. Let’s discuss more about this.
Find IP address using curl command
We use curl command whenever we want to access or download files on the remote server. But curl command can also be used to find your IP address in Linux. Check the tutorial on how to use curl command in Linux for more details.
The curl command line tool supports many protocols such as HTTPS, HTTP, FTP, FTPS, and many more. Let’s have a look at the following commands which will display the IP address.
curl ifconfig.co
OR
curl ifconfig.me
Or
curl icanhazip.com
You can see the public IP address. “ifconfig.me” , “ifconfig.co”, “icanhazip.com” are web sites the return your public IP address when requested. You can visit these websites directly from your browser to find your IP address in Linux. You can use these servers when you want to know the public IP address of your system.
Find the public IP using dig command
DIG stands for Domain Information Groper. It is a network administration command-line tool that is used for querying Domain Name System (DNS) name servers. You can query the information about various DNS records, host addresses, mail exchanges, and name servers.
Check Dig command Official Documentation for more details.
To find the public IP address, we will use dig command along with opendns.com resolver to find your IP address in Linux. Let’s have a look at the command below:
dig ANY +short @ns1-1.akamaitech.net ANY whoami.akamai.net
OR
dig ANY +short @resolver2.opendns.com myip.opendns.com
Show the IP address using wget command
As of now, we know how to display the public IP address using curl command and dig command. Here, we will discuss how to display the IP address using wget command.
The wget command is used to download the files from the terminal, resume the download, download the entire website and many more. It supports the protocols such as HTTPS, HTTP, FTP, and many more. Learn the tutorial on how to use wget command in Linux for more details.
We will use wget command with -qO- option. The “-q” enables the quiet mode while “-O” send the output to the standard output i.e. STDOUT. Let’s have a look at the command below”
wget -qO- ifconfig.me
As discussed earlier, ifconfig.me is a web site that returns your public IP. You can see the public IP address of the Linux system.
How to find your Local/Private IP address in Linux?
The IP address that belongs to your Local area network is considered as private IP. Here we will learn how to find your private IP address. We can find it using ifconfig, ip, or hostname commands. Let me demonstrate all the commands here. You will notice that my public and Private IPs are the same which is because I’m demonstrating these commands on a Virtual Private Server. The IP addresses will be different for you if you’re performing the commands on a machine that’s connected to a router.
Find IP using the hostname command
Here, we will discuss how to view the private IP address using the hostname command. The hostname command is used to either set the name of the system or display the name of the system. It displays the DNS name. We will use the hostname command with the “-I” option. The “-I” option is used to display all the network addresses of the host.
For more details, check the Hostname Official Documentation.
Let’s have a look at the command below:
hostname -I
It has displayed all the configured network addresses of network interface such as wifi or router.
Show IP address using ifconfig command
To view the private IP address, we can use the ifconfig command. It will display all the configured IP addresses of network interface. Let’s have a look at the command below:
ifconfig
You can see that it has displayed all the IP addresses. If you want to display the IP address of particular network device, just mention the interface name after ifconfig command. To do so, type the following in the terminal:
ifconfig eth0
For more details, check the ifconfig Official Documentation.
View the IP address using ip command
Earlier we discussed how to view private IP address using ifconfig command and Hostname command. Here, we will discuss how to show IP address using IP command. IP command displays all the related information regarding IP address. It is more powerful than ifconfig command.
Check the difference between IP command and Ifconfig command.
Here, we will use addr option with IP command. You can also use address or addr show. Let’s have a look at the command below:
ip address show
OR
ip addr show
OR
ip addr
Check the tutorial on how to use IP command in Linux.
Conclusion
That’s it. We have successfully learned how to view public and private IP addresses and find your IP address in Linux. Both have different methods. You can view public IP address using curl , wget and dig command while private IP address using ifconfig, IP and hostname command. If you still have any doubt, do let us know in the comment section.