Ipsets With Ufw
Ipsets With Ufw
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:
1. A systemd service script that creates and loads the ipset at startup.
2. A UFW application profile or direct rules that utilize the IPset for allowing specific
traffic.
/usr/local/bin/create_ipset.sh:
#!/bin/bash
IPSET_NAME="knocknoc"
/etc/systemd/system/ipset.service:
[Unit]
Before=ufw.service
After=network.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/create_ipset.sh
[Install]
WantedBy=multi-user.target
This script sets up the service to run before UFW (as indicated by Before=ufw.service ) but after the
network is available ( After=network.target ).
COMMIT
This rule will allow SSH connections on port 22 for IP addresses that are in the knocknoc IPset.
With this setup, your system will create and populate the ipset at startup, and UFW will utilize
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.
Revision #6
Created 28 December 2023 09:52:32 by Dave
Updated 5 March 2024 16:29:40 by Dylan