[Security]Setup a firewall using iptables on Ubuntu 12.04.3 - lifuzu/cafe GitHub Wiki

Flush all rules

sudo iptables -F
sudo iptables -X

List iptables

sudo iptables -v -L -n
sudo iptables --list

List INPUT table with line number

sudo iptables -L INPUT --line-numbers

Add/append a rule in the INPUT table

sudo iptables -A INPUT -i eth0 -p tcp --dport 443 -j ACCEPT  # append the rule to INPUT table
sudo iptables -A INPUT -i eth0 -p tcp --dport 22 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT

Insert a rule in the INPUT table

sudo iptables -I INPUT 1 -p tcp --dport 80 -j ACCEPT  # insert the rule to line 1

Replace a rule in the INPUT table

sudo iptables -R INPUT 1 -p tcp -s 192.168.0.0/24 --dport 80 -j ACCEPT  # replace the rule in the line 1

Delete a rule in the INPUT table with line number

sudo iptables -D INPUT 9  # delete the rule in line 9 of INPUT table

or

sudo iptables -D INPUT -s 127.0.0.1 -p tcp --dport 111 -j ACCEPT  # delete the rule as the rule content

###How to persistent the IP tables:

sudo apt-get install iptables-persistent
sudo invoke-rc.d iptables-persistent save

REFERENCE:

  1. https://help.ubuntu.com/lts/serverguide/firewall.html
  2. https://www.digitalocean.com/community/articles/how-to-set-up-a-firewall-using-ip-tables-on-ubuntu-12-04
  3. https://fedoraproject.org/wiki/How_to_edit_iptables_rules
  4. http://www.thegeekstuff.com/2011/02/iptables-add-rule/