Add new rule to iptables firewall

Sometimes the server you manage can have iptables based firewall configured to protect access for ports. The firwall configuration done in file is only part of the changes. You need then to test the configuration and tell iptables to reload new config.

Here is the process:

  1. Login to shell of the box you need to change firewall settings.
  2. Edit iptables.rules file, for example using Midnight Commander “mcedit”
    mcedit /etc/network/iptables.rules
  3. Add your new rule. Here is the example to unblock access for port 22 for IP xxx.xxx.xxx.xxx
    -A INPUT -s xxx.xxx.xxx.xxx/32 -p tcp -m tcp --dport 22 -j ACCEPT
  4. To test new setting execute first following command.
    iptables-restore --test /etc/network/iptables.rules
  5. If the test is ok you can reload iptables with this command
    iptables-restore /etc/network/iptables.rules
  6. Very important step!
    Log to ssh from different console to check if you did not block ssh access to the box with your new rule!
  7. Log out from ssh.