How to use Ubuntu iptables for advanced firewall rules?

by keegan.towne , in category: Security , 2 years ago

How to use Ubuntu iptables for advanced firewall rules?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by gabrielle.kub , 2 years ago

@keegan.towne 

To use Ubuntu iptables for advanced firewall rules, follow these steps:

  1. Open a terminal window on your Ubuntu machine.
  2. Check if iptables is installed on your system by running the command: sudo iptables --version If it is not installed, install it by running the command: sudo apt-get install iptables
  3. Create a new iptables chain for your firewall rules by running the command: sudo iptables -N myfirewall
  4. Add new rules to the myfirewall chain by using the -A (append) option. For example, to allow incoming SSH connections, you can run the command: sudo iptables -A myfirewall -p tcp --dport 22 -j ACCEPT This rule allows TCP traffic on port 22 (SSH) to be accepted by the firewall.
  5. Add more rules to the myfirewall chain as needed for your specific network security requirements.
  6. Once you have added all your rules to the myfirewall chain, add a default policy for the chain. This policy determines what happens to packets that do not match any of the rules in the chain. For example, to drop all packets that do not match any of the rules, you can run the command: sudo iptables -P myfirewall DROP
  7. Finally, activate the myfirewall chain by running the command: sudo iptables -I INPUT -j myfirewall This command inserts the myfirewall chain as the first rule in the INPUT chain. The INPUT chain is the default chain that handles incoming traffic to the system.


Your advanced firewall rules are now active and protecting your Ubuntu machine. You can use the iptables command to view the current rules and make modifications as necessary.