One of the first lines of defense in securing your cloud server is a functioning firewall. In the past, this was often done through complicated and arcane utilities. There is a lot of functionality built into these utilities, iptables being the most popular nowadays, but they require a decent effort on behalf of the user to learn and understand them. Firewall rules are not something you want yourself second-guessing.
To this end, UFW is a considerably easier-to-use alternative.
What is UFW?
UFW, or Uncomplicated Firewall, is a front-end to iptables. Its main goal is to make managing your firewall drop-dead simple and to provide an easy-to-use interface. It’s well-supported and popular in the Linux community—even installed by default in a lot of distros. As such, it’s a great way to get started securing your sever.
Before We Get Started
First, obviously, you want to make sure UFW is installed. It should be installed by default in Ubuntu, but if for some reason it’s not, you can install the package using aptitude or apt-get using the following command:
On DigitalOcean VPS this is installed by default.
sudo apt-get install ufw
Check the Status
You can check the status of UFW by typing:
sudo ufw status
Right now, it will probably tell you it is inactive. Whenever ufw is active, you’ll get a listing of the current rules that looks similar to this:
Status: active To Action From -- ------ ---- 22 ALLOW Anywhere
Using IPv6 with UFW
If your VPS is configured for IPv6, ensure that UFW is configured to support IPv6 so that will configure both your IPv4 and IPv6 firewall rules. To do this, open the UFW configuration with this command:
On DigitalOcean VPS this is enabled by default.
sudo vi /etc/default/ufw
Then make sure “IPV6” is set to “yes”, like so:
Now UFW will configure the firewall for both IPv4 and IPv6, when appropriate.
Set Up Defaults
One of the things that will make setting up any firewall easier is to define some default rules for allowing and denying connections. UFW’s defaults are to deny all incoming connections and allow all outgoing connections. This means anyone trying to reach your cloud server would not be able to connect, while any application within the server would be able to reach the outside world. To set the defaults used by UFW, you would use the following commands:
sudo ufw default deny incoming
sudo ufw default allow outgoing
The syntax is pretty simple. You change the firewall rules by issuing commands in the terminal. If we turned on our firewall now, it would deny all incoming connections. If you’re connected over SSH to your cloud server, that would be a problem because you would be locked out of your server. Let’s enable SSH connections to our server to prevent that from happening:
sudo ufw allow ssh
This command allows a connection on port 22 using the TCP protocol.
sudo ufw allow 8080/tcp
sudo ufw allow 8443/tcp
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
Our default set up is to deny all incoming connections. This makes the firewall rules easier to administer since we are only selectively allowing certain ports and IP addresses through. However, if you want to flip it and open up all your server’s ports (not recommended), you could allow all connections and then restrictively deny ports you didn’t want to give access to by replacing “allow” with “deny” in the commands above. For example:
sudo ufw allow 80/tcp
would allow access to port 80 while:
sudo ufw deny 80/tcp
would deny access to port 80.
There are two options to delete rules. The most straightforward one is to use the following syntax:
sudo ufw delete allow ssh
As you can see, we use the command “delete” and input the rules you want to eliminate after that. Other examples include:
sudo ufw delete allow 80/tcp
This can get tricky when you have rules that are long and complex.
A simpler, two-step alternative is to type:
sudo ufw status numbered
which will have UFW list out all the current rules in a numbered list. Then, we issue the command:
sudo ufw delete [number]
where “[number]” is the line number from the previous command.
Turn It On
After we’ve gotten UFW to where we want it, we can turn it on using this command (remember: if you’re connecting via SSH, make sure you’ve set your SSH port, commonly port 22, to be allowed to receive connections):
sudo ufw enable
You should see the command prompt again if it all went well. You can check the status of your rules now by typing:
sudo ufw status
sudo ufw status verbose
for the most thorough display.
To turn UFW off, use the following command:
sudo ufw disable
Configure Apache Tomcat
Setup HTTP connector on 8080 and HTTPS connector on 8443. In your
<Connector> declaration add
proxyPort attribute and set it to default HTTP and HTTPS port ( 80 and 443 respectively ). Setup firewall redirect rule from 80 to 8080 and from 443 to 8443. Then the server will accept regular http and https URLs without the need to specify port numbers.
Below is a sample declaration of these connectors.
sudo vi /var/lib/tomcat7/conf/server.xml
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" URIEncoding="UTF-8" redirectPort="8443" /> <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true" redirectPort="8443" keystoreFile="keys/domain.keystore" keystorePass="nekipassword" enableLookups="false" keyAlias="mykey" maxThreads="150" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS" />