In this blog post, we will go through the steps on how to get IP address in Linux. We all use the Internet Protocol (IP) in our day-to-day lives, whether we're aware of it or not. For example, every time you type a website name or a search term into your web browser, it always looks up the IP address of the URL and then loads the website you searched.

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

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

Linux find IP address (private and private) commands

Private IP addresses are not routable over the internet and are only meant to work within the local network. Typically, your router assigns a private IP address to each device inside your local area network

This gives you a unique IP address for all your devices within the local area network, such as your phone, laptop, smart TV, printer, media centre, etc. Devices on the local network are connected to the Internet through the NAT (network address translation).

The following commands will help you to check IP address Linux. Note that these commands will show you the public IP address when you enter them in the terminal.

  • curl ifconfig.me
  • curl -4/-6 icanhazip.com
  • curl ipinfo.io/ip
  • curl api.ipify.org
  • curl checkip.dyndns.org
  • dig +short myip.opendns.com @resolver1.opendns.com
  • host myip.opendns.com resolver1.opendns.com
  • curl ident.me
  • curl bot.whatismyipaddress.com
  • curl ipecho.net/plain

The following commands will get you the private IP address (Linux get local IP) 

  • ifconfig -a
  • ip addr (ip a)
  • hostname -I | awk '{print $1}'
  • ip route get 1.2.3.4 | awk '{print $7}'
  • (Fedora) Wifi-Settings→ click the setting icon next to the wifi name that you are connected to → Ipv4 and Ipv6 both can be seen
  • nice -p device show

What are public and private IP Addresses?

Private addresses are those which are used in Local Area Networks (LAN). They are also confined to a specific network as well as an area. Public addresses are essential for establishing external connectivity to other networks such as the "Worldwide Web" (www) of the Internet.

NAT is an interpretation of a private IP to a public one, and this usually consists of three major types: static, dynamic, and PAT. In a static NAT network, one private IP is mapped to one public IP. A private IP address is mapped to a public IP in a dynamic NAT but from a reservoir of various public IP addresses.

How does the Internet Protocol help in networking? 

An IP address is the most direct direction to a computer over a network. Other systems, such as DNS and Avahi, also help route one computer to another. While these technologies help route, the internet protocol combined with these systems makes networking easier and faster. 

TCP

The TCP/IP protocol was invented in 1978 by Bob Kahn, Vint Cerf, and many others. TCP stands for Transmission Control Protocol, the protocol responsible for the credible transmission of data over various networks. TCP generally checks the integrity of transmissions and therefore provides for re-transmission if the packets have not been delivered correctly to the right address.

On the other hand, the IP protocol generally deals with routing packets like those sent by TCP. IP stands for Internet Protocol which clearly defines its rules that let packets be sent from an origin to a destination from computer to computer. 

Under the IP protocol, packets are routed by a path of computers that gets progressively closer to the intended receiver of the packet. A unique IP address identifies each computer or device on the network and is a numeric identifier given to that device on the network.

Linux get IP address - extra tips.

There are many ways that Linux show IP address to its users. Two types of IP addresses are available: a public IP address and a private IP address. The public IP address generally identifies your computer or network from the outside world. 

Your private IP address will identify your machine inside your private network. To get your IP addresses, you can use a mix of commands, particularly ifconfig, IP, or hostname, or make use of graphical environment apps.

How to Find Your Public IP Address

You can discover your public IP address using various commands to connect you to the Internet for running queries.

Getting your Public IP Address with the help of dig Command

The dig command is a DNS lookup utility command used in Linux. Using it, you can look up your public IP address by connecting to many OpenDNS servers. OpenDNS hosts DNS servers that will help you discover the IP addresses of networks available on the Internet. Enter the following dig command:

dig +short myip.opendns.com @resolver1.opendns.com 

As output, you should get back your public IP address from OpenDNS resolvers. Your IP address will be a series of groups of digits of the format 216.58.216.16 or a variation of this sort.

Use of Curl to Return your Public IP Address

The curl command is a networking tool that allows you to interact with as many servers as you can on the Internet. This is a very famous tool and is common amongst all Linux users. 

By Using wget Instead of curl

Wget is an alternate command to curl that is used to download data and interact with many other servers on the web. You can also use wget to retrieve your public IP address.

Using a web browser to get the IP address

You can enter the following URLs in your browser to find your IP address. 

You can also check the opposite websites you have reached at some point in your time and curl to ascertain your IP address within the browser. There are many such conditions, like once you are logged on to a Linux server, where you do not have any access to a graphical interface. In such cases, you can use the shell commands.

You can always retrieve your IP address using the ifconfig command, which includes various flags that filter the contents of your private IP address. You'll Run the subsequent command inside your shell:

ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'|([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'|([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'

IP address - The ultimate output is your private IP address. This may look like 192.168.1.2 etc. We will also filter the ifconfig output during this case using the sed command. It's a utility for pursuing and reworking streams of text.

You can run the subsequent command to urge your private IP address:

ifconfig | sed -En -'s/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p'

Here, we vet filtering ifconfig output to urge your IP address under the TCP/IP protocol. The above commands may fail if you do not have sed or ifconfig installed. In this case, the command fails. Try the hostname command and follow the tactic below.

How to Find your Private IP Address using Hostname Command

The https://linux.die.net/man/1/hostname command refunds the DNS information available from the machine. You'll find your private address by executing the subsequent command in your shell:

$ hostname -I

The above command enumerates all your configured addresses on all network interfaces, including your private IP address.

Get a Private IP Address with the assistance of IP Command.

We can also get the private IP address of an individual by a Linux machine using the IP command. The IP command shows and manipulates the routing, devices, policy routing, and tunnels.

We can use the subsequent variations of the IP command with flags to return to our private IP address. They are;

ip route get 1 | awk '{print $NF;exit}'|$ ip route get 1 | awk '{print $NF;exit}'

ip route get 8.8.8.8 | head -1 | awk '{print $7}'|$ ip route get 8.8.8.8 | head -1 | awk '{print $7}'| cut -d' ' -f8|$ ip route get 8.8.8.8 | head -1 | awk '{print $7}'|$ ip route get 8.8.8.8 | head -1 | awk '{print $7}'| cut -d' ' -f8|$ ip route get 8.8.8.8 | head -1 | awk '{print $7}'|$ ip route get 8.8.8.8 | head -1 | awk '{print $7}'| cut -d' ' -f8

ip route get 8.8.8.8 | head -1 | awk '{print $7}'|$ ip route get 8.8.8.8 | head -1 | awk '{print $7}'| cut -d' ' -f8|$ ip route get 8.8.8.8 | head -1 | awk '{print $7}'|$ ip route get 8.8.8.8 | head -1 | awk '{print $7}'| cut -d' ' -f8|$ ip route get 8.8.8.8 | head -1 | awk '{print $7}'

These commands are the printout of the routing table entries for sending an invitation to all or any alternate servers. This generally involves our private IP address because of the source of the requests.

Finding your Private IP Address from Network Connection

Despite all the above-mentioned steps to find the IP address, we will show you how to get it using the GUI of your Linux distros. 

  • To navigate to your "Menu."
  • To find the "Preferences" tab.
  • to navigate to "Network" or the equivalent for managing your network information
  • You can select the network type (i.e., wifi, Ethernet, or other)
  • You can view your private IP address under the printed information about your IP
  • Suppose these usually won't add a strictly command-line environment but will work on desktop Linux systems. If you're on a command-line only login, you'll get to try the opposite command-based methods above.

Conclusion

We hope that this tutorial helped you to get IP address in Linux systems. Linux is supposedly one of the most preferred software to generate and find an IP address. If one knows all the structures and required data to use Linux, it becomes the best software to find IP addresses. Despite all its complications, Linux provides many ways to find one's IP address, making it more user-friendly than other operating systems.  You can also buy linux server to practice it on a routine basis.