ipsets with UFW
ToThis haveis an example that lets you use UFW (https://wiki.ubuntu.com/UncomplicatedFirewall) and ipsets to dynamically whitelist IPs for a common host-based firewall.
First you need a systemd script that creates an ipset
on startup and then have a script that integrates this ipset
with UFW to allow specific traffic (like an SSH knock), you'll need to create two main components:
- A systemd service script that creates and loads the
ipset
at startup. - A UFW application profile or direct rules that utilize the ipset for allowing specific traffic.
1. Systemd Service for Ipset
Create a systemd service that will run at startup to create and populate your ipset
. Here's how you can create this service:
Step 1: Create the ipset
Script
First, create a script that will define and load your ipset
. Let's call this script create_ipset.sh
.
/usr/local/bin/create_ipset.sh:
#!/bin/bash
# Name of the ipset
IPSET_NAME="knocknoc"
# Flush if exists and create
ipset ipset -exist flush $IPSET_NAME
ipset -exist create $IPSET_NAME hash:hash:ip
# Optionally, add IPs here or via separate mechanism
# ipset add $IPSET_NAME 192.168.1.100