howtouselinux

3 Easy Ways to Find IP address in Linux

Table of Contents

Recently we got some questions from our readers about how to check ip address in Linux.

  • How can I check my public IP address from the Linux terminal?
  • What is the easiest way to find my IP address in Linux?
  • How do I determine the IP address of a Linux machine?

 

If you’re looking for answers to these questions, then look no further. In this article, we will show you 3 easy ways to find your IP address in Linux.

 

 

The following Linux commands can be used to get ip address.

  • ip addr
  • ip addr show eth0
  • ifconfig -a
  • ifconfig eth0
  • ip route
  • ip route show dev eth0

 

Procedures to find ip address in Linux

You can easily find the IP address in Linux by using ip addr, ifconfig, or ip route commands.

  • Open the terminal application.
  • Type ifconfig -a or ip addr or ip route command
  • Press Enter to run the command.
  • The output will display the ip address for all the network interfaces.

 

What is Ip address?

From Wikipedia, IP address is used in a system for communication. It is a numerical label such as 192.0.2.1. An IP address serves two main functions: network interface identification and location addressing.

An IP address is the identifier that enables our device to send or receive data packets across the internet. It holds information related to our location and therefore making devices available for two-way communication.

Most internet service providers still use IPv4. It’s based on 32 binary bits, consists of four numbers from 0 to 255, and is separated by dots. For example, 103.86.98.1.

Related: Solving a Networking Issue on Linux in 20 minutes

Find IP address with ip addr command in Linux

You can quickly find your ip address in Linux using ip addr command.  All we need is to open the terminal then type ip addr in the prompt. The number next to inet is the IP address. This command will list IP address, MAC address, MTU size and other information about a network interface.

We can also type ip address or ip a for short. Here are the commands we can use to check ip address with ip addr command in Linux.

  • ip addr – Show IP address for all interfaces
  • ip addr show dev em1 – Display information only for device em1
  • ip addr show up – Show IP address for the running interfaces

 

By default, the ip addr command will display ip information of all the network interfaces on the Linux system.

From the following example, there are three network interfaces l0, eth0 and eth1. The ip address for eth0 is 10.1.0.143 and the ip address for eth1 is 10.254.222.37.

# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 9001 qdisc mq state UP group default qlen 1000
link/ether 02:3f:4d:eb:52:e7 brd ff:ff:ff:ff:ff:ff
inet 10.1.0.143/24 brd 10.1.0.255 scope global dynamic noprefixroute eth0
valid_lft 3198sec preferred_lft 3198sec

3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether fa:16:3e:18:3a:76 brd ff:ff:ff:ff:ff:ff
inet 10.254.222.37/23 brd 10.254.223.255 scope global dynamic eth0
valid_lft 70000sec preferred_lft 70000sec

 

 

The ip addr show dev command is used to view the IP address for a specific network interface. The syntax for the command is:

ip addr show dev interface_name

Example:

$ ip addr show dev eth1
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether fa:16:3e:18:3a:76 brd ff:ff:ff:ff:ff:ff
inet 10.254.222.37/23 brd 10.254.223.255 scope global dynamic eth0
valid_lft 69786sec preferred_lft 69786sec

The ip addr show up command is used to list the running interfaces. It will show all of the IP addresses that are currently up on the system.

There are a number of other options that you can use with the ip addr command. For more information, you can type command ip addr help.

Related article: 3 ip commands you need to know about Linux networking

Get Ip address with ifconfig command in Linux

The ifconfig command is another most common command used to view IP related information in Linux. To use this command to find the IP address, just type ifconfig -a in the shell prompt. If we are only interested in certain interface eth0, use: ifconfig eth0

# ifconfig -a
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 9001
inet 10.1.0.143 netmask 255.255.255.0 broadcast 10.1.0.255
ether 02:3f:4d:eb:52:e7 txqueuelen 1000 (Ethernet)
RX packets 1142453 bytes 77488923 (73.8 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 5835535 bytes 8505814381 (7.9 GiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

The ifconfig command is a very power tool. It is a network configuration utility that can be used to configure a network interface. The ifconfig command can be used to set an IP address, netmask, and gateway for a network interface. The ifconfig command can also be used to troubleshoot network issues.

If a network interface is not working, the ifconfig command can be used to check the status of the network interface.

Related: 10 Linux Interview Questions about networking You Need to Know

Get Ip address with ip route command in Linux

The ip route command can also be used to find the IP address of a Linux system. This command will print out the IP address of our system along with other network details. Once we execute the command we will see an output similar to the text below.

$ ip route
default via 10.1.0.1 dev eth0 proto dhcp metric 100
10.1.0.0/24 dev eth0 proto kernel scope link src 10.1.0.143 metric 100

The information shows us the server’s IP routing table and network gateway.

On these lines, the IP address following `src` are the IP(s) configured on the server.

In the example above, we can see that the server has an IP address of `10.1.0.143`.

Related: Step by Step Guide to troubleshoot a network issue in Linux

Check IP address with Bash script in Linux

We can get a list of the network interfaces and IPv4 addresses on our server by running the following command:

ip -4 -o a | cut -d ' ' -f 2,7 | cut -d '/' -f 1
The output lists the interface names on the left and the associated IP addresses on the right.

lo 127.0.0.1
eth0 10.1.0.79

Let’s break down the command:

  • ip -4 -o a : output each record on a single line, replacing line feeds with the ‘\’ character. This is convenient when you want to get ip address in bash script.
  • The cut command is used to extract a subset of the lines from a command output, or to extract a subset of the columns. The “-d” parameter specifies the delimiter that is used to separate the fields. The “-f” parameter specifies the field number(s) that you want to extract. Here’s an example: cut -d’,’ -f1,3 filename.txt This command will extract the first and third column from the lines in the file filename.txt, using commas as the delimiter.

 

Find public IP address in Linux

The way to get your public IP address is to Google it. Type in “what is my IP address” or “how to find my IP address,” and Google will tell you. Your IP address is public to the internet. We can get it from many online tools. It can be easily tracked by others. 

We recommend using VPN service to hide your IP address to prevent misuse of your personal information. When you use a VPN, you don’t connect directly to the internet on your own.

Instead, you first connect to your VPN provider. You only visit sites, download files, stream videos, and access your online bank accounts or credit card portals after connecting through your VPN.

FAQ about ip address 

difference between an ip address and a MAC address in Linux

The main difference between an ip address and a MAC address in Linux is that an ip address is a network address, while a MAC address is a hardware address.

An ip address is used to identify a computer on a network, while a MAC address is used to identify the network interface of a computer.

Additionally, the MAC address is permanent, while the ip address can be changed. You can check this post about how to get the MAC address in Linux.

how to troubleshoot problems in Linux

If you are having problems with your ip address in Linux, there are a few things you can do to troubleshoot the issue.

First, check the status of your network interfaces with the ifconfig or ip command. If one of your network interfaces is not active, try restarting the interface with the ifdown and ifup commands.

If we are still having problems, try using the netstat or ping command to troubleshoot the network issue. The netstat command can be used to display information about all active network connections, the ping command can be used to test connectivity between two computers.

Understanding network interface and Ip address

A Linux server can have more than one network interface. This is often the case with web servers and other devices that need to be able to connect to different networks. For example, a web server may need to be able to connect to both the internet and a local network.

A network interface can have more than one IP address. The first address is called the primary address, while the others are called secondary addresses or aliases. Using secondary IP addresses is common with web servers.

For example, a web server may have a primary IP address that is used to connect to the internet and a secondary IP address that is used to connect to a local network. This allows the server to be reachable from both networks.

In the following example, there are two IP addresses on the physical interface eth0.

eth0 Link encap:Ethernet HWaddr 00:50:56:8F:44:C7
inet addr:10.252.10.51 Bcast:10.252.10.255 Mask:255.255.255.0
eth0:0 Link encap:Ethernet HWaddr 00:50:56:8F:44:C7
inet addr:10.252.10.52 Bcast:10.252.10.255 Mask:255.255.255.0

 

3 Ways to Find MAC Address In Linux

 

David Cao
David Cao

Hey there! I am David, a Cloud & DevOps Enthusiast and 18 years of experience as a Linux engineer. I work with AWS, Git & GitHub, Linux, Python, Ansible, and Bash. I am a technical blogger and a Software Engineer, enjoy sharing my learning and contributing to open-source.