How to Check if Port is Open or Closed on a Linux Server

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.

What is Port in Linux?

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:

Check Open Port with netstat command

The netstat is a very useful Linux command line utility. Besides, it helps to show information about network connections, routing tables and ports. By default, the netstat utility is not present in modern Ubuntu based distributions. So you need to install it using the apt command.

				
					apt install net-tools -y
				
			

After the successful installation, run the following command to check all open ports in your system.

				
					netstat -lntu
				
			

You should see all open ports in the following screen.

A brief explanation of each option is shown below:

  • -n: Show open ports.
  • -t: Used to show TCP ports.
  • -u: Show listening UDP ports.

Also filter the resulting output using the grep command. For example, to check that port 80 is open or closed then run the following command.

				
					netstat -lntu | grep :80
				
			

If port 80 is open, you should see the following output.

				
					tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
				
			

You don’t see any output., if port 80 is closed.

Check Open Port with SS command

Moreover, the ss is a common command line utility in Linux. Mainly used to identify the open port and socket in your system.

Let’s run the ss command with tulpn option to check all open ports including, TCP and UDP in Linux.

				
					ss -tulpn
				
			

You see all open ports on the following screen.

A brief explanation of each option is shown below:

  • -n: Don’t resolve service names.
  • -t: Display TCP ports.
  • -u: Display UDP ports.
  • -l: Shows listening sockets.
  • -p: Display processes and sockets.

Check Open Port with lsof command

Meanwhile, the lsof also called “list open files” is another command line tool used to check open ports in Linux.

In order to check all open ports, run the following command.

				
					lsof -i -P -n | grep LISTEN
				
			

See all open ports on the following screen.

Check Open Port with nmap command

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

				
			

Check Open Port with Netcat command

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
				
			

Check Open Port with Telnet command

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.

Check Open Port with PowerShell

By default, the PowerShell package is not included in the Linux default repository. Though you need to install it using the Snap package manager.

Please run the following command to install the PowerShell package on your system.

				
					snap install powershell --classic
				
			

Once the PowerShell package is installed, launch it with the following command.

				
					pwsh
				
			

Once you are connected to the PowerShell, you should see the following output:

				
					PowerShell 7.3.2
PS /root>

				
			

Next, create a checkport.ps1 file to check the open port.

				
					nano checkport.ps1
				
			

Add the following code:

				
					[CmdletBinding()]
[OutputType([System.Management.Automation.PSCustomObject])]
param (
[Parameter(Mandatory)]
[string[]]$ComputerName,
[Parameter(Mandatory)]
[int[]]$Port,
[Parameter()]
[int]$TcpTimeout = 1000
)
begin {
$Protocol = 'TCP'
}
process {
foreach ($Computer in $ComputerName) {
foreach ($Portx in $Port) {
$Output = @{ 'Computername' = $Computer; 'Port' = $Portx; 'Protocol' = $Protocol; 'Result' = '' }
Write-Verbose "$($MyInvocation.MyCommand.Name) - Beginning port test on '$Computer' on port '$Protocol<code>:$Portx'"

$TcpClient = New-Object System.Net.Sockets.TcpClient
$Connect = $TcpClient.BeginConnect($Computer, $Portx, $null, $null)
$Wait = $Connect.AsyncWaitHandle.WaitOne($TcpTimeout, $false)
if (!$Wait -or !($TcpClient.Connected)) {
$TcpClient.Close()
Write-Verbose "$($MyInvocation.MyCommand.Name) - '$Computer' failed port test on port '$Protocol</code>:$Portx'"
$Output.Result = $false
}
else {
$TcpClient.EndConnect($Connect)
$TcpClient.Close()
Write-Verbose "$($MyInvocation.MyCommand.Name) - '$Computer' passed port test on port '$Protocol<code>:$Portx'"
$Output.Result = $true
$TcpClient.Close()
$TcpClient.Dispose()
}
[pscustomobject]$Output
}
}
}

				
			

Save and close the file when you are done.

Then, run the following command to check if port 80 is open or closed.

				
					./checkport.ps1 -ComputerName localhost -Port 80
				
			

If port 80 is open, you should see the following screen.

Also check the multiple ports using the following command.

				
					./checkport.ps1 -ComputerName localhost -Port 80,22,25,9099

				
			

This shows you the list of open and closed ports in the following screen.

Thank you for reading How to Check if Port is Open or Closed on a Linux Server. We shall conclude this article now. 

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.

Avatar for Hitesh Jethva
Hitesh Jethva

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.

5 1 vote
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x