How do I whitelist multiple IP address at the same time with Ubuntu 21.04?

I have to whitelist multiple IP addresses for CDN. How would I do this for about 50 IP addresses? I am using Ubuntu 21.04.

2 Replies

Linode Staff

If you are using UFW to manage your firewall configuration, you could use a bash script to automate this process for you. Here's an example for how you might do so:

From your Linode, create a text file and then list the IP addresses you wish to allow, one per line. Here's an example I made using a random IP address generator:


After adding the list of IP addresses, you'll want to save this file and then create a new script file. We're going to use the bash shell to loop over the IP addresses in this list and insert each one into the ufw allow command.


#!/bin/sh for x in `cat ./iplist.txt`; do ufw allow from $x; done ufw reload > /dev/null

Once you've saved this file, you'll want to make it executable via chmod 700 Then you can run it by typing ./ This will add a new UFW allow rule for each of the IP addresses you've listed in the txt file. It's worth noting that your user account will need access to set UFW rules. When the script completes, you can review the current rules by running the ufw status command.

You can modify the content of the script to allow access only on specific ports by appending to any port $number to the end of the ufw allow command in the script. For example if you wanted to allow only port 80 traffic from the IP addresses you'd want to configure the script to ufw allow from $x to any port 80.

Hope that helps as a starting point.

Note, that if you use ufw you will be stuck with Ubuntu for the rest of your natural life. If you decide to move your project to another distro, you’ve just signed yourself up for a lot of tedious porting work.

IMHO, better to do all that boring stuff up front and write your script to use iptables directly and future proof your work. @bd5k’s ideas would till apply. ufw is Canonical’s patent-medicine “friendly front-end” to iptables that has one purpose only — make it hard to port your stuff to another distro.

Like patent-medicines (that make you feel good for awhile because of their high grain alcohol content), use of ufw is a powerful inductor of a splitting headache.

— sw


Please enter an answer

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct