How to enable or disable UFW firewall and release port 22
UFW is a simple way to manage firewall in Ubuntu. In this guide, you will see how to check the status, activate or deactivate the firewall and release the port 22/TCP to maintain SSH access to the VPS.
Quick Summary
The safe order to activate the UFW
On a remote VPS, the safest order is to check the status, release the SSH port, and then enable UFW. If you enable the firewall before allowing SSH, it may block the connection to the server itself.
- firewall
- UFW
- Standard SSH
- 22/TCP
- Common system
- Ubuntu
Before activating: be careful with SSH
If you are connected via SSH, release the port used by SSH before runningsudo ufw enable. In this guide we use the default port 22. If your server uses another port, change the number in the commands.
Step by step
- 01
Connect to the server
Access your VPS Ubuntu over SSH using the root user or a user with permission
sudo.ssh usuario@ip-do-servidor - 02
Check current UFW status
Before changing rules, check whether UFW is active or inactive:
sudo ufw statusTo see more details, use:
sudo ufw status verbose - 03
Release port 22 for SSH
To allow SSH access via the default port, release port 22/TCP:
sudo ufw allow 22/tcpOn application-profiled Ubuntu installations, this command can also be used:
sudo ufw allow OpenSSH - 04
Activate UFW
After releasing SSH, activate the firewall:
sudo ufw enableThe system may ask for confirmation. Read the warning and confirm only if the SSH rule is already created.
- 05
Disable UFW when necessary
To temporarily disable the firewall, use:
sudo ufw disableThis command disables UFW, but should not be used as a permanent security solution.
- 06
Check the applied rules
After releasing ports, enabling or disabling the firewall, review the rules:
sudo ufw status verboseIf you need to remove a rule later, list it numbered:
sudo ufw status numbered
Useful UFW Commands
View simple status
sudo ufw statusAllow SSH on port 22
sudo ufw allow 22/tcpEnable or disable firewall
sudo ufw enable
sudo ufw disableSee numbered rules
sudo ufw status numberedGood practices when using UFW
- Release SSH first: on remote VPS, never activate the firewall before allowing its access port.
- Use TCP on port 22: prefer
22/tcpto make the rule clearer. - Open only what is necessary: only release ports used by real services, such as HTTP, HTTPS, SSH or specific applications.
- Match with Fail2ban: firewall controls ports; Fail2ban helps block IPs with repeated login attempts.
Common mistakes with UFW
- Enable before releasing SSH: may take down your access to the VPS.
- Forget custom port: if SSH doesn't use 22, release the correct port.
- Opening too many doors: broad rules increase the attack surface.
- Do not review rules: always check with
sudo ufw status verbose.
FAQ
Should I release port 22 before enabling UFW?
Yes. On a remote VPS, release the SSH port before enabling the firewall to avoid blocking your own access to the server.
What command releases SSH on UFW?
You can use sudo ufw allow 22/tcp to release port 22 via TCP. On some Ubuntu installations, sudo ufw allow OpenSSH also works.
Can I disable UFW without deleting the rules?
Yes. The sudo ufw disable command disables the firewall, but normally keeps the rules configured for when it is enabled again.
How do I view the UFW rules in detail?
Use sudo ufw status verbose to see rules, default policy, and status. Use sudo ufw status numbered if you need to identify rules to remove later.
Conclusion
UFW makes it easy to configure firewall on VPS Ubuntu. To maintain secure access, check the status, open port 22/TCP for SSH, enable the firewall, and confirm the applied rules.
To strengthen security, combine UFW with SSH public key authentication, regular updates, and tools like Fail2ban.
BedHosting LTDA | CNPJ: 60.677.890/0001-00 | BedHosting.com.br
