How to Find/Get your IP Address in Linux

Updated on

3 min read

Find  IP address in Linux

Knowing the IP address of your device is important when troubleshooting network issues, setting up a new connection, or configuring a firewall.

IP Addresses can be classified into two categories, public and private. A public IP is an IP Address that is unique and can be accessed from the Internet. Private IP addresses are reserved for internal use within your private network without being directly exposed to the Internet. Furthermore, there are two types of IP addresses, IP version 4 (IPv4), and IP version 6 (IPv6).

This article explains several different methods of determining the public and private IP Addresses of a Linux system.

Find Your Private IP Address

Private IP addresses are not routable over the Internet and are meant to work within the local network only. Typically, a private IP address is assigned to each device inside your local network by your router. This provides a unique IP address for all devices within the local network, such as your phone, laptop, smart TV, printer, media center, etc.

Devices on the local network are connecting to the Internet through NAT (network address translation).

The following IPv4 address ranges are reserved for the private networks:

  • 10.0.0.0/8
  • 172.16.0.0/12
  • 192.168.0.0/16

You can determine your system private IP address by querying the network stack with commands such as ip, ifconfig or hostname.

In Linux, the standard tool for displaying and configuring network interfaces is ip .

To display a list of all network interfaces and the associated IP address type the following command:

ip addr

The output will look something like below. The private IP address is highlighted.

Private IP address

You can also use the following commands to display the private IP address:

hostname -I
ifconfig

Find Your Public IP Address

A Public IP address is a globally routable IP address that is assigned to a network device, allowing it direct access to the Internet. They are assigned to the device by its ISP, and each device has a unique public IP address.

The public IP addresses are used by home routers, web servers, mail servers, and so on.

Determining the public IP address involves contacting a remote server over the HTTP/HTTPS or DNS protocol and obtaining the IP address from the remote server response.

On Desktop machines, the easiest way to find out your public IP address is to type “what is my ip” in your browser:

What is my ip

If you are on a headless Linux server or you want to assign the IP address to a shell script variable you can use command-line tools such as dig , curl and wget .

Most of the DNS providers, such as OpenDNS and Google allows you to query their servers and obtain your public IP address.

You can use any of the commands below to get your IP:

  • dig ANY +short @resolver2.opendns.com myip.opendns.com
  • dig ANY +short @resolver2.opendns.com myip.opendns.com
  • dig ANY +short @ns1-1.akamaitech.net ANY whoami.akamai.net

There are many online HTTP/HTTPS services that respond with your public IP address. Here are some of them:

  • curl -s http://tnx.nl/ip
  • curl -s https://checkip.amazonaws.com
  • curl -s api.infoip.io/ip
  • curl -s ip.appspot.com
  • wget -O - -q https://icanhazip.com/

If any of the commands above is not working, there may be a problem with the online service.

You can even create an alias in your ~/.bashrc or ~/.zshrc file, so you don’t have to type and remember a long command. For example, you can add the following alias:

alias pubip='dig ANY +short @resolver2.opendns.com myip.opendns.com'

Now, whenever you need to find your public IP just type pubip in your terminal.

Conclusion

We have shown you several different commands and online services that you can use to find out your private and public IP address.

If you have any questions or remarks, please leave a comment below.