HACKER Q&A
📣 z9znz

What Linux setup/hardening guide do you use?


When setting up a new publicly accessible Linux server, it is usually necessary to make some changes from the defaults to improve security. There are many guides which can be followed, some from presumably reputable sources. However, the details depend quite a bit on Linux distro, version, and the current date.

Considering Ubuntu (22.02 LTS), what setup and hardening guide do you follow?

And if not using Ubuntu, or using a different version, please describe what you use and why (and if being so generous, also what guide you follow).

TIA!


  👤 norenh Accepted Answer ✓
My personal preference is to begin with STIG from DoD Cyber Exchange: https://public.cyber.mil/stigs/downloads/

They take a while to come for the latest version of a OS and none is out for Ubuntu later than 20.04 yet, but it is a very good starting point for a checklist even if you do not run a DoD system (a lot of the points might be specific for these but easily skipped) and depending on changes between versions, some parts might still apply. For a recently released OS you usually have to rely on the system documentation from the system creator (Canonical in this case) but most of my experience is from the Red Hat world so not sure how good it is for other distros.

If you start with the best practices for your OS/distro and then dig into a hardening guide (like STIG) you should probably have a pretty good understanding and base documentation for your specific use-case. As always, if you use a specific service/software on top of that (a web-server, mail-server, etc) you will need to dig into the documentation of that specific product and after that check for hardening guides.

The general principle of removing/disabling/blocking everything that you do not use is usually a good one. Knowing what you actually use and the exceptions is what makes it hard :-)


👤 s_ting765
Hardening against vulnerabilities is a zero sum game ultimately.

It is my experience that most guides you will find for Linux hardening are either outdated, too opinionated or are written with shock value to induce paranoia [0].

I harden ssh (no root login, no passwords), setup fail2ban for it and leave it at that.

Only thing I remember to do is not to mess with security configurations e.g disabling selinux to make something work. Keeping number of software installed to an absolute minimum. And last but not least I setup automatic updates via unattended-upgrades.

[0] https://madaidans-insecurities.github.io/guides/linux-harden...


👤 thedougd
Find your favorite reference (e.g. CIS Benchmark Level 1/2, etc) and look for an open source implementation in configuration management code from a credible source. You can pear back from there. You’ll likely find they’ve implemented the configuration as well as the auditing.

I use to download the Cookbooks for CIS benchmark from Chef’s repos.

An important detail is that these frameworks will typically word a control as "Disable X, unless it is required by the application or not used”. Don’t beat yourself up for removing controls that are not applicable to your situation.

Once your application is installed and configured, with configuration management of course, set AppArmor or SELinux into permissive but logging mode. After profiling the application in use for a period, write a policy to allow those events for your application and enable enforcing mode.

Systemd offers some very simple to use controls as well. You can update your application unit to do sandboxing things like block access to system directories, block system capabilities, and even limit network access. These additions are very easy with significant rewards.

If you can bake it all into a machine image, glorious. If you need last mile configuration or personalization, use cloud-init to make those changes on first boot. If you need to save state, externalize it with an additional volume or network storage. Log to an external system, and disable remote access. Enforce a maximum life for a server to a couple of days and replace. Of course, many applications can’t survive as cattle and need to live longer or have difficult replacement procedures.


👤 hitpointdrew
Can’t believe some of the terrible advice here.

Stick with Ubuntu LTS, unless you are willing to pay for RHEL. Do NOT use Fedora or any Arch based distro, those have way too aggressive releases to be considered for any type of server (they are fine desktop or lab distros but I would never consider them for legit servers).

Ideally have a firewall (or security group if in AWS, etc) that sits in front of your server. Make sure only the absolute necessary ports are open (white list by ip if you can). Extra credit have Wire Guard server that you connect to and only allow ssh through that.

If you are the only person accessing a shell on the machine then make sure shh allows keys only, and make sure you private key has a pass phrase on it. Extra credit, use jump cloud (free if you have under 5 users), and it’s super easy to enforce 2FA with ssh.

If you have several people that need to ssh then use a short lived certificate solution like Teleport.


👤 NoboruWataya
I can't claim to have been through it but this is sitting on my bookmarks folder and looks very useful: https://github.com/imthenachoman/How-To-Secure-A-Linux-Serve...

My only tip I haven't seen mentioned here is be very careful using docker with ufw, as by default docker will effectively override ufw port restrictions if it is told to expose a port.


👤 fbhabbed
I am surprised noone mentioend the Arch Security (https://wiki.archlinux.org/title/security) page yet.

It applies to basically every distribution and has useful links at the bottom for even more tips.


👤 CameronNemo
Honestly I think nowadays the best idea is to not mess with defaults for the most part, but I do:

* Check open/listening ports and reconfigure or disable unnecessary services to limit attack surface. Do you really need an NTP server, or just a client? What about SNMP, you going to use that or do you use telegraf/metricbeat/whatever?

* Harden configs for any remaining services. SNMPv3 only. SSH key/cert based auth only. Disable insecure ciphers or protocols.

* Keep the distro PATCHED. So easy to pwn old ass sudo binaries, openssl versions, or kernels. Patch your shit. Can't overstate this.

* send audit logs to a central log store, for post-intrusion forensics


👤 bediger4000
I use Arch, btw. But I run the linux-hardened kernel, doing pacman -Syu once a week.

I also run endlessh on TCP port 22, and a fake HTTP server as a community service.


👤 halz
Really depends on the role of the system. Will it be multi-user/single-user? Hosting containers? Hosting virtual machines? Running as a virtual machine? Doing network magic? Each of these classifications will take hardening in different directions.

You can get a rough feel for the enthusiasm of your distro's desire to do 'hardened things' by checking its kernel config. Heres one such script that has consolidated some hardening guides; https://github.com/a13xp0p0v/kconfig-hardened-check and also spot check with http://kernsec.org/wiki/index.php/Kernel_Self_Protection_Pro...

Then can also see if the distro is doing things to harden the binaries (relro, nx, canaries, aslr, pie, etc) ; https://www.trapkit.de/tools/checksec/ Also! you may be surprised to find which distros are comfortable with allowing unprivileged user namespaces (kernel.unprivileged_userns_clone=1)


👤 marginalia_nu
Most server-oriented linux distributions come with fairly sensible defaults.

Maybe wouldn't set up a pubnix on them without some tweaking, but for most use cases the defaults are sane. Maybe disable password login on SSH and if you're worried.

Set up a honeypot if you don't believe me.


👤 cypherg
Use a trusted distro like Ubuntu LTS. Install security updates automatically.

Be aware and focus specifically on which ports/services are available from the internet.

If you absolutely need SSH, ensure you use something like fail2ban and use complex credentials. If possible, use obscurity to change from the default port of 22.

If you're running an internet-facing service, like nginx, ensure that it's always up to date.

Using SELinux and AppArmour and GRSecurity are usually way overkill and cumbersome to manage.


👤 archi42
For my personal servers/services? Everything behind an OpnSense and only accessible via VPN. There is one service on HTTPS a friend has to access, that got a nginx as reverse proxy with http basic auth & long random password in front of it. This reduces my attack surface to "people on my network or VPN", which is further segmented in VLANs. The OpnSense firewalls between VLANs, and especially the IoT crap & my VPN only have very limited access.

I HTTPS all the things except the Tasmota UIs. I use a SmallStep CA with ACME on my internal network to get certs for my internal stuff; to be safe, the root cert is NameConstrain'ed to `.lan`. Obviously everything is in my local DNS. If someone steals that approach: Make sure to put DHCP-assigned DNS names into something like `.dhcp.lan` ;-)

As you can see, this is already quite opinionated. Generally: Be minimal with what you expose, make sure to quickly update, setup SELinux, setup proper firewalls/VLANs for multi-host deployments. SSH only with keys, not with passwords. Compartmentalize, so that one breach doesn't pwn your machine(s). For that you need either more physical machines, or you can put stuff in containers (I prefer root-less podman over docker) or VMs (e.g. libvirt/qemu or ESXi).

Also, don't rely on some Ubuntu LTS to get all updates you need for "a long time". E.g. Tomcat is a community package and the 18.04 LTS was missing many (security) updates last time I've checked. I've also seen docker containers not being timely updated by their maintainers. Do some external port scans to not be surprised that you e.g. accidentially left your SQL open for the world. Because mistakes happen, and a `nmap -T5 -p1-65535 $host` doesn't hurt you.


👤 viraptor
Most is the guides online have the issue of being too specific to what the author cares about or just a dump of random "I thought it would be good" ideas. I wouldn't follow any specific ones really.

Instead, if you'd like to improve your security, I'd recommend 3 things:

- Learn basics of threat modelling. There are many posts about it, but in short it's asking: who can do what and what are the options to prevent that.

- Learn what's running in your system. What services you're running, what apps. What untrusted data hits any software.

- Go to the higher quality sources for what you actually care about. For example, you find out you're running samba - read the security sections of their docs https://www.samba.org/samba/docs/using_samba/ch09.html and system administration guides. Read about firewall configuration for your system.


👤 badrabbit
I like to live life dangerously, I use defaults.

Seriously though: Lynis when setting things up and then for work CIS.

But, that's just checking boxes. I do use apparmor to lockdown my browser and VMM but that's as far as I go and I have accepted that security on Linux is a dangerous illusion because there will always that one man page, that one setting everyone appsrently knows, that one permission you are expected to know by implication of having an understanding of some Linux subsystem where that is really all anyone targeting you needs to get in or move laterally. Logging more than you should and forwarding centrally (auditd and security logs) seems about all you can do (detection focused not prevention) that said, checking boxes is much better than defaults. Security is a reponsibility left to the user/admin by default and to be fair, Linux is hardlh alone in that regard,it just so happens to change a lot making it harder to keep up.


👤 Magellanic
I like to self-host and wrote a guide on the things I do to protect my servers [0], basically, hardening SSH, enabling automatic security updates, configuring Failban and installing a firewall.

[0] https://www.fuzzygrim.com/posts/secure-vps


👤 danwee
As a newbie, this is what I do:

- I put all my servers on a private network (e.g., db server, web server, monitoring server)

- The only server exposed to the internet is my load balancer (usually nginx) and a bastion server

- The bastion server only runs SSH. It's hardened (how good? I don't know; I just try to follow "best practices") and nothing else runs on it

- I access my internal servers via the bastion server

Not sure how secure my setup is, but I am not a "sec" guy, and the above setup is already above my average knowledge of software engineering (which is what I usually do). I know I could go for a VPN, or pay a saas to have better security, or use SELinux or what not...


👤 type0
SELinux, Rocky is good for servers (successor to CentOS)

👤 hagg3n
I've been hosting small and mid-sized web sites and applications for years without a hitch using my own script, written from reading it up online. It might not work exactly as is for you but perhaps can offer some reference: https://github.com/corenzan/provision

👤 tryauuum
I do things like disabling ipv6 (to avoid thinking about ipv6 firewall), setting various sysctl settings (e.g. to make sure ipv4 forwarding is disabled), mounting /proc with hidepid option. I think this matters on any distro.

There's no perfect guide. You should collect things that make sense to you, put them in git and apply to your servers via Ansible/SaltStack


👤 Aaronstotle
This all depends on what you are hardening against, some basic steps are to install fail2ban and limit SSH to your home/office IP.

You can disable and remove services you don't use and turn on auto-update and that will be good enough for day to day use in my opinion.


👤 closeparen
Is setting up a server by hand and following a checklist even the right frame for this question? I would imagine the orgs that are sophisticated about this have images, Puppet/Chef modules, etc. Might not even permit any manual tweaking.

👤 nuker
Selinux In enforcing mode, but you want RedHat family distro for that.

👤 blibble
firewall ssh to known bastion hosts, key only login, make sure nothing's listening unexpectedly

👤 egberts1
It depends

Do you want which of the two?

* performance

* security

* older hardware


👤 dimensionc132
https://www.timesys.com/security/securing-your-linux-configu...

You can also do it the easy way, instead of installing Ubuntu, which is based on Debian, install and Arch spin such as Arco Linux https://arcolinux.com/ and during the installation choose the 'hardened kernel' option instead of the default or zen kernel.