An IP (Internet Protocol) address is a unique identifier identifying a device connected to the internet or a local network. It acts as an identifier that permits data to be sent between devices on a network. They contain location information and make devices accessible for communication.
This article will look at the various ways to find the IP address of a network interface in Linux.
Types of IP addresses
The two main Internet protocols are Internet Protocol version 4 and Internet Protocol version 6. The two are commonly abbreviated as IPv4 and IPv6. IPv6 is the newest Internet protocol and is way better in terms of efficiency and complexity.
What is IPv4?
IPv4 is an internet protocol version widely used to identify devices on a network using an addressing system.IPv4 uses 4 bytes (32 bits). These bytes are regularly called octets, and for coherence, these bytes, bits, and octets are written in dotted decimal. Dotted decimal isolates every octet of the IP address with a decimal point.
Features of IPv4
- IPv4 has a 32-bit address length.
- IPv4 does not offer encryption and authentication facilities.
- IPv4 has a connectionless protocol.
- IPv4 requires less memory and ease of remembering addresses.
- It also allows the creation of a simple virtual communication layer over diversified devices.
What is IPv6?
IPv6 is an internet protocol that has been deployed to replace IPv4.IPv6 uses a 128-bit address length, which has helped solve the problems of limited IP addresses, as with IPv4.IPv6 supports auto and renumbering address configuration.
Features of IPv6
- IPv6 offers hierarchical addressing and routing infrastructure.
- IPv6 has stateful and stateless configurations.
- In IPv6, encryption and authentication are provided.
- IPv6 has a 128-bit address length.
- In IPv6, there is end-to-end connection integrity.
The difference between a private and a public IP address
Private and Public IP addresses uniquely identify machines within a network. A private IP address identifies a device within a local network, while a Public IP address identifies a device outside a network. A public IP address is usually provided to you by the internet service provider (ISP).
Finding the IP address in Linux
Linux offers a range of options to determine the system’s IP address. There are two commonly known ways to determine the IP address in Linux.
These methods are:
- Use of CLI commands.
- Use of GUI network-manager
Find the IP address of a networking interface using CLI commands.
You can use various commands to find the IP address of a networking interface in Linux using the command line (Terminal). In this article, we will explore some of the most used CLI commands for finding IP addresses.
CLI Command for getting Private IP addresses:
To get the Private IP address of a system, use the following commands below.
-
The
ifconfig
Command
ifconfig
is a command-line utility used to list and configure network interfaces on your system. However, despite this command being marked as ‘deprecated’ and being replaced by the IP command in the 90s, ifconfig
is still the go-to command for most users.
Execute the command below to get private IP assigned to your network interface.
ifconfig -a
Tip: If you encounter an error like ‘ifconfig command not found,’ run the command to install net-tools on your system.
sudo apt install net-tools
You will need to run the ifconfig
command from the /sbin
directory for some distributions, as shown below.
/sbin/ifconfig -a
-
Use the ip command
The ip
command is the successor of the ifconfig
utility and comes pre-installed in all Linux distributions. It has many more additional features, including configuring addresses and routes and managing the ARP cache.
Execute the command below to get the private IP address using the ip
utility.
ip addr or ip a
Another ip
command you can use to retrieve the IP address is:
ip route get 1.2.3.4 | awk '{print $7}'
-
The hostname command
The hostname is a universal Linux command that you can use to perform various functions depending on the parameter passed. You can use it to set the system hostname, obtain DNS information and even retrieve the assigned IP address.
To get the IP assigned to our system, we will use the -I parameter shown below.
hostname -I
Alternatively, if several network interfaces are connected, you can print only the first address by piping the output to the AWK command, as shown below.
hostname -I | awk '{print $1}'
-
Use the
nmcli
utility
nmcli
is a powerful command-line utility used to manipulate network interfaces. You can use it to list all network interfaces, show network device status, configure network connections (create, edit, activate, and deactivate), and retrieve the assigned IP address.
To get the IP address using nmcli
, execute the command below.
nmcli -p device show
CLI commands for getting public IP addresses
Getting the public IP address can be a little tricky compared to getting the private IP address. There are two main methods that you can use:
- Use external-web based services (most popular and recommended)
- Use system-based commands
Get public IP with external web-based systems
We will use two main commands for this step—the curl and dig utilities. Depending on your Linux distribution, you can install them using the commands below.
Debian-based systems:
sudo apt install curl sudo apt install dnsutils
RHEL-based systems:
yum install curl yum install bind-utils
ArchLinux:
pacman -Sy curl pacman -Syu bind
OpenSUSE:
zypper install curl sudo zypper in bind-utils
cURL (client URL) is command-line tool developers use to send data to or from the server. It supports various protocols, including HTTP methods, SMTP, FTP, and POP3. Execute any of the cURL commands to get the public IP address.
curl ifconfig.me curl -4/-6 icanhazip.com curl ipinfo.io/ip curl api.ipify.org curl checkip.dyndns.org host myip.opendns.com resolver1.opendns.com curl ident.me curl bot.whatismyipaddress.com curl ipecho.net/plain
Another command-line utility you can use is the dig (Domain Information Groper) command to retrieve information from DNS servers.
Use the command below to the public IP address with the dig command.
dig +short myip.opendns.com @resolver1.opendns.com
Finding the IP address of a networking interface using GUI
To find the IP address of a networking interface using a Graphical User Interface, follow the steps below.
- Launch the ‘Settings’ app from the applications menu and select ‘Network.’
2. You will see a list of network interfaces available on your system. Click on the gear icon next to the interface connected to the network. In our case, we are connected via Ethernet (wired), as shown above. A window will pop up with the details of your network connection, including the IP addresses.
Conclusion
This post has given you a comprehensive guide on IP addresses and how to find the private IP and public IP assigned to your Linux system. We have looked at both command-line and graphical methods. However, we highly recommend using the CLI tools since they will give you much more important information other than the IP address. Is there any method we left that you wish to share with our readers? Please, leave a comment below.