20/01/2016
How to open a port to one ore more specific IP in Iptables.
Sometimes you need to open a port on your server, you want it to be recheable only from specific IP address, you can use Iptables for this:
iptables -I INPUT -p tcp -s 10.1.1.2 --dport 22 -j ACCEPT
In that case, you are opening ssh port only to IP 10.1.1.2, if you need to open DNS for your internal network.
iptables -I INPUT -p udp -s 10.1.0.0/16 --dport 53 -j ACCEPT
Once you have them added and opened for those IPs, you need to close the door for the rest of IPs
iptables -I INPUT -p tcp -s 0.0.0.0/0 --dport 22 -j DROP
iptables -I INPUT -p udp -s 0.0.0.0/0 --dport 53 -j DROP
and another example :
#################################################
# clear existing chains
#################################################
iptables --flush
iptables --delete-chain
#################################################
# allow loopback
#################################################
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
#################################################
# allow established connections
#################################################
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
#################################################
# allow ICMP from 10.0.0.1
#################################################
iptables -A INPUT -s 10.0.0.1 -p icmp --icmp-type any -j ACCEPT
iptables -A OUTPUT -p icmp -j ACCEPT
#################################################
# allow port range from 10.0.0.1
#################################################
iptables -A INPUT -m state --state NEW -s 10.0.0.1 -m tcp -p tcp --match multiport --dports 0:5555 -j ACCEPT
#################################################
# deny all
#################################################
iptables -A INPUT -j DROP
#################################################
# default policies
#################################################
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
#################################################
# save the new policy
#################################################
service iptables save