Linux Commands

How do I check if Port 80 and 443 are Open Linux/Ubuntu?

How do I check if port 80 and 443 are open Linux Ubuntu

It is essential to know the status of Ports as it leads to the Proper Functioning of Applications, and Remote Access as well. Finding out Open Ports is among the crucial concerns of a System Administrator as leaving them unchecked will lead to potential security concerns. Port 443 is used for protected Web Browsing as it transfers data over a protected network whereas Port 80 transfers the Data in Plain text.

This article explains the different methodologies to check if Port 80 or Port 443 is open in Linux/Ubuntu.

How Do I check if Port 80 and 443 are Open in Linux/Ubuntu?

To check if the port is open for remote connectivity of Ubuntu, several methods can be used. Some of the methods to check if a port is open are:

  • “netstat” Method
  • “ss” command Method
  • “lsof” command Method
  • “netcat” Method
  • “nmap” Method
  • “PowerShell” Method

Enabling Port 443 (Prerequisite)

Before diving into the process of checking the status of ports, let’s have a look at how we can enable Port 443 to “LISTEN”. Port 443 is used by web servers in the background and is disabled by default. To enable the port, we have to enable it from the configuration files of any of the running services like Apache or Node. To enable Port 443, first, redirect to the Apache2 directory using the command:

cd /etc/apache2

List the Apache2 directory files and look for the “ports.conf” file using the command:

ls

Open the “ports.conf” file using the command:

sudo nano ports.conf

This will open the text editor for the “ports.conf” file and in our case, only Port 80 is Opened and actively Listening:

Edit the file and add the line below:

Listen 443

Save the file by pressing “ctrl+O” and exit using “ctrl+X”:

Now your Apache2 will use Port 443 for active listening.

The detailed procedure for checking the status of Port 80 and 443 using all the mentioned methods is explained below.

Method 1: Checking Port 80 & 443 Status Using “netstat” Command

The netstat command is used for configuring the network connectivity. To use netstat, install the “net-tools” package first using the command:

apt update -y && apt install net-tools -y

It will take some time to download and install the packages.

Once downloaded, use the command below to get all the port details:

sudo netstat

To get the Port 80 and only the one with LISTEN status, use the command:

sudo netstat -tulpn | grep LISTEN | grep :80

Here, the “-tulpn” flag displays all the listening ports. The “grep” is used to filter and in this case, it will filter the word “LISTEN” as well as display only the items matching “:80” which is the port number.

The output shows that Port 80 is Listening meaning it is Open.

Now to get Port 443 and only the one with LISTEN status, use the command:

sudo netstat -tulpn | grep LISTEN | grep :443

Method 2: Checking Port 80 & 443 Status Using “ss” Command

The ‘’ss” command is used to display the socket statistics. The “ss” command can “List all Sockets”, “List Listening Sockets”, “Display TCP and UDP Sockets”, and can also display “Established Connections”. It is used as an alternative to the “netstat” command as it is simple to use and is faster than the “netstat” command. To use the “ss” command, use the command below:

ss -tulpn

This will display all the Listening Ports:

To display only Port 80, use the “ss” command as follows:

ss -tulpn | grep LISTEN | grep :80

The “-tulpn” flag will display all the listening ports. The “grep” will filter the word “LISTEN” as well as display only the connections matching “:80” which is the port number.

The output above shows Port 80 is Open and is Listening.

Similarly, to find if the Port 443 is open, the command will be:

ss -tulpn | grep LISTEN | grep :443

Method 3: Checking Port 80 & 443 Status Using “lsof” Command

The “lsof” command means “List Open Files” and lists information about files currently opened by a process. Although it is used to list opened and in-process files, it can also be used to display information about network connections such as showing the open sockets. Use “lsof” by running the following command:

sudo lsof -i -n -P | grep LISTEN | grep :80

Here the flag “-P” shows the port number, “-i” shows the open sockets, and “-n” is responsible for showing the IP Address instead of remote hostnames:

Here, the “4u” indicates file descriptors for IPv6, “0t0” shows the status of Port 80 which is currently in LISTEN State, and “31398” indicates the process id.

To find out about Port 443, use the command:

sudo lsof -i -P -n | grep LISTEN | grep :443

Method 4: Checking Port 80 & 443 Status Using “netcat” Method

The “netcat” is another method to check the status of a Port. It is used to analyze network hosts by connecting to a TCP port. To use netcat, use the command given below:

nc -zv <<strong>hostname</strong>> <<strong>port</strong> number>

Here, the “-z” scans for open ports, “nc” is the command for netcat, and “-v” indicates whether the port is open or closed. As we want to check the status of Port 80 on host “taha khan”, the command used will be:

nc -zv tahakhan 80

Similarly, for Port 443, we will use:

nc -zv tahakhan 443

Method 5: Checking Port 80 & 443 Status Using “nmap” Method

The “nmap” method is used to check the Port Status on a Host Machine. The “nmap” command is used as follows:

nmap -p <<strong>port</strong> number> <<strong>host</strong> IP>

Here, the “-p” flag is used to specify the Port.

Before using “nmap” ensure it is installed. If not, use the following command to install:

sudo apt install nmap -y

To use the “nmap” command you need to know the IP Address of the Host Machine. Consider an example in my case where the Host System name is “tahakhan” and the Client is “ubuntu1”. The Host Machine has IP Address “192.168.184.130”. To check the status of Port 80, use the command:

nmap -p 80 192.168.184.130

The status of Port 80 of our Host Machine will be displayed:

Port 80 shows the State as Open meaning it is Listening. Similarly, to find the status of Port 443 of our Host Machine, execute the “nmap” command as shown below:

nmap -p 443 192.168.184.130

Port 443 is open as shown in the State status.

Method 6: Checking Port 80 & 443 Status Using “PowerShell”

With PowerShell in Ubuntu, you can use the TcpClient class to check the open ports. Before checking the connections and open ports, install PowerShell in Ubuntu using the command:

sudo snap install powershell --classic

Launch it using the command:

pwsh

The command line will change indicating PowerShell is initialized:

Create a file now where a script will be added. In our case, we create a file using the “nano” editor:

nano Port.ps1

The “.ps1” extension indicates it is a PowerShell Script.

The text editor will open, and add the below script inside the “Port.ps1” file:

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

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

Save the file using “ctrl+O” and exit the text editor. To test Ports, run the command:

./<<strong>filename</strong>>.ps1 -ComputerName <<strong>hostname</strong>> -Port <<strong>portnumber</strong>> <<strong>portnumber</strong>>...

In our case, Port 80 and Port 443 have to be checked, so specify “-Port 80, 443”

./Port.ps1 -ComputerName tahakhan -Port 80, 443

Executing the command displays the status of both Ports.

The Result “True” means that both Ports are open.

Conclusion

Check the status of Port 80 and 443 on Ubuntu/Linux using “netstat”, “netcat”, “nmap” and various other methodologies. Ports in Ubuntu are primarily used for network communication and configuration. As many functions rely on specific ports to function, it is essential to know the status of Ports. Port configuration can lead to the Proper Functioning of Applications, and Remote Access as well. This article explained various methodologies involving different steps to check the status of specific Ports on a Host System.

Similar Posts