
On a Linux system, closing a particular port might help increase security or free up system resources. This short tutorial will walk you through closing a port using IPtables, FirewallD, UFW, or stopping the service. If you are looking for a tutorial to open a port in Linux, please check our tutorial: How to Open a Specific Port on Linux
How to Check Open Ports on Linux
First, let’s see which ports are open. You can use one of these commands:
Using netstat:
sudo netstat -tuln
Below is a sample output in which underlined in red are the ports which are opened. (22 and 53)

Using ss:
sudo ss -tuln
Below is a sample output in which underlined in red are the ports which are opened. (22 and 53)

Using lsof:
Thelsof
command is mostly not preinstalled on Linux systems. You need to manually install this command to use. If you are using a Debian-based system like Ubuntu, use the command:sudo apt-get install lsof
to install. For Centos/Redhat:sudo yum install lsof
and for Fedora:sudo dnf install lsof
sudo lsof -i -P -n
Below is a sample output in which underlined in red are the ports which are opened. (22 and 53)

These commands will list all the open ports on your system.
How to Close a Port Using UFW (Uncomplicated Firewall)
For Ubuntu and other Debian-based systems, UFW is a straightforward firewall management application.
Install UFW (if it’s not installed):
sudo apt-get install ufw
sudo ufw enable
Close the Port:
Replace <port_number>
with the port number, you want to close.
sudo ufw deny <port_number>
Example screenshot:

Check UFW Status:
sudo ufw status
How to Close a Port Using iptables
A more sophisticated tool for controlling firewall rules is IPtables. To install IPtables use the command: sudo apt-get install iptables
if it was not installed.
Block the Port:
Replace <port_number>
with the port number, you want to close.
sudo iptables -A INPUT -p tcp --dport <port_number> -j DROP
sudo iptables -A INPUT -p udp --dport <port_number> -j DROP
Example screenshot:

Save IPtables Rules:
On Debian-based systems:
sudo sh -c "iptables-save > /etc/iptables/rules.v4"
On Red Hat-based systems:
sudo service iptables save
How to Close a Port Using Firewalld
Firewalld is the default firewall tool for CentOS and Fedora.
Install Firewalld (if it’s not installed):
sudo yum install firewalld
Block the Port:
Replace <port_number>
with the port number, you want to close.
sudo firewall-cmd --permanent --remove-port=<port_number>/tcp
sudo firewall-cmd --permanent --remove-port=<port_number>/udp
Example screenshot:

Reload Firewalld:
sudo firewall-cmd --reload
Check Firewalld Status:
sudo firewall-cmd --list-all
How to Stop a Service to Close a Port
Sometimes, a port is open because a service is using it. Stopping the service will close the port. In that case, blocking the service will be the best option rather than closing the port.
Identify the Service:
Use netstat
, ss
, or lsof
to find which service is using the port.
sudo netstat -tuln | grep <port_number>
OR
sudo ss -tuln | grep <port_number>
OR
sudo lsof -i :<port_number>
For Example:

Replace <port_number>
with the actual port number.
Stop the Service:
Replace <service_name>
with the name of the service.
sudo systemctl stop <service_name>
For example:

Disable the Service (if needed):
This prevents the service from starting at boot.
sudo systemctl disable <service_name>
On Linux, there are multiple methods to close a port: UFW, IPtables, Firewalld, or terminating a service. Select the approach that best suits your needs. For simplicity, UFW is a great choice. For more control, IPtables is ideal. Firewalld is perfect for Red Hat-based distributions, and stopping a service directly is effective when dealing with a specific application. Always ensure you correctly identify the service using the port before stopping it.
Related Blogs: