nc-nmap-telnet

3 commands to check a port is open on Linux

During a troubleshooting process, it is essential to check if a port is open or not on a Linux system, before going to any other step, if for example the port is closed, we can all simply open it otherwise we go to the next state of the troubleshooting process.

In this guide, we’ll show you how to verify this using three commands: nc, nmap, and telnet.

Check the remote port with the « nc » command :

nc stands for netcat is a simple Unix utility that reads and writes data over network connections, using TCP or UDP.

Syntax :

# nc [-options] [IP or Host_name] [Port]

In the example below, we will check whether port 21 (ftp) is open or not on the remote Linux server 10.58.33.20.

If the port is open you will get the message below :

[root@server ~]# nc -zvw3 10.58.33.20 21
Connection to 192.168.1.8 21 port [tcp/ftp] succeeded

Otherwise you will get the message below :

[root@server ~]# nc -zvw3 10.58.33.20 21
nc: connect to 10.65.1.25 port 21 (tcp) failed: Connection refused

Check the remote port with the telnet command :

telnet command is used for interactive communication with another host.

Telnet command syntax :

telnet [Host_name or IP_address] [Port]

If the port is open you will get the message below :

#telnet 10.65.1.25 21
Trying 10.65.1.25...
Connected to 10.65.1.25
Escape character is '^]'.

Otherwise you will get the message below :

#telnet 10.65.1.25 21
Trying 10.65.1.25...
telnet: Unable to connect to remote host: Connection refused

Check remote port with the « nmap » command :

Nmap, short for Network Mapper, is a free, open-source tool for vulnerability scanning and network discovery.

Syntax :

nmap [-options] [Nom_Hôte ou IP] [-p] [Port]

If the port is open, you will get the message below :


[root@server ~]# nmap 10.65.1.25 -p 21

Starting Nmap 7.70 ( https://nmap.org ) at 2020-05-06 12:37 IST
Nmap scan report for 10.65.1.25
Host is up (0.00025s latency).

PORT STATE SERVICE
21/tcp open ftp

Nmap done: 1 IP address (1 host up) scanned in 10.08 seconds

If the port is not open, you will get the message below :

[root@server ~]# nmap 192.168.1.8 -p 80
Starting Nmap 7.70 ( https://nmap.org ) at 2019-03-16 04:30 IST
Nmap scan report for 192.168.1.8
Host is up (0.00036s latency).

PORT STATE SERVICE
80/tcp closed http

Nmap done: 1 IP address (1 host up) scanned in 13.07 seconds