Enable/Disable Firewall and Modify Remote Port in Ubuntu 18.04-22.04

In Ubuntu 18.04-22.04, the default firewall system is ufw (Uncomplicated Firewall). Taking Ubuntu 18.04 as an example, it can be configured as shown in the following picture:

1. Check the Firewall Status (Default is Disabled)
#ufw status 

 

2. Enable the Firewall

#ufw enable

3.Disable the Firewall

#Ufw disable 

4.If the firewall is enabled, the commands to allow a specific port are as follows:

ufw allow 80/tcp                         ##Allow port 80 
ufw allow 22/tcp                         ##Allow port 22 
ufw allow 8112/tcp                     ##Allow port 8112 
ufw allow 1000:2000/tcp            ##Allow the port range 1000 to 2000.

 

5.The commands to delete an allowed port are as follows:

ufw delete allow 80/tcp               ##Delete the rule allowing port 80
ufw delete allow 22/tcp               ##Delete the rule allowing port 22 
ufw delete allow 1000:2000/tcp   ##Delete the rule allowing the port range 1000:2000

 

6.Modify the remote SSH port, restart the SSH service, and allow the new remote port.

For example, if the current remote port is 22 and you need to change it to 2882, the commands are as follows:
sed -i 's/^Port 22/Port 2882/' /etc/ssh/sshd_config     ##Modify the remote port 
systemctl restart sshd                 ##restart the SSH service 
ufw allow 2882/tcp                     ##allow the new remote port

7.Use the new port for remote login.

ssh root@ specific IP -p 2882 

 

(The above operations are also applicable to Debian 10-12 systems.)

  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

CentOS 7.x Firewall: Enable/Disable and Add Ports

  In CentOS 7.x, the default firewall is firewalld. Here is an example using CentOS 7.6...

How to enable/disable ping on Linux system

To prevent others from discovering and potentially attacking your machine through network ping...

How to View Memory Usage and Clear Cache in Linux System

Sometimes, when a server becomes unresponsive, it may not necessarily be due to high CPU usage....

How to remotely access a Linux system using Putty tool

Introduction to Putty software PuTTY is a Telnet, SSH, rlogin, raw TCP, and serial interface...

Linux System RuiSu Installation Tutorial

Note: The prerequisite for installing RuiSu is to ensure that the network card is in the "eth"...