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

How to switch to a different repository for CentOS 8 EOL

 CentOS 8 reached its end of life at the end of 2021. When using yum repositories for...

To change the network interface name in CentOS 7.x

一.modify GRUB 1.Edit the GRUB configuration file by running the command: `vi...

To modify the hostname on CentOS

Method One:   1.Open a terminal or SSH into the Linux system. 2.Log in to the system with...

An incorrect configuration in the /etc/fstab file on the Linux system is causing login issues

Encountering communication issues with the machine, I checked the system error messages through...

How to use the atop monitoring tool in Linux

Atop is a monitoring tool used to monitor resources and processes in Linux systems. It...