How to Find your IP Address and MAC Address in Linux

Posted on

There can be many reasons why you want to know your IP address when connecting to a Linux terminal. Say you’re creating a certificate and need to enter the details. Or you’re trying to create a connection between two servers that you’ve connected to remotely and need to get the IP details of one to enter into the other. Whatever the reason, the issue is made a bit complicated by the fact that a typical Linux box will have multiple networking interfaces. To the average person “Get a system’s IP address” has a very simple meaning. Find the address to which incoming communications are sent. Or the address from which outgoing communications are issued.

It’s not just virtual addresses, but MAC addresses as well. In this article we’ll see various ways to hardware and virtual addresses of our networking interfaces.

Getting the IP Address of your Linux Server

Step 1: Get the name of the Network Interface

Whether you’re working remotely via SSH or logged in directly on the console, we first need to find out which network interface is connecting to the Internet on Linux. The overwhelming majority of the time, it will be called “eth0”. But to be sure, we type in the following command:

ip route list | grep default

The “ip route” command lists the routing tables. We search for the “default” entry as shown above. We can see that in this example, the network interface is indeed “eth0” as expected. You might be tempted to think that the IP address next to it is the one we’re looking for, but it’s not!

Step 2: Get the IP Address of the Interface

Now that we have the name of the active network interface, we can find its IP address by typing in the following:

ip addr show

This will show us a list of all the existing network interfaces. We just need to find the one we identified in the first step. In this case, eth0 as shown here:

Find your IP address

We see that “eth0” is the second entry in the list. Below that, we see the word “inet” followed by the IP address that we’re looking for.

As a point of interest, we see that the flags for “eth0” in this case include “LOWER_UP”. It means that the system detects an ethernet cable in the port and moreover, it can also see another device plugged into it at the other end. This is yet another hint that “eth0” is the interface that we’re interested in. But it’s not a guarantee as there can be other connected ethernet interfaces as well!

Finding the MAC Address of the Linux Box

In addition to the virtual IP address, each interface is also associated with a hardware MAC address that it can be necessary to know. As with the IP address, it can be confusing to find the right one because there seem to be so many! To get the MAC address of the interface, we use the following command:

ip link

This will generate an output as shown here:

The “eth0” interface has a line that starts with “link/ether”, followed by the MAC address.

Getting the Remote IP from Which you Connect

Finally, if you’re connected to your Linux box remotely via SSH, you might need to find out your local (remote for the server) IP address. This is obviously different from the server’s IP address. To get that, you can use the following command:

echo $SSH_CLIENT | awk '{ print $1}'

This simply prints the value of the $SSH_CLIENT variable and filters it to find the IP address. It will simply print the IP address of the SSH client on the next line. Clean and simple!

These are the few ways to get the various IP and MAC addresses of the different interfaces on Linux.


Leave a Reply

Your email address will not be published. Required fields are marked *