How do I allow certain IPs and block all other connection in iptables on AMAZON EC2?


You can accomplish this using UFW.

Enable UFW and set default incoming to deny:

$ sudo ufw enable && sudo ufw default deny incoming 

Then you can allow specific IP's with:

$ sudo ufw allow from to any 

This will allow IP-address to any port on your firewall.

  • ubuntu@master:~$ sudo ufw enable && sudo ufw default deny incoming Command may disrupt existing ssh connections. Proceed with operation (y|n)? y Firewall is active and enabled on system startup Default incoming policy changed to 'deny' (be sure to update your rules accordingly) ubuntu@master:~$ sudo ufw allow from 10.9.XX.xxto any Rule added After this if i tried to take new session with putty (from the specific ip which I allowed Ip addresses) I couldn't make it.Any idea ? – prem May 25 '15 at 13:25
  • The IP (10.9.XX.XX) you allowed, is that your public IP address? – krt May 26 '15 at 7:39
  • I got it now.Will check it with public ip this may work .I have one issue like I denied all incoming connection with private ip so that I couldn't able to take ssh for that instance.Any idea? – prem May 26 '15 at 7:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.