In this post, I will show you how to manage ufw
rules using a list of IPs by commands.
First, we need to store the list of IPs into a file, IPs.txt
for example:
1.1.1.1 2.2.2.2 3.3.3.3 4.4.4.4 // leave the last line an empty new line
Please note that you must leave the last line an empty new line, read this post to understand why.
The command bellow will print every single line in IPs.txt
file:
while read line; do echo $line; done < IPs.txt
Replace echo $line
with ufw allow from $line to any port 22
, we will have the command that allows the list of IPs to access your server via SSH port:
while read line; do ufw allow from $line to any port 22; done < IPs.txt
To reverse the command above:
while read line; do ufw delete allow from $line to any port 22; done < IPs.txt
More useful ufw commands here.
for line in $(<IPs.txt); do ufw allow from $line to any port 22; done