How To Secure An Ubuntu 16.04 LTS Server - Part 1 The Basics
How To Secure An Ubuntu 16.04 LTS Server - Part 1 The Basics
04 LTS server -
Part 1 The Basics
Submitted by The Fan Club on
This guide is based on various community forum posts and webpages. Special thanks to all. All
comments and improvements are very welcome as this is purely a personal experimental project at
this point and must be considered a work in progress.
This guide is intended as a relatively easy step by step guide to:
Harden the security on an Ubuntu 16.04 LTS server by installing and configuring the following:
1. Install and configure Firewall - ufw
2. Secure shared memory - fstab
3. SSH - Key based login, disable root login and change port
4. Apache SSL - Disable SSL v3 support
5. Protect su by limiting access only to admin group
6. Harden network with sysctl settings
7. Disable Open DNS Recursion and Remove Version Info - Bind9 DNS
8. Prevent IP Spoofing
9. Harden PHP for security
10.Restrict Apache Information Leakage
11.Install and configure Apache application firewall - ModSecurity
12.Protect from DDOS (Denial of Service) attacks with ModEvasive
13.Scan logs and ban suspicious hosts - DenyHosts and Fail2Ban
14.Intrusion Detection - PSAD
15.Check for RootKits - RKHunter and CHKRootKit
16.Scan open Ports - Nmap
17.Analyse system LOG files - LogWatch
18.SELinux - Apparmor
19.Audit your system security - Tiger and Tripwire
Requirements:
Ubuntu 16.04 LTS or later server with a standard LAMP stack installed.
1. Firewall - UFW
A good place to start is to install a Firewall.
UFW - Uncomplicated Firewall is a basic firewall that works very well and easy to
configure with its Firewall configuration tool - gufw, or use Shorewall, fwbuilder, or
Firestarter.
Use Firestarter GUI to configure your firewall or refer to the Ubuntu Server Guide, UFW
manual pages or the Ubuntu UFW community documentation.
Install UFW and enable, open a terminal window and enter :
sudo apt-get install ufw
Add the following line and save. You will need to reboot for this setting to take effect :
Note : This only is works in Ubuntu 12.10 or later - For earlier Ubuntu versions
replace /run/shm with /dev/shm
Save and Reboot when done
tmpfs /run/shm tmpfs defaults,noexec,nosuid 0 0
3. SSH Hardening - key based login, disable root login and
change port.
The best way to secure SSH is to use public/private key based login. See
SSH/OpenSSH/Keys
If you have to use password authentication, the easiest way to secure SSH is to disable root
login and change the SSH port to something different than the standard port 22.
Before disabling the root login create a new SSH user and make sure the user belongs to the
admin group (see step 4. below regarding the admin group).
if you change the SSH port keep the port number below 1024 as these are priviledged ports
that can only be opened by root or processes running as root.
If you change the SSH port also open the new port you have chosen on the firewall and close
port 22.
Open a Terminal Window and enter :
sudo vi /etc/ssh/sshd_config
Edit the /etc/sysctl.conf file and un-comment or add the following lines :
# IP Spoofing protection
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1
# Log Martians
net.ipv4.conf.all.log_martians = 1
net.ipv4.icmp_ignore_bogus_error_responses = 1
Restart BIND DNS server. Open a Terminal and enter the following :
sudo service bind9 restart
8. Prevent IP Spoofing.
Open a Terminal and enter the following :
sudo vi /etc/host.conf
After installation edit the configuration file /etc/denyhosts.conf and change the email, and
other settings as required.
To edit the admin email settings open a terminal window and enter:
sudo vi /etc/denyhosts.conf
Fail2ban is more advanced than DenyHosts as it extends the log monitoring to other services
including SSH, Apache, Courier, FTP, and more.
Fail2ban scans log files and bans IPs that show the malicious signs -- too many password
failures, seeking for exploits, etc.
Generally Fail2Ban then used to update firewall rules to reject the IP addresses for a
specified amount of time, although any arbitrary other action could also be configured.
Out of the box Fail2Ban comes with filters for various services (apache, courier, ftp, ssh,
etc).
Open a Terminal and enter the following :
sudo apt-get install fail2ban
After installation edit the configuration file /etc/fail2ban/jail.local and create the filter rules
as required.
To edit the settings open a terminal window and enter:
sudo vi /etc/fail2ban/jail.conf
Activate all the services you would like fail2ban to monitor by changing enabled = false to
enabled = true
For example if you would like to enable the SSH monitoring and banning jail, find the line
below and change enabled from false to true. Thats it.
[sshd]
enabled = true
port = ssh
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
If you have selected a non-standard SSH port in step 3 then you need to change the port
setting in fail2ban from ssh which by default is port 22, to your new port number, for
example if you have chosen 1234 then port = 1234
[sshd]
enabled = true
port = <ENTER YOUR SSH PORT NUMBER HERE>
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
If you would like to receive emails from Fail2Ban if hosts are banned change the following
line to your email address.
destemail = root@localhost
to:
action = %(action_mwl)s
You can also create rule filters for the various services that you would like fail2ban to
monitor that is not supplied by default.
sudo vi /etc/fail2ban/jail.local
Good instructions on how to configure fail2ban and create the various filters can be found
on HowtoForge - click here for an example
When done with the configuration of Fail2Ban restart the service with :
sudo service fail2ban restart
Then for basic configuration see How to install PSAD Intrusion Detection on Ubuntu 12.04
LTS server and follow from step 2:
To update and run RKHunter. Open a Terminal and enter the following :
sudo rkhunter --update
sudo rkhunter --propupd
sudo rkhunter --check
16. Scan open ports - Nmap.
Nmap ("Network Mapper") is a free and open source utility for network discovery and
security auditing.
Open a Terminal and enter the following :
sudo apt-get install nmap
To email a logwatch report for the past 7 days to an email address, enter the following
and replace [email protected] with the required email. :
sudo logwatch --mailto [email protected] --output mail --format html --range
'between -7 days and today'
To setup Tripwire good installation guides can be found on Digital Ocean here and on
Unixmen here
To run tiger enter :
sudo tiger
Tags:
Ubuntu 16.04
U