How to find the IP of a local device using nmap

Recently I've been playing with a new Raspberry Pi 4 I bought after I used other one, a 3 model B, for a long time. It is an interesting device for Linux enthusiasts because it is portable, stackable, quite powerful and best of all really cheap to buy and maintain.

The standard installation process involves mounting an SD card and copying the selected OS iso into it. It has an ARM architecture which is important for when choosing the OS, in my case I've been trying Raspbian, Ubuntu, and Arch Linux. All of them have had good performance.

One way to use it is by connecting it directly to a monitor and a keyboard like a common PC, but because it has support for ethernet and wifi, it is very convenient to put it away and run it in headless mode. Especially at the beginning when the wifi connection is not yet configured, can connect it directly to the ethernet and then SSH into it from another laptop.

But this had a problem, without a monitor, it was important to have a way to obtain the IP of the device remotely. Using the command-line, the normal way I've been checking this in Linux when having a monitor and a keyboard has been by running: ip a | grep 192 which will print the device local IP.

For remote devices, there are several options, like checking the web interface of your router if available. There is also a command called nmap which can be used to discover devices connected to the local network. It has many subcommands so here we are just scratching the surface, but it fits very well to solve this problem.

You can scan your whole local network with this command:

sudo nmap -sn 192.168.1.0/24

The result will contain the IP of the connected devices, including the Raspberry Pi in my case, and the computer from where you are running the command. However it is still not completely readable.

We can improve the output with a bit of post text processing, for example I find convenient to run:

sudo nmap -sn 192.168.1.0/24 > /tmp/nmap-result \
  && sed -i "s|Nmap|\nNmap|" /tmp/nmap-result \
  && less /tmp/nmap-result

By saving the output into a file, it is easier to search it or go back to it if necessary. Additionally, by piping the output via sed and adding that extra line break, each device's IP becomes a lot more clear. This is an example of the final output:

Nmap scan report for 192.168.1.54
Host is up (0.11s latency).
MAC Address: XX:XX:XX:XX:XX:XX (Intel Corporate)

Nmap scan report for 192.168.1.56
Host is up (0.11s latency).
MAC Address: XX:XX:XX:XX:XX:XX (Raspberry Pi)

Not only we know which IP is from each device, but we also get the MAC address. With the MAC address of the device we can assign an static IP to that device in the router configuration, so we no longer need to worry about searching it. After it has a static IP, we can also assign it one or more domain names in the /etc/hosts file or in a local DNS server if we have one.

If you didn't know about nmap I hope you found this useful and use it. There is a lot more information about the command in the Arch Wiki post.

Back to the posts list
Loading ...