1st Five Minutes on a Server 4

Posted by JD 02/28/2014 at 17:00

11/2018 Update

For Ubuntu 18.04, the commands have changed for network setup thanks to netplan, but the major ideas are still the same.
Hammond is active on the Ubuntu Forums. He has many server setup guides

6/2016 Update

Nothing major, just minor tweaks and a few details added.

4/2015 Update

Seems that lots of people are interested in extremely basic security of Linux systems and would like to see what others do initially on their fresh servers or VMs. So, I did a little googling (DRY, right?) to see what a few others are doing and to compare that with my setup steps. We all come from different places, backgrounds, and have different things we want out of our servers.

This is about basic, first-pass security, not securing Apache, MySQL, Tomcat, email, or any webapp. The box has an ssh-server, known login, DHCP IP and is ready for other stuff. That is our starting point.

Let’s get started.

My Methods and Goals

I use Ansible as a DevOps tool. That decision took a few years to solidify after trying puppet, Rexify, SaltStack and Chef. Ansible is easy to start and can grow into any necessary level of complexity needed. The greatest positive for Ansible is that it is based on ssh. That is also the negative. In just 15-30 minutes you can be managing your systems with Ansible too. If I were Facebook or of that scale, I might choose something else, but for 1000 servers or less, Ansible is easily capable out of the box.
This is NOT an article about Ansible. Previously, I used a simple bash script to repeat the initial server setups and prior to that, a text file that I’d copy/paste commands from into every new server. I like that Ansible will let me break things up into tasks and include what is relevant for a single server, group(s) of servers or all servers. Some configuration changes apply to server and desktops, others only apply to a server OR a desktop and others only apply to a specific type of machine (MariaDB, Redmind, wikimedia, etc.) and nothing else.
Regardless of where you are on the automation scale, just creating something standard is important. Don’t feel bad or good just because you copy/paste or have things scripted. It is about the results, after all – a more secure server and a properly configured base server are the goals. Consistency matters, where it makes sense.

My network has a few things running on it that I want every other server to leverage. Leveraging those services is important. Things like:

  • apt-cacher-ng (package cache to avoid excess internet traffic that isn’t needed)
  • NTP server (time)
  • munin server (monitoring)
  • email server
  • backup server

Starting from a fresh Ubuntu Server installation with a deploy account (or my personal account) and ssh-server running, but nothing else. IPs are still DHCP provided.

First 5 Minutes

My account already exists on the new server, since I installed it and it is using DHCP for IPs. I do base installs manually. Bringing up a new server happens once every few months, so that isn’t a big deal and takes about 15 minutes without really watching. Automating initial server installs might be a next step if I were doing 1 a week.

Overview of Steps

Manually determine the IP for the server. This can be from the hosting provider or a console login or arp. My servers come up with DHCP addresses, so I need to ssh into them on the DHCP portion of the server subnet. Then a few manual steps are needed.

  1. ssh keys (ssh-copy-id) from the workstation running Ansible (not really a server )
  2. purge nano – easier than setting up vi/vim as my default, plus saves 2Kb of storage. ;) I extremely dislike nano.
  3. fix the sudoers for my account (or deploy accnt)

Then I run an ansible-playbook to install, configure the next things:

  1. static IPs or bridge configurations
  2. personal settings, bash functions, aliases, scripts (backups, kernel-cleanup, etc)
  3. setup apt to point to apt-cacher-ng leveraging an internal package cache server
  4. aptitude full-upgrade (gets the latest packages AND the latest kernel) (apt is fine too)
  5. aptitude install my base packages (about 25 things); more later
  6. sshd_config settings (no root, force v2 only connections, disallow passwords from outside the LAN)
  7. Logwatch settings (protect ssh, email address)
  8. /etc/hosts file for the LAN; for small networks, this is easier than internal DNS
  9. munin-node config for monitoring
  10. satellite email server setup; all systems can send email.
  11. email aliases so system mail is forwarded somewhere
  12. Performance monitoring (still missing real alarming)
  13. NTP client settings ; accurate time is very important for security AND for troubleshooting later
  14. Setup networking; interfaces for static IP, jumbo frames, dns-servers, etc.
  15. Restart any services due to changed config files – this will change the IP and ssh connectivity will be lost.

Now we have a static IP and can ssh in without a password.

Base Packages?

By installing certain base packages, commonly needed tools are available. The base list:

  • acpid – so a client VM can receive shutdown requests from the host VM server
  • openssh-server – never forget this. Might be running on a console. Should already be installed during the server installation.
  • fail2ban – dynamically block any failed ssh attacks. I can’t think of any reason NOT to have this on every box. The defaults are sufficient for LAN ssh protection on port 22. If you move sshd to a different port, fail2ban config needs updates too.
  • iptables-persistent – no need to manually save/restore iptable rules
  • postfix – system emails need an MTA to get forwarded to a real email address; only sends email, does NOT receive from outside the machine/VM.
  • aptitude – aptitude is smarter about package dependencies than apt-get. With 16.04, I started using apt as a replacement. Until I see how smart apt is for non-trivial dependencies, I want aptitude available.
  • sysstat – performance capture tool library.
  • munin-node – performance reporting/monitoring; setup BEFORE it is needed! Capturing this data for a few weeks before there are any issues makes trouble shooting issues much easier.
  • ethtool – want jumbo frames and expanded network settings control?
  • rsync – duh. MANDATORY
  • rdiff-backup – my backup tool of choice Best How-To – rdiff-backup is least bad of all the choices I’ve found. Very much like rsync, but with versioning based on diffs, not hard-links. 30 days of versioned backups are less than 1.20x the original storage.
  • mlocate – locate is instantaneous and replaces find for many needs (package name may have changed to locate )
  • logwatch – daily reports of server activity (emailed, so MTA needed)
  • lshw – create daily system hardware configs, compare today to yesterday and highlight any changes
  • ntp – Keeping accurate time is a security consideration. Also, when troubleshooting complex client/server systems, having all the timestamps on logfiles match to the 0.001 sec makes finding root causes much easier. Once had to troubleshoot 50 systems, each with different timestamps. What a pain.
  • tree – nice directory tree visualization – find . -type d replacement
  • heirloom-mailx – provides a CLI mail program, handy for testing the send part of email.
  • software-properties-common – PPA helper
  • Ubuntu 16.04 has a few more dependencies – mainly for python APT support which ansible needs.
  • Personal scripts for capturing system information, cleaning up old kernels and making backups. On desktops, many more scripts will be copied to my ~/bin/ from another desktop. This is manual. I’ve decided to keep 1 system of record for my ~/bin, so any new scripts I create go posted to that machine for later use – even if the script will not work on that box due to dependencies. For example, wrote a script to convert Spanish captions into English captions. There is a specific tool required for the translations which is only installed and configured on the Plex Server machine.

Too much bloat?
What is missing?

Missing and ToDo Steps

  • Backups!!! Backups are the primary security tool for any computer system. My backups are special for each snowflake server, but I start with the same basic script. Currently pushing backups to a central server, but need to pull those backups to better manage backup windows and I/O contention on the backup disks.
  • Firewall settings – default deny, except ssh managed by fail2ban; then open specific ports as needed; also need to block all outbound SMTP that aren’t to the primary email server
  • Email front-end server firewall blocks for known spamming subnets that I just never want to allow, ever, again.
  • Monitoring – Monit or munin is easy to config on nodes. The server should be easy too, just haven’t gotten there.
  • Alarming – nagios or opennms are standard tools for this.
  • Application settings, working AND secure configs; these are generally part of the server Ansible playbook which gets created just AFTER all these other things are handled. There is a flaw in my method – if I have great backups, then why do I need the Ansible playbook to rebuild a specific server? Well, I don’t. If I had 2-500 of the same server types, then ansible to rebuild them would make more sense. All my systems are snowflakes, so daily backups are still needed, with restores being the primary recovery process.

Category5.tv has a 3 part video on using LVM with rdiff-backup – eps 455, 461 and 465. youtube has the videos.

Automatic security updates ARE NOT performed. I’ve been burned by updates, so I want to control exactly when this happens, know what is changed (logwatch) and have a backup that is solid to be able to restore, if necessary. The scripted patching process does create detailed logs, so if something bad happens that is seen quickly, it is easy to find the culprit.

I manually run a script with apt-get that does an update and full-upgrade every Saturday morning across almost every Linux machine on the network. A log file is created. Changed from aptitude for scripted runs like this.

No DNS server – The 2nd time my small network was hacked was through BIND. I don’t run it anymore, leaving that for paid professionals.

DHCP Reservations – these are manually added for all portable devices. Nice to have them work correctly when off this network, but also have static IPs when on the LAN. Servers have static IPs configured.

Ansible

Ansible is one of those tools like sudo and ssh and rsync that is very easy to get started, but has huge depths in capabilities if we dig a little deeper.

  • Simple Ansible Tutorial. Ansible is one of the few tools that I’m willing to install from source. It only gets installed on 1 machine, a workstation. The Ubuntu package version are missing some very important fixes and Ansible is under very active development, improved all the time. I use git clone to get it, then read the README.md for instructions. It is only needed on 1 machine, not any of the servers to be managed. Find the code on github.
  • Another Ansible Introduction.
  • More complete Ansible info simple to real-world. Some interesting techniques.
  • For visual learners, like me, a good 20 min video can make things “click” and reduce the time to understanding.

More Security

This initial setup is only good for a LAN server/desktop, not enough security for an internet-facing box.There are 30-100 more things to harden it. Bob Toxen literally Wrote The Book on Linux security. Find it at bookstores and Amazon, if you’d like to know more.

Making a Linux internet server secure is a constantly moving target. Attacks change daily and we need to be prepared as administrators. If you can’t spend 60 minutes a week maintaining a running server, best to outsource that task. Maintaining an internet server is fun for a month, but after that, it is work. Internal LAN servers are much easier provided they aren’t available over the internet.

25 Linux Hardening Tips

There is the Debian Security Manual and the Debian Hardening Guide. RHEL/CentOS and those flavors of Linux are hardened in very different ways, thanks to SELinux facilities. Of course, we should never forget the SANS Security Checklist or the NSA Security Guides or the OWASP Guides.

For desktop security, the Ubuntu Basic Security page is good to start, but nothing can help with desktop security more than an educated user and the less-friendly Ubuntu Security Pages. We all have some reading to do.

Of course, there are articles here on home network and desktop security too:

Methods by Other People

I get the feeling these guys are not system administrators, rather they are programmers doing DevOps jobs. I honestly do not know. Both have repeatable, standard, processes which is the most important thing for all of us.

Other Pros Using Ansible

Better Ways?

  • What do you do?
  • What did I get completely wrong? Sometimes I’m an idiot. ;)

Let me know in the comments.

  1. Miguel Santos 03/07/2014 at 00:44

    Just wanted to thank you for this. A lot! You have here a very good primer on Linux security and some links to more advanced stuff.
    Cheers!

  2. JD 03/22/2014 at 13:06

    In the last week, I’ve been using a 13.10 Ubuntu system on a Chromebook. Don’t know if the installation was standard or not, but I do know that packages which are always installed were not. I’ve had to add a few packages to my “core packages” list to keep my sanity.

    • bash_completion
    • aptitude
      Cannot believe these are not included anymore.
      Also had to screw around with systemd configuration for the first time. Meh.
  3. charlesbrown 08/19/2014 at 06:33

    JD,

    Last winter you and I were talking and this is exactly what I was looking for. A place to start on servers. I have a new one inside my network and I have begun to experiment. I did exactly what you suggested but, the only ones that I found were very brief and not so complete as you have done. I have not put anything on the server yet. I have just been playing with ssh so far. I really appreciate your efforts.

    Charlie

  4. JD 08/20/2014 at 11:31

    heirloom-mailx added package – 14.04 doesn’t come with a Mail CLI program!

    Yesterday, I upgraded the center server of my network from Ubuntu 10.04 to 14.04.1. Upgrade does NOT describe what happened. Found the 20G partition with the OS and found the swap, then booted off a YUMI-created 14.04.1 flash drive and installed. Formatted the last JFS / on my network into ext4 and got a 100% clean OS install. Setup my ssh-keys, purged nano, and setup sudo – then created a specific ansible script just for this snowflake server by copy/pasting my normal KVM-server script into the hostname.yml file.

    $ ansible-playbook romulus.yml

    and walked away for 10 minutes (after ensuring the first ssh and sudo commands worked). Came back and fixed the few errors – never properly handled bridged networks in ansible before – solved. BAM – server installed and working.

    Ok – it wasn’t really that easy and with a server like this one, the center of my network, it has lots and lots of very special configurations. It is the only NTP server for the network, as an example. It is the central backup server here too – all other systems backup onto storage that it holds. Snowflake? YES!

    I need to start using Ansible “roles” more. I sorta am with my tasks, but not in the most flexible ways.