How do I add OpenSSH to ufw?

I know how to add OpenSSH to ufw using the ufw allow ssh but I need to make sure only CIDR can access the port. What do I do? How do I tell OpenSSH to allow ssh access from the specific IP address range and CIDR? Is it possible?

ufw syntax to set up a ufw and open port 22/80/443

The syntax is simple

# ssh port
sudo ufw allow from CIDR to any port 22
# http port
sudo ufw allow from sub/net to any port 80
# https port
sudo ufw allow from to any port 443

Allowing ssh connections

Here is how to configure UFW to allow incoming SSH connections is by referencing its CIDR and with a comment:

export CIDR=""
sudo ufw allow from $CIDR to any port 443 comment "open openssh for $CIDR"

Verifying the rules ufw ssh rules

ufw status
ufw status verbose
ufw status numbered 
ufw show added
1 Like

Do check my guides:

1 Like

I accept @monk’s answer as that it what I was looking for :grinning:

Linux sysadmin blog - Linux/Unix Howtos and Tutorials - Linux bash shell scripting wiki