Linux – Iptables: Blocking outbound traffic except to certain IP addresses

firewalliptableslinuxSecurity

Using iptables, I need to block all outbound traffic on my server, except:

  • SSH access to a small number of IP addresses
  • HTTPS access to the same small list of IP addresses

Can anybody show me a suitable set of rules?

Thank you.

Best Answer

iptables -I OUTPUT -d <remote_ip> -p tcp --dport 22 -j ACCEPT
iptables -I INPUT -s <remote_ip> -p tcp --sport 22 -j ACCEPT
iptables -I OUTPUT -d <remote_ip> -p tcp --dport 443 -j ACCEPT
iptables -I INPUT -s <remote_ip> -p tcp --sport 443 -j ACCEPT
iptables -P INPUT DROP
iptables -P OUTPUT DROP

You will need to put in the first 4 rules for each of the IPs. Be warned, though, because you will have to log in via the console on this machine; all other access to it will be blocked.