Thank's to Xentime @ Unbuntu forums for this.
I am setting up (trying ) this.
When I test or try and modify anything on Linux that I am not familiar with I always use a virtual machine first, Then if I am happy with the result I do it on my actual machine.
This HOWTO is starting point for those who want a restrictive firewall for incoming, forwarded, and outgoing traffic.
Here is an iptables configuration script (shell script) that I use as a starting point for all my machines. It drops all incoming and forwarded connections, while allowing outgoing connections on the FTP (21), SSH (22), HTTP (80), HTTPS (443), and DNS (53) ports. This allows you to perform software updates, browse the web, and establish remote connections to SSH servers running on their default port, while restricting all other traffic.
1. Open the Text Editor application (gedit) and paste the following into a new document:2. Save the file into your Home folder and name it "iptables.sh" (without the quotes).Code:#!/bin/sh
# Flush iptables
# Set iptables rules
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -p tcp --dport 21 -j ACCEPT # FTP port
iptables -A OUTPUT -p tcp --dport 80 -j ACCEPT # HTTP port
iptables -A OUTPUT -p tcp --dport 443 -j ACCEPT # HTTPS port
iptables -A OUTPUT -p tcp --dport 1024 -j ACCEPT # SOCKS port
iptables -A OUTPUT -p udp --dport 53 -j ACCEPT # DNS port
# Set iptables default policy
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT DROP
# Save rule-set to file
sh -c "iptables-save > /etc/iptables.rules"
sh -c "iptables-save > /etc/iptables.downrules"
# Add the following to "/etc/network/interfaces":
# pre-up iptables-restore < /etc/iptables.rules
# post-down iptables-restore < /etc/iptables.downrules
3. Open up the Terminal application. (Ctrl+Alt+T)
4. Set the file permissions for "iptables.sh" to execute:5. Run the shell script as root:Code:chmod +x iptables.sh6. Open up the network interfaces configuration file for editing as root:Code:sudo ./iptables.sh7. Add the following to the configuration file so the rules load on start-up:Code:sudo nano /etc/network/interfaces8. Save the file and exit the text editor. (Ctrl+O <Enter> then Ctrl+X)Code:pre-up iptables-restore < /etc/iptables.rules
post-down iptables-restore < /etc/iptables.downrules
9. Restart/Reboot your computer.
10. Open up the Terminal application. (Ctrl+Alt+T)
11. Check iptables to ensure the rules have been applied:Steps 6 and 7 are noted in shorthand within the shell script to aid with configuration when moving from one machine to another.Code:sudo iptables -vL
Allow/Drop Outgoing Connections
There are times you may want to use an application that will create outgoing connections not allowed by the rules in place for a limited time but do not want to poke holes in your firewall indefinitely (i.e. computer games). This is best achieved by simply allowing all outgoing connections and then restricting them when you are done. This is achieved by doing the following.
1. Open up the Terminal application. (Ctrl+Alt+T)
2-A. Allow all outgoing connections:orCode:sudo iptables -P OUTPUT ACCEPT
2-B. Drop all outgoing connections (minus those that have ACCEPT rules):3. Check iptables to ensure the rules have been applied:Code:sudo iptables -P OUTPUT DROPCode:sudo iptables -vL
Edited by NickAu1, 01 April 2014 - 08:29 PM.