Open/Close Ports on Ubuntu distributions

IPTables is an application program that allows to set up, maintain, and inspect the tables of IP packet filter rules provided by the Linux kernel.

All IPTables rules are stored on /etc/iptables.rules
You can open/close any port from it and then restart the network by running “etc/init.d/networking restart”

Before opening or closing ports, we recommend to first verify if the port is open or closed and who is using the port. There a number of different commands:

Commands to verify ports:

nmap IP#
nmap localhost
netstat –ntulp

to verify single port

netstat -nap | grep  <port #>

to list all current rules in iptables

iptables -L

For opening a TCP port:

iptables -A INPUT  -p tcp –dport <port #> -j ACCEPT

For opening a UDP port:

iptables -A INPUT -p udp –sport <port #>  -j ACCEPT

Save changes:

iptables-save > /etc/iptables.rules

If you need to disable the firewall temporarily, you can flush all the rules using:

iptables -F