fd Blog

Daniel Hilgarth on software development

Simple Firewall Setup on Ubuntu

Install iptables-persistent:

sudo apt-get install iptables-persistent

Create the firewall rules:

sudo nano /etc/iptables/rules.v4

Use these rules as a starting point: *filter :INPUT DROP :FORWARD DROP :OUTPUT ACCEPT

# Allows all loopback (lo0) traffic and drop all traffic to 127/8 that doesn't use lo0
-A INPUT -i lo -j ACCEPT
-A INPUT ! -i lo -d 127.0.0.0/8 -j REJECT

# Allow established connections
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# Allows all outbound traffic
-A OUTPUT -j ACCEPT

# Allows HTTP and HTTPS connections from anywhere (the normal ports for websites)
-A INPUT -p tcp --dport 80 -j ACCEPT
-A INPUT -p tcp --dport 443 -j ACCEPT

# Allow FTP connections from anywhere
-A INPUT -p tcp --dport 21 -j ACCEPT
-A INPUT -p tcp --destination-port 49152:65534 -j ACCEPT

# Allows SSH connections
## SSH
-A INPUT -p tcp --dport 22 -j ACCEPT

# Allow ping
-A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT

# log iptables denied calls (access via 'dmesg' command)
-A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7

COMMIT

Comments