โš™๏ธHow to Auto-Load nftables Masquerade Rules at Boot with systemd Service๐Ÿ‘จโ€๐Ÿญ

Ronald BartelsRonald Bartels
1 min read

To automatically load your nftables masquerade rules at boot, you need to create a systemd service that will apply the /etc/nftables/nft-masquerade.nft file when the system starts.


๐Ÿ› ๏ธ Step 1: Create the Systemd Service

Open a terminal and create a new service file:

sudo nano /etc/systemd/system/nft-masquerade.service

Add the following content:

[Unit]
Description=Load nftables masquerade rules
After=network.target
Wants=network.target

[Service]
Type=oneshot
ExecStart=/usr/sbin/nft -f /etc/nftables/nft-masquerade.nft
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

๐Ÿ”„ Step 2: Reload Systemd & Enable the Service

Save the file (CTRL+X, then Y, then ENTER), then reload systemd to recognize the new service:

sudo systemctl daemon-reload

Enable the service so it starts automatically at boot:

sudo systemctl enable nft-masquerade.service

๐Ÿš€ Step 3: Start & Verify

Manually start the service:

sudo systemctl start nft-masquerade.service

Check its status:

sudo systemctl status nft-masquerade.service

If everything is set up correctly, you should see "Active: exited successfully". ๐ŸŽ‰


๐Ÿ› ๏ธ Step 4: Testing

To verify that the rules are applied, run:

sudo nft list ruleset

This should display your nftables rules, including the masquerading setup.


โœ… Done! Your nftables masquerade rules will now automatically load on boot using systemd. ๐Ÿš€

10
Subscribe to my newsletter

Read articles from Ronald Bartels directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ronald Bartels
Ronald Bartels

Driving SD-WAN Adoption in South Africa