How can I test if a port is available on Linux?
On Linux, tools like telnet, nc, ncat, and nmap can be used to test the availability of ports.
- Test the availability of a port using the telnet command:
telnet IP address port number
For example: telnet 192.168.1.1 80
If the port is available, a message indicating successful connection will be displayed; if the port is not available, a message indicating connection failure or inability to connect will be displayed. - Test the availability of a port using the nc command:
nc -z -v IP address port number
For example: nc -z -v 192.168.1.1 80
If the port is available, it will display a message saying “Connection to IP address port number [tcp/] succeeded!”; if the port is unavailable, it will display a message saying “Connection to IP address port number [tcp/] failed: Connection refused”. - Test the availability of a port using the ncat command:
ncat -v IP address port number
For example: ncat -v 192.168.1.1 80
If the port is available, you will see the message “Ncat: Connected to IP address: port number”; if the port is not available, you will see the message “Ncat: Connection refused”. - To test if a port is available using the nmap command:
nmap -p port number IP address
For example: nmap -p 80 192.168.1.1
If the port is available, it will display a message showing the port number/tcp open; if the port is not available, it will display a message showing the port number/tcp closed.
Here are several commonly used methods that you can choose from to test if a port is available.