How To Check IP Address In Linux

In this guide I will be showing you how to check both your internal and external IP addresses in Linux. The difference between these two types of IP addresses are that external IP addresses are the IP addresses that servers and other computers will see whenever there is a connection, while the internal is for communicating with your switch, router, or other systems on the same network. Only systems on the same network will be able to communicate directly over your internal IP.

Check External IP Address In Linux

If you just need to find out your external IP address (public IP address) then you can do so by running the following command. What this command will do is send a request to our web server which will return your IP address to you. (Source code)

curl -s

If you need to save this as a variable for scripting then please use the following line as the php script purposely adds line breaks for readability sake which may cause conflicts with some scripts.

ipaddr=$(curl -s | grep -E "[0-9]")

Check Internal IP Address In Linux

You can find out not only your internal IP address but also other information such as your subnet mask and more by using either the ifconfig command or the ip command. The ifconfig command is actually being deprecated or replaced by the ip command but it is still more commonly used simply out of habit and scripts being written around it.

So lets get started with the ifconfig method, using ifconfig you can either have blanket information on all of your interfaces including loopback, or you can specify a specific interface (for example wlan0 for wireless or eth0 for ethernet). Here are two examples below.

ifconfig eth0

If you would prefer to use the ip command then the same applies, you can have blanket information on every interface or you can specify which interface you want to check. Here are 2 examples of using the ip command to check your internal IP address in Linux.

ip addr
ip addr show eth0

Display Only IP Address In Linux

You can show your IP address only, without any other information by using the following command piped into awk. You will need to change the interface if you are not checking eth0.

ip addr show eth0 | awk '/inet/ && !/:/ {gsub("/[0-2][0-9]","");print $2}'

If you need to save this output as a variable for scripting purposes you can do so by using the following line.

ipaddr=$(ip addr show eth0 | awk '/inet/ && !/:/ {gsub("/[0-2][0-9]","");print $2}')

I hope this guide showed you how to check your IP address in Linux, please don’t forget to like, comment, or share using the social media buttons below. Thank you!

Leave a Reply

Protected with IP Blacklist CloudIP Blacklist Cloud