IPsets with UFW
This is an example that lets you use UFW (https://wiki.ubuntu.com/UncomplicatedFirewall) and IPsets to dynamically allowlist IPs for a common host-based firewall.
This is achieved in only a few steps:
- Install the Knocknoc orchestration agent on the UFW host and enroll it into the Knocknoc server. Follow these steps.
- Edit the IPSet names, as you will likely want others (knoc_ssh/knoc_https are created by default). Do this by editing
/opt/knocknoc-agent/etc/ipset.listand then restarting withsystemctl restart create-ipsets, which leaves existing grants in place - Edit your UFW rules files to make use of the IPSets (/etc/ufw/after.rules).
- You're done!
1. Install the Knocknoc Agent
Install the Knocknoc Agent, which creates basic IPSets for you along with a startup script to initiate them on boot. These IPSets are editable as noted above. You can use the 'knoc_https' one to test access to port 443 on the orchestration-agent machine.
2. Integrating IPset with UFW
For integrating the ipset with UFW, you will typically use direct iptables rules. However, UFW does not directly support ipset in its regular configuration files, so you'll have to add these rules in the after.rules file for them to be applied with UFW.
Add Rules to UFW's after.rules
Edit the /etc/ufw/after.rules file to add your custom iptables rules referencing the ipset.
# At the end of /etc/ufw/after.rules
-A ufw-after-input -m set --match-set knoc_https src -p tcp --dport 443 -j ACCEPTCOMMIT
This rule will allow HTTPS connections on port 443 for IP addresses that are in the knoc_https IPset.
You can do the same to protect SSH, by using knoc_ssh and --dport 22.
Notes and Considerations
- Testing: Always test firewall and startup scripts in a controlled environment before deploying them on a production server. "Cutting your hands off" is too easy if you are not careful! Try it first with a port other than SSH, e.g., 443, and use Netcat in listen mode to verify access.
- Order of Execution: The systemd script needs to execute before UFW starts, hence the
Before=ufw.servicedirective. Ensure that the network is available before the script runs.
With this setup, your system will create and populate the ipset at startup, and UFW will use these sets to allow traffic as specified. This approach provides a balance between the simplicity of UFW and the power of ipset and iptables. Always ensure to test and verify your configuration in a safe environment before applying it to a live system.
UFW + JIT SSH on your Knocknoc Server
You can use UFW on your Knocknoc server to control just-in-time exposure/access of SSH, noting you should have a break-glass method of accessing SSH should you break the firewall or otherwise sever access. Allow-listing a fixed management IP address is an example of this.
Complete this in 3 steps + testing + shifting to default deny
Step 1 - Ensure Agent is running on Knocknoc Server
sudo systemctl status knocknoc-agent
Confirm the 'knoc_ssh' IPSet was created by the Agent:
sudo ipset list knoc_ssh
You should see an empty set. If it doesn't exist, create it:
sudo ipset create knoc_ssh hash:ip
This is the IPSet that Knocknoc will add and remove user IPs from as they log in and out.
Step 2 - Create a Knoc
We'll use the knoc_ssh IPSet already created by the Agent
Step 3 - Edit UFW Rules
sudo nano /etc/ufw/after.rules
Add this line before the final 'COMMIT':
-A ufw-after-input -m set --match-set knoc_ssh src -p tcp --dport 22 -j ACCEPT
Reload UFW:
sudo ufw reload
Testing
- Log in to Knocknoc as your test user (not '
/admin') - Activate the SSH Knoc
- On the server, check the IPSet:
user@knocknoc:~$ sudo ipset list knoc_ssh
Name: knoc_ssh
Type: hash:ip
Revision: 6
Header: family inet hashsize 1024 maxelem 65536 bucketsize 12 initval 0xdd15faed
Size in memory: 256
References: 0
Number of entries: 1
Members:
192.168.10.110
Your user's IP address should now appear in the set. SSH from that IP allowed through UFW.
Log out of Knocknoc and check that the IP is removed.
user@knocknoc:~$ sudo ipset list knoc_ssh
Name: knoc_ssh
Type: hash:ip
Revision: 6
Header: family inet hashsize 1024 maxelem 65536 bucketsize 12 initval 0xdd15faed
Size in memory: 216
References: 0
Number of entries: 0
Members:
Shift to default deny
Now that you've confirmed it works, we'll block all incoming SSH except for authenticated Knocknoc users:
Note: The Knocknoc server listens on 127.0.0.1:8756 by default, which is only accessible locally. For other users to reach the login page, change the bind address to 0.0.0.0:8756 in /opt/knocknoc/etc/knocknoc.conf and restart with sudo systemctl restart knocknoc. Then allow port 8756 through UFW:
sudo ufw default deny incoming
sudo ufw allow 8756/tcp
sudo ufw enable
Only IPs in the 'knoc_ssh' IPSet can now reach port 22. Everyone else is denied.
Break-glass reminder: Before enabling default deny, ensure you have an alternative way to access the server (console access, or a fixed management IP allow-listed in UFW) in case something goes wrong.
Example of allow-listing a fixed management IP:
sudo ufw allow from 203.0.113.10 to any port 22
Bonus: IPv6 support
If your network uses IPv6, create a separate IPv6 IPSet:
sudo ipset create knoc_ssh_v6 hash:ip family inet6
In the Knoc settings enter 'knoc_ssh_v6' in the IPv6 Set Name field.
Then add a matching rule in '/etc/ufw/after6.rules' before the final 'COMMIT':
-A ufw6-after-input -m set --match-set knoc_ssh_v6 src -p tcp --dport 22 -j ACCEPT
Reload UFW:
sudo ufw reload






