Securing an Ubuntu Server

For more current information, see: http://www.adminbuntu.com/security

Security is relative. Will these steps make your server “secure”? It will be more secure than it was before. And more secure than most servers. Your server will not be “low hanging fruit”. Security is an on-going process. It includes settings, practices and procedures. Make it your business to regularly read about security and to understand the concepts and our system. Paranoia is useful with regard to server security.

I’ve tested what is presented here in Ubuntu Server 10.04 (Lucid) and 10.10 (Maverick). If you want to harden your new Ubuntu server, this is a good start.

Ubuntu server is well designed, regularly updated and relatively secure. The Ubuntu Security Team manifests an onging effort to keep Ubuntu secure. Regular security updates are available and easy to implement.

  • No open ports
  • Role-based administration
  • No X server
  • Security updates
  • Kernel and compiler hardening

In this post, we are going to meet the security challenge in with multi-pronged effort that will include: system analysis, changing settings for additional hardening against attack, installing a firewall maintenance system, scanning for rootkits, and offering a regular maintenance regimen.

  • Change settings for increased security
  • Implement UFW, the uncomplicated firewall
  • Use denyhosts to automatically blacklist attackers
  • Scan the system for vulnerabilities with Tiger
  • Detect attempted intrusions with psad
  • Install nmap and scan the system for open ports
  • Check the system for rootkits with chkrootkit
  • Monitor logs

Change settings for increased security

see also: https://help.ubuntu.com/community/StricterDefaults

Secure shared memory

/dev/shm can be used in an attack against a running service, such as httpd. Modify /etc/fstab to make it more secure.

sudo vi /etc/fstab

Add this line:

tmpfs     /dev/shm     tmpfs     defaults,noexec,nosuid     0     0

Disable root SSH login

The root account is disabled by default in Ubuntu. If you installed Ubuntu on Slicehost or Linode, root is enabled. In any case, it is a good idea to disable root SSH access. Edit /etc/ssh/sshd_config and set PermitRootLogin to no.

sudo vi /etc/ssh/sshd_config

Change PermitRootLogin to no:

PermitRootLogin no

Of course, if you access your server via SSH, you should make sure you have sudo working for your user before disabling SSH root access.

Only allow admin users to use su

This helps prevent privilege escalation.

By default, Ubuntu does not have an admin group. Create an admin group:

sudo groupadd admin

Add yourself to the admin group:

sudo usermod -a -G admin andrew

Restrict access to /bin/su to admin group members:

sudo dpkg-statoverride --update --add root admin 4750 /bin/su

Check permissions for /bin/su with:

ls -lh /bin/su

…and see the following:

-rwsr-x--- 1 root admin 31K 2010-01-26 17:09 /bin/su

Do not permit source routing of incoming packets

see also: http://www.cromwell-intl.com/security/security-stack-hardening.html

sudo sysctl -w net.ipv4.conf.all.accept_source_route=0
sudo sysctl ­-w net.ipv4.conf.default.accept_source_route=0

Don’t allow system users to access an FTP server

This is only needed is ftpd is installed and running. Only if you’ve installed ftpd. However, it is Ok to do this anyway and it will remove a FAIL from the tiger report.

SFTP is probably better than FTP, if it is usable for your files transfer needs.

see ftpusers manual: http://manpages.ubuntu.com/manpages/lucid/man5/ftpusers.5.html

Edit /etc/ftpusers:

sudo vi /etc/ftpusers

Add system users to deny use of ftpd:

backup
bin
daemon
games
gnats
irc
libuuid
list
lp
mail
man
mysql
news
ntp
postfix
proxy
sshd
sync
sys
syslog
uucp
www-data

UFW: basic firewall

previous post: Ubuntu UFW Uncomplicated Firewall Examples

community documentation: https://help.ubuntu.com/community/UFW

server guide: https://help.ubuntu.com/10.04/serverguide/C/firewall.html

ufw manual: http://manpages.ubuntu.com/manpages/lucid/en/man8/ufw.8.html

project wiki: https://wiki.ubuntu.com/UncomplicatedFirewall

nice article: http://savvyadmin.com/ubuntus-ufw/

UFW (Uncomplicated Firewall) provides an easy to understand interface to control iptables (iptables conteol Netfilter, which is built into the kernel). Will just a few commands, your server can control access. Checking status is also easy.

UFW (uncomplicated firewall) is a simple interface used to configure iptables.

Install and enable Uncomplicated Firewall:

sudo aptitude install -y ufw
sudo ufw enable

Display available UFW commands:

sudo ufw show

Display UFW configuration:

sudo ufw status

Allow SSH and HTTP access to the Apache server:

sudo ufw allow ssh
sudo ufw allow http

In the above example, ports for OpenSSH and Apache were opened by service name (“ssh” and “http”). You can use a port number instead of the service name (like “80″ instead of “http”).

See services running and which names to use:

The practice here is to open only ports that you use – ports that use a service that have a service running. To see a list of services that you have running for which you might want to open ports for:

sudo ufw app list

To see a list of services that UFW uses (like in the “sudo ufw allow ssh” example, above):

less /etc/services

Denyhosts: avoid SSH attacks

project: http://denyhosts.sourceforge.net/

Looking at /var/log/auth.log on servers that I manage shows a steady streams of attacks on SSH. I am countering these attacks in a number of ways, starting with denyhosts.

Denyhosts periodically scans /var/log/auth.log for repeated failures to access the system via SSH. It then adds these offenders to /etc/hosts.deny. See the project page for details.

sudo aptitude -y install denyhosts

That does it – the rest is automatic. You can see the IP addresses added to /etc/hosts.deny with:

sudo less /etc/hosts.deny

Tiger: security system scanner

project: http://www.nongnu.org/tiger/

Tiger creates an automated security audit by analyzing files and settings on the system and creating a report listing what has been analyzed and listing warning, alerts and failures.

The tiger command creates a report of potential security problems in /var/log/tiger. The use the tigexp command to look up the resulting codes generated for a detailed explanation and what to do to make the system more secure. The problems tiger considers most serious are marked with FAIL.

It has been a while since Tiger has been updated. It still produces a useful report.

Install tiger:

sudo aptitude -y install tiger

Run tiger to create a report of security issues.

sudo tiger

Use less to view the most recent tiger report:

sudo -i
less /var/log/tiger/`ls -t1 /var/log/tiger | head -1`
exit

Use tigexp to list explanations for FAIL codes:

tigexp dev002f

Google is also helpful, naturally.

Ignore these:

--FAIL-- [dev002f] /dev/fuse has world permissions
--FAIL-- [logf005f] Log file /var/log/btmp permission should be 660

Changing permissions for these could cause problems.

Detect attempted intrusions with psad

project: http://www.cipherdyne.org/psad/

Psad is a collection of lightweight daemons that log attempted intrusions, in particular monitoring iptables.

Installation:

sudo aptitude -y install psad

The daemons will run automatically.

To check current status:

sudo psad -S

You can modify psad settings to e-mail the admin in the event of intrusion detection.

Nmap: port scanning

project: http://nmap.org/

This allows you to see which ports are open, verifying that UFW/iptables is working correctly.

Installing nmap:

sudo aptitude install -y nmap

Port scanning:

nmap -v -sT localhost

SYN Scanning:

sudo nmap -v -sS localhost

scan type explanations: http://nmap.org/book/man-port-scanning-techniques.html

Chkrootkit: check for rootkit presence

project: http://www.chkrootkit.org/

Chkrootkit scans the system for evidence that a rootkit has been installed.

This is a confidence test to be used to test whether your system has been compromised. In a perfect world you would not need this…but in this world, it is good to run periodically.

Installing chkrootkit:

sudo aptitude install -y chkrootkit

Running chkrootkit:

sudo chkrootkit

LogWatch

Ubuntu community documentation: https://help.ubuntu.com/community/Logwatch

The most detailed and informative logs in the world are useless if no one looks at them. Logwatch winnows the deluge to a succinct report…which you will look at. Even so, familiarize yourself with your system’s logs and review them on a regular basis. A daily logwatch habit would be a good start.

Installation:

sudo aptitude -y install logwatch

Usage:

sudo logwatch | less

Ongoing maintenance

Your server is now more secure. Once a week, perform on-going maintenance.

Updating software:

sudo aptitude update
sudo aptitude safe-upgrade

The safe-upgrade action is preferred by me because it does not upgrade packages that rely on dependencies that have not been upgraded to required levels.

see: http://wiki.debian.org/Aptitude

Or, you could set-up automatic security updates, if you cannot do the weekly maintenance. This is not a perfect solution because an administrator is not monitoring what is being updated and testing after updates. see: https://help.ubuntu.com/10.04/serverguide/C/automatic-updates.html

Check for attempted instrusions:

sudo psad -S

UPDATED: Analyze system with tiger. Because the tiger reports in /var/log/tiger/are owned by root, run these commands one at a time. (This solves a problem some people were having with permissions.)

sudo -i
tiger
grep FAIL /var/log/tiger/`ls -t1 /var/log/tiger | head -1`
exit

In the above, FAILs are pulled from the newest report file with grep. The ls clause in backticks gives grep the newest file in the directory. The sudo -i command allows you to run multiple commands as root, ending with exit.

Use tigexp to list explanations for FAIL codes:

tigexp dev002f

Scan ports with nmap:

sudo nmap -v -sS localhost

Check for rootkits

sudo chkrootkit

Look at logs:

sudo logwatch | less

Keep up with trends

visit: http://www.linuxsecurity.com/

Elsewhere

http://www.itsecurity.com/features/ubuntu-secure-install-resource/

http://www.cyberciti.biz/tips/linux-security.html

Be Sociable, Share!

43 thoughts on “Securing an Ubuntu Server

  1. Fantastic review! For some reason I could not figure out how this command works:

    sudo grep FAIL /var/log/tiger/`sudo ls -t1 /var/log/tiger | head -1`

    It kept spitting out errors about ‘sudo ls…’ not being found. So I modified the permissions of /var/log/tiger so that I could just grep FAIL /var/log/tiger/*securityreporthere* and shazzam!

    Thank you!

  2. @Peter: Glad to hear it and well done! The security on /var/log/tiger/ is probably a bit much. It seems like it should have a group ownership of adm with rights to read the reports. I modified the instructions based on your experience.

  3. Thanks for sharing your knowledge. This is exactly what I was looking for. :)

  4. Thank you. This was tremendously helpful — broad in scope, with clear, succinct explanations.

  5. I would recommend using the grsecurity.net kernel patches to the linux kernel (http://grsecurity.net/ and http://grsecurity.net/~spender for the latest patch). This by itself locks down a linux server. It prevents non-root users from executing code outside of the bin directories (trusted path execution). It prevents buffer overflow exploits from executing code on the stack (no-exec stack). It prevents non-root users from seeing other users processes by making /proc read-only. So, if an attacker does actually gain access to the server, they really can’t do anything interesting.

  6. Note that tiger installs sendmail as a dependancy, which you might not want to do.

  7. Nice article!
    No intention to plug, but you might like my open source project Lynis as well, to check how well the system is hardened and what other tips are available!

  8. Nice post. Apart from editing the /etc/ssh/sshd_config to set the permission to no, I think you can also disable root account with this command: $sudo passwd -l root

    Thanks again.

  9. @Helen: Editing the /etc/ssh/sshd_config to set the permission to no disallows root from logging in via SSH and “sudo passwd -l root” disables the root account all together. -A

  10. Great Howto! Will these steps work on securing the desktop too, or is it too much/too little for locking it down?

  11. @John: Ubuntu desktop is pretty secure for a desktop. All these measures are based on what Internet-facing servers get hit with. Man, the number of attacks on some of my servers is unbelievable.

  12. @Andrew – That is what I have been reading about it security, I am not worried about my home system, but I recently loaded on an old laptop and am just trying to keep it nice and secure when I use public WiFi, I do always make sure to VPN back home just to be safe :) I can only imagine the amount of attacks you see on your servers though.

  13. I’ve got this message, how can I solve it without compromising installation?

    –FAIL– [lin016f] The system permits source routing from incoming packets
    –FAIL– [lin005f] Installed file `/usr/share/bind9/bind9-default.md5sum’
    –FAIL– [lin005f] Installed file
    –FAIL– [dev002f] /dev/fuse has world permissions
    –FAIL– [dev002f] /dev/rfkill has world permissions
    –FAIL– [netw018f] Administrative user avahi allowed access in /etc/ftpusers
    –FAIL– [netw018f] Administrative user bind allowed access in /etc/ftpusers
    –FAIL– [netw018f] Administrative user messagebus allowed access in
    –FAIL– [netw018f] Administrative user openpanel-core allowed access in
    –FAIL– [netw018f] Administrative user vmail allowed access in /etc/ftpusers

    Because following this “Only allow admin users to use su” I’ve been locked out of my system. Needed a restore.

  14. thx for your nice how-to :) for beginners a good point to start…

    now my ubuntu-server is a bit more secure :)

  15. Great post. Is this still safe to use two years later? Any better applications out now that you’d use instead?

  16. Thanks for the great tutorial, straight forward now messing around, I am a newbie, although I have installed ubuntu server before I had never found a good site for quick secure set ups from the get go.

    Would webmin be a good interface to use outside of running manual system check commands from the server itself.

    Again Thanks….

  17. I see a lot of interesting posts on your page. You have to spend a
    lot of time writing, i know how to save you a lot of time, there is a tool that creates readable, SEO
    friendly posts in couple of seconds, just search in google
    - k2 unlimited content

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>