Show nftables config for blocking all incoming connections on personal linux desktop…
- User
Show nftables config for blocking all incoming connections on personal linux desktop machine
- Assistant
Show nftables config for blocking all incoming connections on personal linux desktop machine
Blocking all incoming connections on your Linux desktop using nftables involves setting up firewall rules that drop unsolicited inbound traffic while allowing necessary outbound traffic and local system communications. Below is a sample nftables configuration that accomplishes this:
nftables ConfigurationCreate a file named nftables.conf (commonly located at /etc/nftables.conf) with the following content:
#!/usr/sbin/nft -f
# Define a table in the 'inet' family, which works for both IPv4 and IPv6
table inet filter {
# Define a chain for handling all inbound traffic
chain input {
type filter hook input priority 0;
# 1. Allow all loopback (lo) traffic and drop traffic using loopback addresses as source or destination
iif "lo" accept
ip6iif "lo" accept
ct state related,established accept
# Drop invalid packets
ct invalid drop
# 2. Allow incoming ICMP (ping) requests
ip protocol icmp accept
ip6 nexthdr icmpv6 accept
# 3. Drop all other incoming traffic
drop
}
# Define a chain for handling forwarded traffic (if your desktop isn't routing)
chain forward {
type filter hook forward priority 0;
# Drop all forwarded packets
drop
}
# Define a chain for handling outbound traffic
chain output {
type filter hook output priority 0;
# Allow all outbound traffic
accept
}
}
Table Definition
inet: This family handles both IPv4 and IPv6, ensuring comprehensive coverage.filter: A conventional name for filtering rules.Chains
input Chain
lo), which is essential for local system operations.ping. You can remove or modify these rules if you want to block pings.forward Chain
output Chain
Install nftables (if not already installed):
sudo apt update
sudo apt install nftables
Replace Existing Configuration:
Backup the existing nftables configuration and replace it with the new one:
sudo cp /etc/nftables.conf /etc/nftables.conf.backup
sudo nano /etc/nftables.conf
Paste the provided configuration into the file and save.
Apply the Configuration:
sudo nft -f /etc/nftables.conf
Enable nftables to Start on Boot:
sudo systemctl enable nftables
sudo systemctl start nftables
After applying the configuration, you can verify the active nftables rules with:
sudo nft list ruleset
This command displays the current ruleset, allowing you to confirm that your policies are correctly in place.
Essential Services: If your desktop runs services that require inbound connections (e.g., SSH server, web server), you'll need to add specific rules to allow traffic on those ports. For example, to allow SSH:
tcp dport 22 ct state new,established accept
ct state established accept
Logging: For debugging or monitoring purposes, you might want to add logging rules before the drop actions to record dropped packets. Be cautious, as excessive logging can fill up log files quickly.
Testing: Always test firewall rules to ensure you don't lock yourself out, especially if accessing the machine remotely. It's advisable to have physical access or an alternative access method in case of misconfiguration.
Backup Configuration: Keep backups of your working nftables configurations to quickly restore them if needed.
By following this configuration, your personal Linux desktop will block all unsolicited incoming connections, enhancing its security posture. Remember to adjust and extend the rules based on your specific requirements and the services you intend to run.
Not yet included in a published release.