After a delayed flight from Montevideo in Uruguay I decided to improve my networking Linux skills and teach myself a bit more about how to check if a port is in use on Linux.

I must admit, I am not a networking person. To be honest, I don’t really like that branch in the tech world. I also, know this is super important to master. For now, I’m OK understanding and able to troubleshoot most common networking issues on Linux, macOS and Windows.

Check if port is in use on Linux

All commands are to be run using our friend the Linux Terminal. 🙂 I use bash.

lsof command

This tool was not installed on my Linux box. You can install it using yum/apt/or whatever package manager you’re using. Here are a couple sample commands.

To view if a specific port is open:

sudo lsof -i:22

View all ‘Listening’ ports:

sudo lsof -i -P -n | grep LISTEN

You should get an output similar to this:

check port is in use lInux
lsof command output

netstat command

The netstat command is one I have used in the past on a Windows box, but always found it a bit complicated to understand the output. Anyways, below some sample outputs.

To view all open ports:

netstat -tulpn | grep LISTEN

To view if a specific port is open. For example SSH on port 22:

netstat -tulpn | grep ':22'

nmap command

The nmap command I have used it mainly in Ubuntu. I like it because you can scan a whole network and the output looks a bit more organized.

To scan your machine for open ports:

nmap localhost

You should get:

Starting Nmap 7.94 ( https://nmap.org ) at 2023-12-07 16:11 EST
Nmap scan report for localhost (127.0.0.1)
Host is up (0.000034s latency).
Other addresses for localhost (not scanned): ::1
Not shown: 997 closed tcp ports (conn-refused)
PORT     STATE SERVICE
22/tcp   open  ssh
88/tcp   open  kerberos-sec
5900/tcp open  vnc

You can run nmap on another host:

sudo nmap -sT -O 192.168.2.13 (TCP ports)
or 
sudo nmap -sU -O 192.168.2.13 (UDP ports)

Very cool! I hope this brief tutorial helps somebody out there. Contact me if you have questions or like to collaborate. Also, remember to check my coffee mugs and T-Shirt designs. Some are below too.

Leave a comment

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