FUNDAMENTALS A Complete Guide for Beginners
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.
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.
❶ 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
- nano: Opens a file in the Nano text editor.
- host.sh: Name of the file.
#!/bin/bash
# Retrieve the IP address
ip_address=$(hostname -I | awk '{print $1}')
# Display the IP address
echo "IP Address: $ip_address"
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
- 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
Running 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.
Scripts (ifcon.sh) >
#!/bin/bash
ip_address=$(ifconfig | grep 'inet ' | awk '{print $2}')
echo $ip_address
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
The 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)
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"
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
The 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.
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 |
|
|
ifconfiq command |
|
|
ip command |
ifconfiq command |
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
Related Articles
- How to Get Date in Bash [2 Methods with Examples]
- How to Print Time in Bash [2 Quick Methods]
- How to List Users in Bash [2 Easy Ways]
- How to Get Current Time in Bash [4 Practical Cases]
- How to Use Date Format in Bash [5 Examples]
- How to Get Timestamp in Bash [2 Practical Cases]
- How to Copy and Paste in Bash [2 Methods & Cases]
- How to Read Password in Bash [3 Practical Cases]
- How to Send Email in Bash [2 Easy Methods]
- Bash Script to Send Email with Attachment [Step-by-Step Guide]
- How to Find and Replace String in Bash [5 Methods]
- How to Get Script Name Using Bash Script? [3 Easy Ways]
- How to Call Another Script in Bash [2 Methods]
- How to Generate UUID in Bash [3 Simple Methods]
- 3 Easy Ways to Write to a File in Bash Script
- How to Write the Output to a File in Bash Script [5 Practical Cases]
- How to Create a List in Bash Scripts? [2 Easy Methods]
- How to Clear History in Bash [2 Practical Cases]
- How to Clear Screen Using Bash Script? [2 Effective Methods]
- How to Check Ubuntu Version Using Bash Scripts? [2 Methods]
<< Go Back to Bash Script Examples | Bash Scripting Basics | Bash Scripting Tutorial