How to allowing SSH connections with ufw

I know I can use sudo ufw allow ssh command. But, what I need is how to allow ssh from specific sub/net and to only one IP address. I have multiple IP address to server. Here is what I want to do

  Internet -> sub/net (4.5.6.7/29) -> server with_multiple_ips (1.2.3.4)

How do I tell ufw allow ssh to do this for me?

Try the following syntax:

export SRC="4.5.6.7/29"
export DEST="1.2.3.4"
sudo ufw allow from ${SRC} to ${DEST} port 22 proto tcp

To add comment:

sudo ufw allow from ${SRC} to ${DEST} port 22 proto tcp comment "Allow SSH from CIDR ${SRC} to ${DEST}"

See my guides:

And:

1 Like

Everything sorted out. :blush:


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