Firewall Ubuntu Servers
This post is in follow up to my post on firewalls on Ubuntu Desktops.
IMO firewalls are often helpful for restricting access to servers. Servers come in two varieties, public and private, and often people wish to limit access or black list IP addresses that misbehave.
Again I will use UFW which is installed by default. In the last section I will introduce iptables. One feature that is nice about ufw, if you understand the ufw rules it is an easy transition to iptables.
The most important thing you need to know to firewall servers is;
- Who (ip address) you wish to allow or restrict access.
- What protocol (tcp / udp) and port is used by your server.
A listing of ports is available here.
Enable your firewall
If you are accessing your server remotely be sure NOT to lock yourself out
Assuming you are accessing via ssh, allow ssh (we will restrict ssh access below, for now just do not lock yourself out).
sudo ufw allow 22
Now enable your firewall. Except for ssh, which you allowed with the above rule, this will deny all incoming (udp/tcp) traffic to your server.
sudo ufw enable
sudo default deny
Public servers
Examples of public servers would be Apache (http) or FTP servers (to name a few). Here we wish to allow access to just about everyone.
Simply allow by port
sudo ufw allow 80
Or if you wish, by protocol and port (most servers will be tcp).
sudo ufw allow 80/tcp
You may specify multiple ports (comma separated list):
sudo ufw allow 80,443/tcp
Or a range of ports, low:high:
#Allow ports 6881 – 6999 (torrent)
sudo ufw allow 6881:6999/tcp
You may specify most services by name.
By Name :
sudo ufw allow ssh
Some servers can be specified “by application”, although this is still by port and is not application specific. By that I mean : if you allow “Apache”, you open port 80, which can be used by any client application (firefox, wget, etc).
List applications with -
sudo ufw app list
ufw app list
Available applications:
Apache
Apache Full
Apache Secure
CUPS
OpenSSH
To translate the cryptic output to English,
Apache = http = port 80
Apache Secure = https = port 443
Apache Full = both ports
As you install servers, they will be added to the list.
Now allow by application.
Examples (you do not need to use all 3 rules):
sudo ufw allow Apache
#Note: Quotes are needed with “Apache Full”
sudo ufw allow “Apache Full”
sudo ufw allow from 192.168.0.0/24 app OpenSSH
You may add custom applications or custom ports to /etc/ufw/application.d
As an example, /etc/ufw/applications.d/apache2.2-common contains
[Apache]
title=Web Server
description=Apache v2 is the next generation of the omnipresent Apache web server.
ports=80/tcp
[Apache Secure]
title=Web Server (HTTPS)
description=Apache v2 is the next generation of the omnipresent Apache web server.
ports=443/tcp
[Apache Full]
title=Web Server (HTTP,HTTPS)
description=Apache v2 is the next generation of the omnipresent Apache web server.
ports=80,443/tcp
So if you changed the ssh port to 8822, add a file “ssh-custom”, at /etc/ufw/applications.d/ssh-custom
[SSH Custom]
title= SSH Custom port
description=OpenSSH Server Custom port
ports=8822/tcp
You will now see “SSH Custom” when you list apps and can use it as above.
Private servers
Examples may included NFS, Samba, ssh, VNC, and VPN. I will use ssh and Apache as an examples.
For these examples we will assume your LAN is 192.168.0.0/24 and your server is 192.168.0.10
Here we almost always wish to restrict access to a single ip or perhaps range of IP. For example to restrict access for ssh to a single machine, say 192.168.0.20
sudo ufw allow proto tcp from 192.168.0.20 to 192.168.0.10 port 22
The syntax is protocol from <ip> to <server ip> port
To allow ssh from any client on your your lan use:
sudo ufw allow proto tcp from 192.168.0.0/24 to 192.168.0.10 port 22
Limiting access
Limiting access comes in two flavors, the first is to limit a DOS or brute force attempt, and the other blacklisting.
Brute Force
UFW will rate limit connection attempts:
ufw supports connection rate limiting, which is useful for protecting against brute-force login attacks. ufw will deny connections if an IP address has attempted to initiate 6 or more connections in the last 30 seconds.
Example (using ssh):
sudo ufw limit ssh
“Limit” opens the port, so you do not need a second rule.
ufw status
Status: active
To Action From
-- ------ ----
22 LIMIT Anywhere
This output demonstrates – Port 22 is open and access is limited by ufw.
Blacklist
Keep in mind the order of your rules is critical. As such I like to block first, accept second. So for example let us assume we wish to block a misbehaving client on our LAN, 192.168.0.20:
sudo ufw insert 1 deny from 192.168.0.20
Here “insert 1″ is specifying to ufw to insert the rule first (or near the top) of the chain.
Using UFW in this way blocks only NEW connections.
IMO better to use iptables or an application such as iplist.
Block ping
By default, UFW allows ping requests. In order to block these requests you will need to edit /etc/ufw/before.rules
sudo -e /etc/ufw/before.rules
Change
# ok icmp codes
-A ufw-before-input -p icmp --icmp-type destination-unreachable -j ACCEPT
-A ufw-before-input -p icmp --icmp-type source-quench -j ACCEPT
-A ufw-before-input -p icmp --icmp-type time-exceeded -j ACCEPT
-A ufw-before-input -p icmp --icmp-type parameter-problem -j ACCEPT
-A ufw-before-input -p icmp --icmp-type echo-request -j ACCEPT
to
# ok icmp codes
-A ufw-before-input -p icmp –icmp-type destination-unreachable -j ACCEPT
-A ufw-before-input -p icmp –icmp-type source-quench -j ACCEPT
-A ufw-before-input -p icmp –icmp-type time-exceeded -j ACCEPT
-A ufw-before-input -p icmp –icmp-type parameter-problem -j ACCEPT
-A ufw-before-input -p icmp –icmp-type echo-request -j DROP
Restart UFW
sudo ufw disable
sudo ufw enable
Deleting rules
Deleting a rule is also easy. Use the same syntax you used to add a rule to ufw with the word “delete” added.
For example, using Apache as an example:
# sudo ufw allow Apache
Rule added
# ufw status
Status: active
To Action From
– —— —-
22 LIMIT Anywhere
Apache ALLOW Anywhere
# sudo ufw delete allow Apache
Rule deleted
# ufw status
Status: active
To Action From
– —— —-
22 LIMIT Anywhere
Iptables
Now that you have ufw under your belt, it is easier to understand iptables. If you are wanting to use iptables, best disable UFW first.
sudo ufw disable
#These iptables rules clean up after UFW, deleting the custom tables
sudo iptables -F
sudo iptables -X
To deny all incoming traffic (take care not to lock yourself out form remote servers, allow ssh first !!!):
sudo iptables -A INPUT -j DROP
You can set a Policy with iptables, but doing so makes it easy to lock yourself out if you issue the command “iptables -F”.
To allow ssh
sudo iptables -A INPUT -p tcp –dport 22 -j ACCEPT
To allow ssh only from your LAN:
sudo iptables -A INPUT -s 192.168.0.0/24 -p tcp –dport 22 -j ACCEPT
iptables is much more feature rich the UFW and hopefully this blog will both stimulate your interest and facilitate your learning of the use of iptables.
See also : Bodhi Zazen’s Iptables Primer
Posted in Linux
[...] This post was mentioned on Twitter by Planet Ubuntu, みはbot. みはbot said: Shadows of epiphany » Blog Archive » Firewall Ubuntu Servers: This post is in follow up to my post on firewalls.. http://bit.ly/07DVbhK [...]
Pingback by Tweets that mention Shadows of epiphany » Blog Archive » Firewall Ubuntu Servers -- Topsy.com — November 23, 2009 @ 6:09 pm
Dropping ICMP control messages is a great way to cause terrible performance. Ping is one thing, messing up congestion control and so on another.
Comment by Robert Collins — November 23, 2009 @ 7:38 pm
OK, thanks for the feedback. I personally prefer to keep ping so I overlooked this detail, I will update my post.
— Should be better now =)
Comment by bodhi.zazen — November 23, 2009 @ 7:46 pm
Social comments and analytics for this post…
This post was mentioned on Twitter by planetubuntu: Bodhi.Zazen: Firewall Ubuntu Servers: This post is in follow up to my post on firewalls on Ubuntu Desktops.
IMO fir… http://bit.ly/6LjTGM...
Trackback by uberVU - social comments — November 23, 2009 @ 10:46 pm
Very nice article thanks bodhi!
You can also look in /etc/services for the ports/protocol info. Very handy if you only have ssh access and well can’t go browsing wiki pages.
Comment by drubin — November 24, 2009 @ 2:01 am
This is indeed very handy drubin.
You may search for a service with grep :
grep ssh /etc/services
ssh 22/tcp # SSH Remote Login Protocol
ssh 22/udp
Comment by bodhi.zazen — November 24, 2009 @ 10:06 am
[...] Firewall Ubuntu Desktops Firewall Ubuntu Servers [...]
Pingback by Bodhi.Zazen: Firewall Ubuntu GUFW | L&C Tech Talk — November 24, 2009 @ 8:27 pm
Lots of of folks write about this topic but you said really true words.
Comment by Jemicomiutt — November 25, 2009 @ 12:56 pm
Hmmm… my main question is if you enter a semi-complicated (for the sort of person ufw is designed for) rule, say one specifying only allowing a certain machine access to a certain port, over tcp, that sort of thing… how do you specify that rule later if you want to delete it? ufw only lists the rules, not the syntax that created them. On the one hand it should give an experienced user enough information that they could recreate the rule and successfully delete it, but for a beginner who didn’t copy it down in a text file somewhere… they might left with no option but to wipe all the rules to get the one they really wanted gone.
Or am I missing something here?
Comment by memilanuk — November 26, 2009 @ 12:37 am
LOL, this is true and I have had that problem.
As with all things, as you use ufw and become more familiar with it you will become more familiar with the syntax.
As you suggest, the syntax of ufw (when listing the rules) is cryptic, but is often sufficient to work through, but it can be difficult.
If you are stuck you could consider either deleting the rule with gufw or post on the Ubuntu Forums, in the security section (ask for help).
Comment by bodhi.zazen — November 26, 2009 @ 8:43 am
I found your blog recently and have been visiting it . I think your way of thinking is good. keep up the good work. If interested in link exchange please contact me.
Comment by bad credit loans — December 4, 2009 @ 7:06 pm
This article was *very* helpful to me at a crucial time. Thank you for your efforts, especially the examples and attention to detail, all written with CLARITY (a difficult commodity to find in Linux work).
Well done!
Comment by ctlarsen — January 6, 2010 @ 9:22 pm