How to Check if Port is Open or Closed on a Linux Server. When your Linux server is running it is important to know, which port is open on the server’s network or to access it remotely. The task of checking for open or closed port is a fairly common but important task to detect intrusion. This post introduces what is a port and then navigates to different ways to check, if the port is open or closed on a Linux server.
Firstly, a port is a logical entity in computer networking. Primarily used to identify a process or application in Linux operating systems. Well, so ports are given numbers from 0 to 65,535. Numbers between 0 to 1023 of ports are reserved for privileged services and designated as well known ports. Whilst port numbers from 1024 to 65535 are available for applications and are also called registered ports.
So, if you are a system administrator and responsible for managing Linux servers then you may often need to check if the port is open or closed. Moreover, opening or closing a port is another reason to access it from the remote machine.
There are several ways you check open ports in Linux. Importantly, every port is closed unless any application or service starts. After starting service, a port is assigned to it. It helps further, if you use trusted firewall to deny port access from unauthenticated users.
How to Check if a Port is Open or Closed on a Linux Server
Follow this tutorial to learn different ways to check if the open port in Linux.
Prerequisites
A Linux system is installed on your machine.
A root user or a user with sudo privileges.
List All Ports in Linux
Before starting, it is essential to list all available ports in your server. All service and port related information are stored in the /etc/services file. Above all, it contains detailed information on running services and ports in your system.
Let’s check all running services and ports using the nano command.
cat /etc/services | less
This shows you the long list of all available services and ports:
In addition, the Nmap also called “Network Mapper” is a network security tool used to check open or closed ports on the remote system and local system.
By default, the Nmap package is included in the default repository of all major Linux distributions. Install it with the following command.
apt install nmap -y
The basic syntax to use the nmap command is shown below.
nmap [remote-ip-address] [option]
If you want to check all open ports on the local system, run the following command:
nmap localhost
You should see the list of all open ports on the following screen:
If you want to check any specific open or closed port, run the following command.
nmap localhost -p 80
You should see the following output if port 80 is open.
Starting Nmap 7.60 ( https://nmap.org ) at 2023-03-02 10:33 IST
Nmap scan report for localhost (127.0.0.1)
Host is up (0.000095s latency).
PORT STATE SERVICE
80/tcp open http
Nmap done: 1 IP address (1 host up) scanned in 0.44 seconds
In essence, there is Netcat, a simple command line tool used to find open ports in Linux. Used by the system administrators to debug network related issues. By default, this tool is included in the default repository of all major Linux distributions. Well, you install it using the following command.
apt install netcat -y
The basic syntax of the Netcat command is shown below:
nc [-options] [ip-adress] [port-number]
Let’s check, if port 80 is open or closed on the local system.
nc -zvw10 localhost 80
If port 80 is open, you should see the following output.
Connection to localhost 80 port [tcp/http] succeeded!
You should see the following output if port 80 is closed.
nc: connect to localhost port 80 (tcp/http) failed: Connection refused
Specifically, the Telnet is an old command line utility used to check open or closed ports on the remote system. By default, the telnet package is included in the default repository of all major Linux distributions. Therefore, you install it using the following command.
apt install telnet -y
The basic syntax to use the Telnet command is shown below:
telnet [IP address] [Port Number]
To check whether port 22 is open on the remote system, run the following command:
telnet 192.168.52.108 22
If the port is open, you should see the following screen.
You should see the following screen if the port is not open.
How to Check if Port is Open or Closed on a Linux Server Conclusion
In this post, we have explained how to check if a port is open or closed in Linux. Now choose your preferred way to check the open port on a local or remote system. I hope this guide helps you to troubleshoot network connection related issues. Use this guide to find unwanted open ports and block it using the firewall tool.
I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.
51vote
Article Rating
Subscribe
Login and comment with
I allow to create an account
When you login first time using a Social Login button, we collect your account public profile information shared by Social Login provider, based on your privacy settings. We also get your email address to automatically create an account for you in our website. Once your account is created, you'll be logged-in to this account.
DisagreeAgree
Login and comment with
I allow to create an account
When you login first time using a Social Login button, we collect your account public profile information shared by Social Login provider, based on your privacy settings. We also get your email address to automatically create an account for you in our website. Once your account is created, you'll be logged-in to this account.