Scan Ports With netcat Command in Linux

Scan Ports With netcat Command in Linux

Whether you want to use SSH on an alternate port or deploy a web application to a specific port, the first step will always be to check whether the port is being utilized.

The netcat utility is one of the preferred tools to troubleshoot networks and can also be used to scan ports.  

For example, If I want to check whether port number 22 is open on my local VM, I’ll use the netcat command like this:

nc -zvn 192.168.1.6 22
Scan Ports With netcat Command in Linux

And as you can see, port no 22 is open for connections. That was easy.

But wait, there’s more you can do with the netcat command.

Scanning ports with the netcat command

You need to install netcat command first as it doesn’t come preinstalled in many distributions.

For Debian/Ubuntu-based distros:

sudo apt install netcat

For Fedora and RHEL:

sudo dnf install nc 

Now, let’s start with scanning multiple ports.

Scan multiple ports using the netcat command

To scan multiple ports at once using the netcat, you’d need to follow the given command syntax:

nc -zvn <target> port1 port2 port3
Scan Ports With netcat Command in Linux

Here,

  • -z is used to instruct netcat to scan ports without establishing a connection.
  • -v produces more verbose output.
  • -n stops netcat to perform domain name resolution.

Scan ports within a specific range using the netcat command

Indeed, you can use the previous method to scan for multiple ports but what if you want to scan more than 50 or 100 ports? You can define the range.

For example, If I want to scan ports ranging from 1 to 100, this would be my command:

nc -vz -w3 google.com 1-100
sagar@LHB:~$ nc -vz -w3 google.com 1-100
nc: connect to google.com (142.250.183.110) port 1 (tcp) timed out: Operation now in progress
nc: connect to google.com (2404:6800:4009:823::200e) port 1 (tcp) failed: Network is unreachable
nc: connect to google.com (142.250.183.110) port 2 (tcp) timed out: Operation now in progress
nc: connect to google.com (2404:6800:4009:823::200e) port 2 (tcp) failed: Network is unreachable
nc: connect to google.com (142.250.183.110) port 3 (tcp) timed out: Operation now in progress
nc: connect to google.com (2404:6800:4009:823::200e) port 3 (tcp) failed: Network is unreachable
nc: connect to google.com (142.250.183.110) port 4 (tcp) timed out: Operation now in progress
nc: connect to google.com (2404:6800:4009:823::200e) port 4 (tcp) failed: Network is unreachable
nc: connect to google.com (142.250.183.110) port 5 (tcp) timed out: Operation now in progress
nc: connect to google.com (2404:6800:4009:823::200e) port 5 (tcp) failed: Network is unreachable
nc: connect to google.com (142.250.183.110) port 6 (tcp) timed out: Operation now in progress
nc: connect to google.com (2404:6800:4009:823::200e) port 6 (tcp) failed: Network is unreachable
nc: connect to google.com (142.250.183.110) port 7 (tcp) timed out: Operation now in progress
nc: connect to google.com (2404:6800:4009:823::200e) port 7 (tcp) failed: Network is unreachable
nc: connect to google.com (142.250.183.110) port 8 (tcp) timed out: Operation now in progress
nc: connect to google.com (2404:6800:4009:823::200e) port 8 (tcp) failed: Network is unreachable
nc: connect to google.com (142.250.183.110) port 9 (tcp) timed out: Operation now in progress
nc: connect to google.com (2404:6800:4009:823::200e) port 9 (tcp) failed: Network is unreachable
nc: connect to google.com (142.250.183.110) port 10 (tcp) timed out: Operation now in progress
nc: connect to google.com (2404:6800:4009:823::200e) port 10 (tcp) failed: Network is unreachabl

Of course, it’s google, and you can’t expect to have them ports open for you. But you can use this for your server and might find open ports

Seems pretty long list of unavailable ports right? In this case, you can use the grep command to fetch only the open ports:

netcat -w1 -znv 192.168.1.6 1-100 2>&1 | grep succeeded
Scan Ports With netcat Command in Linux

Here,

  • -w1 will force the netcat command to wait for 1 second for each port.
  • 2&1 redirects standard error.

Common Networking Port Numbers in Linux
Here are the common networking ports you’ll encounter in Linux.
Scan Ports With netcat Command in Linux

Wrapping Up

This was a quick guide on scanning open ports using the netcat command. Since you have found the opened ones, perhaps you would like to know how to close those ports.

I hope this guide resolves any queries you previously had and if not, let me know in the comments.

How to Find Open Ports and Close Them in Linux

How to Find Open Ports and Close Them in Linux

So you are dealing with a critical server where you have to maintain security at any cost. And closing ports to block unwanted traffic is the first step you’d take.

sudo ufw deny 80
sudo ufw enable

So this guide will explain how you can find and close open ports in your server.

Find open ports in Linux

In this tutorial, I am going to use the ss command to find open ports.

You can use the -l option with the ss command to get listening ports. But to be more specific, I’m going with -lt to get listening TCP ports:

ss -tl
How to Find Open Ports and Close Them in Linux

Similarly, if you want to have a list of both TCP and UDP in the listening state, you can use the given command:

ss -tul
How to Find Open Ports and Close Them in Linux

And to get the listening port of each service, you can use -n and for more fine-tuned results, you can always use the grep command:

ss -tuln | grep LISTEN
How to Find Open Ports and Close Them in Linux

Enough of finding open ports, let’s jump to how you can close them.

Close open ports in Linux

To close the port, first, you will need to stop the service and to find the service name, you can use the same ss command with -p option:

sudo ss -tulnp | grep LISTEN
How to Find Open Ports and Close Them in Linux

As you can see, the NGINX is utilizing port number 80. So let’s stop it using the given command:

sudo systemctl stop nginx

As it will enable itself on every boot and you can alter this behavior using the given command:

sudo systemctl disable nginx

For better results, I would recommend changing firewall rules.

Here, I’m going to block port no 80 (used by NGINX) in UFW (which is pre-installed in Ubuntu).

First, let’s check the status of UFW:

sudo ufw status
How to Find Open Ports and Close Them in Linux

And if it shows inactive, you can use the given command to enable it:

sudo ufw enable

Now, you just have to pair the deny option with the port number:

sudo ufw deny 80
How to Find Open Ports and Close Them in Linux

And here’s the end result:

How to Find Open Ports and Close Them in Linux

No sign of NGINX!

Wrapping Up

This was my take on how you can find and close open ports in Linux. I hope you will find this helpful.

And if you have any queries, let me know in the comments.