How to Get IP Address in Bash [3 Methods]

IP address is crucial for configuring network settings on a device. A user may need the IP address in setting up network connections and troubleshooting network-related issues. There are many ways to get IP address using Bash script. In this article, I am going to talk about those.

Key Takeaways

  • Finding device IP address.
  • Difference between public and private IP.
  • Extracting different types of IP address.

Free Downloads

What is IP Address?

IP address is a unique address for every device connected to the internet or in a local network. There are mainly two types of IP addresses. IPv4, IPv6. Both public and private IP addresses can exist within the space of  IPv4 or IPv6. For example, an IPv4 address might look like 192.168.0.1.

Public vs Private IP Address

Public and private IP addresses are two distinct types of addresses used in computer networks. Here’s a short overview.

  • Public IP Address

A public IP address is globally unique and assigned to a device directly connected to the internet. It allows communication between devices on the internet and is routable across the public internet. Public IP addresses are obtained from Internet Service Providers (ISPs) and are registered with regional internet registries. They are visible to the public internet and can be used to access devices or services from anywhere on the internet.

  • Private IP address

A private IP address is used within a local network (e.g., home network, office network) and is not directly reachable from the internet. It uniquely identifies devices within a private network and allows internal communication between devices. Private IP addresses are defined by private IP address ranges reserved for local network use, as specified by the Internet Engineering Task Force (IETF) in RFC 1918. They are not directly accessible from the internet unless a mechanism like Network Address Translation (NAT) is used to translate between private and public IP addresses.

3 Methods to Get IP Address in Bash

There are multiple commands available in the Unix operating system that can extract the IP address. In the following methods, I am going to discuss five different ways to get the IP address of a device.

You can read the Comparative Analysis of Methods to find the easiest one.

Method 1: Using the “hostname” Command to Get IP address

The hostname command is used to find the hostname of a device. However one may use the -i option to find the device IP address using this command.

Steps to Follow >

❶ At first, launch an Ubuntu Terminal.

❷ Write the following command to open a host.sh file in the built-in nano editor:

nano host.sh
EXPLANATION
  • nano: Opens a file in the Nano text editor.
  • host.sh: Name of the file.
Creating a .sh file in nano❸ Copy the following script and paste it into nano. Press CTRL+O and ENTER to save the file; CTRL+X to exit. Alternatively, copy the following script. Paste the script in a text editor and save it as .sh file.
#!/bin/bash

# Retrieve the IP address
ip_address=$(hostname -I | awk '{print $1}')

# Display the IP address
echo "IP Address: $ip_address"
EXPLANATION

The provided Bash script retrieves the IP address of the machine and displays it. It does this by using the hostname -I command, which retrieves a list of IP addresses associated with the machine. The output of this command is then piped to the awk command, which prints the first field from the list. This selected IP address is assigned to the ip_address variable using command substitution. Finally, the script uses the echo command to display the IP address.

❹ Use the following command to make both file executable:

chmod u+x host.sh

Changing permission of a Bash script

EXPLANATION
  • chmod: Changes permissions.
  • u+x: Giving the owner executing permission.
  • host.sh: Name of the script.

❺ Run the host.sh script by the following command:

./host.sh

Finding IP address using hostname commandRunning the script gives the following IP address of the device as indicated in the image. If if confiq is not found in your system, install it using the following command:

sudo apt install net-tools

Method 2: Using the “ifconfig” Command to Get IP Address

Using the ifconfiq command in association with other commands can extract the IP addresses of a device. Look at the following script for a better understanding.

You can follow the Steps of Method 1 to learn about creating and saving shell scripts.

Scripts (ifcon.sh) >

#!/bin/bash

ip_address=$(ifconfig | grep 'inet ' | awk '{print $2}')
echo $ip_address
EXPLANATION

The provided Bash script retrieves and displays the IP address of a specified network interface. By default, it finds both loopback and private IP of the device. The script uses the ifconfig command and to gather interface information and then employs awk to filter and extract the line containing the IP address. Finally, it echoes the interface name with the corresponding IP address.

Run the script by the following command:

./ifcon.sh

Getting IP address using the ifconfiq commandThe program gives IP addresses IP 192.168.0.123 and 127.0.0.1. The IP address 192.168.0.123 refers to the private IP of the device. And 127.0.0.1 refers to the loopback IP of the current device itself.

Method 3: Using the “IP” Command to Get IP Address in Bash

ip is another command to find different network interfaces. In this method, I will show you how to get the loopback IP address of a device.

of specific ethernet interfaces(like eth0, eth1)

You can follow the Steps of Method 1 to learn about creating and saving shell scripts.

Scripts (ip.sh) >

#!/bin/bash

interface="lo"  # Replace with your desired interface name
ip_addr=$(ip addr show "$interface" | awk '/inet / {print $2}')
echo "IP address of $interface: $ip_addr"
EXPLANATION

The provided Bash script retrieves and displays the IP address of a specified network interface. By default, it finds the IP address of the loopback interface (lo). The script uses the ip addr show command to gather interface information and then employs awk to filter and extract the line containing the IP address. Finally, it echoes the interface name with corresponding IP address.

Run the script by the following command:

./ip.sh

Getting IP address using the ip commandThe program gives the loopback IP 127.0.0.1/8. The IP address 127.0.0.1 commonly refers to the local host or the current device itself. “/8” is the subnet mask that specifies the network range associated with the IP address.

Note: Modifying the interface variable allows you to find the IP address of a different network interface. To get the IP address of Ethernet interfaces  use (eth0, eth1, etc.), Wi-Fi interfaces (wlan0, wlan1, etc.) and loopback interfaces (lo) in the interface variable.

Comparative Analysis of Methods

Here is a comparative analysis between the above-discussed methods. Take a look at the analysis to determine which method suits your needs best.

Method Advantage Disadvantage
hostname command
  • Short and easy to execute
  • Need to extract proper field.
ifconfiq command
  • Simple and easy to extract network related information
  • Need to use grep command
  • May not available in the system
ip command
  • More available than

ifconfiq command

  • Need to use

awk command

Conclusion

In conclusion, there are commands for finding the IP address associated with a device. However, you must have a clear understanding of network interfaces and other network related topics to adroitly use those commands.

People Also Ask

How to get public IP address only in Bash?
Use curl command with HTTP request to get public IP address.
Can I find IP address from hostname using Unix-like?
YES. you can find the IP address associated with a hostname using the host or nslookup command in Unix operating system
How to find all IP address using the hostname command?
You can find all IP address using hostname command with -I option.

Related Articles


<< Go Back to Bash Script Examples | Bash Scripting Basics | Bash Scripting Tutorial

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
icon linux
Md Zahidul Islam Laku

Hey, I'm Zahidul Islam Laku currently working as a Linux Content Developer Executive at SOFTEKO. I completed my graduation from Bangladesh University of Engineering and Technology (BUET). I write articles on a variety of tech topics including Linux. Learning and writing on Linux is nothing but fun as it gives me more power on my machine. What can be more efficient than interacting with the Operating System without Graphical User Interface! Read Full Bio

Leave a Comment