Setting up nftables on an Arch Linux system, especially for a Sway desktop user, involves several steps to ensure that your firewall is configured correctly and tailored to your needs. Below, I’ll guide you through the installation and basic configuration of nftables.
Step 1: Install nftables
First, ensure that nftables is installed. Open a terminal and run the following command:
sudo pacman -S nftables
Step 2: Enable and Start the nftables Service
To ensure that nftables is loaded on boot, enable and start its service:
sudo systemctl enable --now nftables.service
Step 3: Create a Basic Configuration
You’ll want to create a basic configuration file for nftables. The configuration is usually kept in /etc/nftables.conf. You can edit this file using your preferred text editor. For example, using nano:
sudo nano /etc/nftables.conf
Here’s a basic example configuration you can use to start with:
#!/usr/sbin/nft -f
# Flushing existing rules
flush ruleset
# Define the main table for the IPv4 firewall
table ip filter {
# Define the chains
chain input {
type filter hook input priority 0; policy drop;
# Allow established and related connections
ct state established,related accept
# Allow localhost
iif lo accept
# Allow ICMP (ping)
ip protocol icmp accept
# Allow SSH (optional, change 22 to your SSH port if different)
tcp dport 22 accept
# Allow Sway IPC
unix dgram socket ("/run/user/1000/sway-ipc.1000.0.sock") accept
# Allow necessary network services below (HTTP, HTTPS, etc.)
tcp dport {80, 443} accept
# Reject all other traffic
reject with icmp type port-unreachable
}
chain forward {
type filter hook forward priority 0; policy drop;
}
chain output {
type filter hook output priority 0; policy accept;
}
}
Step 4: Start nftables with the Configuration
After saving your configuration, you can start nftables with your new ruleset:
sudo nft -f /etc/nftables.conf
Step 5: Verify Your Configuration
You can check the active ruleset by running:
sudo nft list ruleset
This will display the current configuration and allowed/blocked connections.
Step 6: Persisting Changes
By saving your rules in /etc/nftables.conf and having the nftables service enabled, your settings should persist across reboots.
Step 7: Testing Your Firewall
-
Connectivity: Test if your connections work (e.g., browse the web, SSH into your machine if you allowed it).
-
Allowing New Services: If you need to allow additional services, simply add new rules to your /etc/nftables.conf and reload the configuration with:
sudo nft -f /etc/nftables.conf
Additional Considerations
-
Update Your Rules: As you install new software or services, remember to update your firewall rules accordingly.
-
Logs: Consider adding logging rules to help troubleshoot any blocked connections:
log prefix "nftables: "
-
Security: Always review your firewall rules to ensure they are as strict as necessary while allowing required services.
Conclusion
This setup provides a basic framework for using nftables on your Arch Linux Sway desktop. You can customize and expand upon this configuration based on your specific needs and the services you're using. For more advanced configurations, refer to the official nftables documentation or the Arch Wiki for more specific scenarios.