Configure persistent iptables rules on boot in manjaro
- User
Configure persistent iptables rules on boot in manjaro
- Assistant
Configure persistent iptables rules on boot in manjaro
To configure persistent iptables rules that load automatically on boot in Manjaro, you can use the built-in iptables service provided by the iptables package. This service utilizes systemd to save and restore your firewall rules across reboots.
Here are the detailed steps to achieve this:
Set Up Your iptables Rules:
First, configure your desired iptables rules manually using the iptables command. For example:
sudo iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT # Allow SSH
sudo iptables -A INPUT -p icmp -j ACCEPT # Allow Ping
sudo iptables -A INPUT -j DROP # Drop all other inbound traffic
Replace these rules with the ones that suit your specific requirements.
Save the iptables Rules:
After setting up your rules, save them to a file that the iptables service can use. By default, the service looks for rules in /etc/iptables/iptables.rules. Create the directory and save the rules:
sudo mkdir -p /etc/iptables
sudo iptables-save | sudo tee /etc/iptables/iptables.rules
This command saves the current iptables rules to /etc/iptables/iptables.rules. The iptables-save utility outputs the current rules in a format that iptables-restore can read.
Enable and Start the iptables Service:
Enable the iptables service to ensure it starts on boot:
sudo systemctl enable iptables
Start the service immediately to apply the saved rules:
sudo systemctl start iptables
The iptables service will now automatically restore the saved rules from /etc/iptables/iptables.rules on every boot.
Verify the Service Status:
Check the status of the iptables service to ensure it is active and running:
sudo systemctl status iptables
You should see an output indicating that the service is "active (exited)", which means it has successfully loaded the rules.
(Optional) Configure IPv6 ip6tables Rules:
If you're using IPv6 and need to configure ip6tables rules, repeat the same steps for ip6tables:
a. Set up your ip6tables rules:
sudo ip6tables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
sudo ip6tables -A INPUT -p tcp --dport 22 -j ACCEPT # Allow SSH over IPv6
sudo ip6tables -A INPUT -p ipv6-icmp -j ACCEPT # Allow ICMPv6
sudo ip6tables -A INPUT -j DROP # Drop all other inbound IPv6 traffic
b. Save the ip6tables rules:
sudo ip6tables-save | sudo tee /etc/iptables/ip6tables.rules
c. Enable and start the ip6tables service:
sudo systemctl enable ip6tables
sudo systemctl start ip6tables
d. Verify the ip6tables service status:
sudo systemctl status ip6tables
Persisting Changes After Modifying Rules:
Whenever you make changes to your iptables rules, remember to save them again:
sudo iptables-save | sudo tee /etc/iptables/iptables.rules
sudo ip6tables-save | sudo tee /etc/iptables/ip6tables.rules # For IPv6 rules
Then restart the services to apply the new rules immediately:
sudo systemctl restart iptables
sudo systemctl restart ip6tables # For IPv6 rules
Notes:
Security Caution: Be careful when configuring firewall rules, especially the DROP policies, to avoid locking yourself out of your system, particularly if you're accessing it remotely via SSH.
Alternative Method: If you prefer, you can use a firewall management utility like ufw (Uncomplicated Firewall) or firewalld, which can simplify firewall configuration and also handle persistence automatically.
Summary:
By following these steps, you've configured your iptables rules to persist across reboots in Manjaro. The iptables and ip6tables systemd services handle loading the saved rules on boot, ensuring your firewall configuration remains consistent.