0%

How to set up Ubuntu14.04 firewall

Ubuntu does not start any firewall in default, so we had better set up some rules for the network in case of any safety issues. The following will make a short introductions about how to set up the iptable for ubuntu.

1. Check whether the iptables is installed

1
2
root@ubuntu14:~# whereis iptables
iptables: /sbin/iptables /etc/iptables.rules /usr/share/iptables /usr/share/man/man8/iptables.8.gz

2. Check the iptables rule

1
2
3
4
5
6
root@ubuntu14:~# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
ACCEPT icmp -- anywhere anywhere
ACCEPT all -- anywhere anywhere

3. Edit the iptables rule

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
root@ubuntu14:~# nano /etc/iptables.rules 
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 5000 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

4. Make the new iptables rules work

1
iptables-restore < /etc/iptables.rules

5. Enable the iptables be launched once OS starts

1
2
3
vi /etc/network/if-pre-up.d/iptables
#!/bin/bash
iptables-restore < /etc/iptables.rules

6. Enable the script be executable

1
chmod +x /etc/network/if-pre-up.d/iptables

7. Check the iptables rule

1
iptables -L -n