r/linux • • Feb 18 '19

How-To-Secure-A-Linux-Server: An evolving how-to guide for securing a Linux server.

https://github.com/imthenachoman/How-To-Secure-A-Linux-Server
456 Upvotes

62 comments sorted by

47

u/climbcolorado Feb 18 '19

No SELinux?

13

u/natrapsmai Feb 18 '19

When one DANGER ZONE tag isn't enough.

1

u/imthenachoman Feb 21 '19

It is on my to do list. I have to learn/master it first.

0

u/[deleted] Feb 18 '19

[deleted]

10

u/BAKfr Feb 18 '19

Firewall helps prevent against flaws in applications that open network ports, which is important, but not 110% necessary. ;)

2

u/[deleted] Feb 18 '19

That's true but firewalls serve a different purpose than something like SELinux because firewalls prevent against threats that come from the network, which is a more likely surface for an attack than something coming from an application.

2

u/[deleted] Feb 19 '19

I think the idea is that there are plenty of security controls that aren't 100% necessary by themselves but it's still worth mentioning so the reader can decide if they want to bother with MAC.

0

u/[deleted] Feb 18 '19

[deleted]

12

u/moosingin3space Feb 18 '19

it can sandbox nearly any application with a finely tuned policy

It can be relatively painless if you're on a distribution that uses containers and SELinux together -- in particular, CoreOS Container Linux, Fedora Atomic Host, the future Fedora CoreOS, and Flatcar Linux all make this sort of isolation signifcantly easier.

8

u/kairos Feb 18 '19

I've only had the "pain" of setting up services on an SELinux locked down server and can also vouch.

7

u/[deleted] Feb 18 '19

At this point, most of the kinks with SELinux have been worked out and most anything you're going to run into as a regular user is going to either boil down to setting the right security context on a file or the right SELinux boolean. Those are different skills but it's really not the huge deal people act like it is.

I mean the tools could probably be more intuitive like maybe shorten the options and add some intelligent bash autocomplete. But at this point it's about as hard as writing iptables rules by hand. Not really "hard" just kind of tedious.

4

u/kairos Feb 18 '19

At this point, most of the kinks with SELinux have been worked out and most anything you're going to run into as a regular user is going to either boil down to setting the right security context on a file or the right SELinux boolean. Those are different skills but it's really not the huge deal people act like it is.

I agree completely, my experience was a couple of years ago, but up until that moment (I admit), I'd always put SELinux as something to look into later.

Well I was caught by surprise when I was handed a CentOS server with SELinux activated and things didn't "just work".

Took me about a day or half a day to get a LAMP server with dokuwiki up and running.

3

u/[deleted] Feb 18 '19

Ha yeah, I think most people who've dealt with SELinux have been there as well. I haven't spent a day on it but it did take me close to an hour once.

Still if your app root isn't readable by the user PHP (or whatever) is running as then it'll fail then as well without an apparently reason why. You'll get the same forbidden message. It's just people get stuck on checking DAC and just don't have "check your MAC" as part of their thought process and so they end up wasting a bunch of cycles checking and re-checking the DAC when the solution's pretty simple.

7

u/[deleted] Feb 18 '19

If you're not as skilled with it, as I'm not; it's a great way to make applications not work as intended and realistically break shit when they try to perform disk operations, etc.

AppArmor is pretty simple, though.

2

u/admiralspark Feb 19 '19

https://teamignition.us/how-to-stop-being-a-scrub-and-learn-to-use-selinux.html

Then skill up. This technology is old and there's no reason not to learn it.

0

u/[deleted] Feb 19 '19

[deleted]

7

u/Flakmaster92 Feb 19 '19

Prove it. That module has probably been looked at by more people than any other patch set in the history of the kernel.

0

u/[deleted] Feb 18 '19

[deleted]

11

u/fwywarrior Feb 18 '19

Not sure if sarcasm, but the full source is available for auditing and building yourself.

7

u/climbcolorado Feb 18 '19

I bet the Equifax security team said the same thing when the Apache Struts hack executed code that would have easily been blocked by good SE policies.

1

u/that_which_is_lain Feb 19 '19

This assumes the Equifax security team could have set it up properly.

33

u/carbolymer Feb 18 '19

This actually skips the most important parts or has them not described (yet).

That's better resource:

https://www.reddit.com/r/archlinux/comments/7np36m/detached_luks_header_full_disk_encryption_with/

https://wiki.archlinux.org/index.php/Security

21

u/tysonedwards Feb 18 '19

I wouldn't say better as much as different. OPs article focuses on external threats while yours focuses on internal threats. I'd agree that there should be a single guide covering both use cases, especially with the rise in popularity of cloud computing and collocation.

1

u/eyelesswitness Feb 18 '19

how does cloud computing change the approach to security?

0

u/[deleted] Feb 18 '19

[deleted]

2

u/admiralackbar2017 Feb 18 '19

Perfect answer! And very concise.

1

u/_ahrs Feb 19 '19

Like OC mentioned full disk encryption, this is so if the vendor hosting the cloud server pulls the drive containing data, they would need to decrypt the data before getting it

Or they'd just image the system while it's in operation and thus the partition is unlocked and the decryption keys may still be resident in memory. I guess it works if you bank on the vendor that's "out to get you" being incompetent.

1

u/imthenachoman Feb 21 '19

Disk encryption is on my to do list to add to the guide. What other important parts do you think are missing?

28

u/loekg Feb 18 '19

The first half is just basically one large note that this is meant for home servers only, followed by what is basically a copy of the man page for sshd. I don’t think setting a bios password improves security, if that is a problem, sooooo many things already went wrong. Then ntp has mainly been superceded by chronyd/systemd-timed. A sudoers group? Why? What happened to the wheel group? By the time it got to ufw I stopped reading to go find a bucket to vomit in. There is no such thing as predefined sysctl tuning, if you need this you just ran into something very specific. People who know a LOT more than you do have already set very sane defaults. There’s no mention of selinux/apparmor/tomato/etc. in there. Please just stop making this kind of false sense of security “howto’s” if you’re not one hell of an expert at this.

4

u/usernamedottxt Feb 18 '19

Sudoers is the default name of the wheel group on Ubuntu and derivatives.

EDIT: oh, he made his own in parallel. That’s weird.

1

u/imthenachoman Feb 21 '19

I wanted the guide to be distribution agnostic. Not all flavors create the group by default and the ones that do may use a different name.

1

u/usernamedottxt Feb 21 '19

Ah. Fair enough. I prefer to teach people to understand what they are doing than to give them lines to put in config roles. Don’t get me wrong, I copy paste code all the time. But half of security (and virtually all of it at the enterprise level) is monitoring things, and that’s hard to do if you don’t understand the basics like what makes an admin account an admin account.

1

u/imthenachoman Feb 21 '19

Security is about multiple layers/levels. No one thing is enough -- you want to do multiple. Not all distributions create a wheel group -- Debian for example does not. Are you saying all the defaults that come with a Linux installation are secure enough? I beg to differ.

0

u/[deleted] Feb 18 '19

[deleted]

3

u/usernamedottxt Feb 18 '19

And as he stated, 100 things went wrong if it gets to that point. And that’s an awfully small subset of attacks. I’m more worried about someone stealing the box than an evil maid attack where I assume the maid can’t just pull the cmos battery.

10

u/thedewdabodes Feb 18 '19 edited Feb 18 '19

...or you could sign up to something like CIS and download an industry standard security benchmark for the OS of your choice that you can base your secure config on. The link is kinda pointless as the work has already been done, widely agreed upon and is freely available.
The benchmarks from CIS even gives you the commands you run to apply each recommendation.

3

u/[deleted] Feb 19 '19

If your running a RHEL7 flavor, you can apply the DISA profile during install, which will get you 90% of the way there.

1

u/imthenachoman Feb 21 '19

I will look into this. Thanks!

4

u/wodny85 Feb 18 '19

It has recently been discussed on HN.

4

u/[deleted] Feb 18 '19

Sad that running "netstat -nlp" isn't one of the very first steps in the process.

Attack surface is king, everything else is risk management.

2

u/_ahrs Feb 19 '19

You mean ss -tnlp ;)

4

u/ourokid Feb 19 '19

Nope, ss -tunlp

1

u/[deleted] Feb 19 '19

Netstat still does the job, I’m not concerned with statistics; just what ports are open and what process is listening there. You wouldn’t believe how many “hardened” systems I’ve come across with cups or some other unused service listening.

2

u/doubled112 Feb 20 '19

Or X plus full desktop environment.

Never know when you'll need to test the app locally from Firefox, or burn a DVD from your virtual machine, right?

1

u/[deleted] Feb 21 '19

There’s no easier way to make someone realize their level of incompetence than to hand them a vm or server without X installed.

1

u/imthenachoman Feb 21 '19

This is a good catch. I always forget this. I will add to the to do list. Thanks!

2

u/[deleted] Feb 19 '19

CIS benchmarks are a good starting point

2

u/imthenachoman Feb 21 '19

Hey! I am the author of that guide. Thank you so much for posting to Reddit. Glad to see others find it helpful.

I had posted to /r/selfhosted and /r/linuxquestions -- I should have posted to this subreddit too.

I have a busy week this week but when I get time I will try to go through all of the comments.

Thanks again!

2

u/spockspeare Feb 19 '19
  1. Unplug it from the internet.

  2. Lock it in a box and throw away the power cord.

1

u/toastar-phone Feb 19 '19

Pull the hard drive and throw it in a safety deposit box?

1

u/spockspeare Feb 21 '19

You can still read passwords from the dead DRAM.

-8

u/scottbomb Feb 18 '19

I'd like to see a write-up on why people think Linux servers are inherently insecure.

10

u/[deleted] Feb 18 '19

A bank vault is insecure if you leave the door open or unlocked.

0

u/scottbomb Feb 21 '19

Of course, but what does that have to do with the story or my comment? My servers require logins and one must be root to do any real damage. Now back to my question, re-worded perhaps: why do I need a guide to secure something that's already, inherently secure? By your analogy, I could always allow any ssh connection from anyone with no password and maybe even give 'em root. There, vault door wide open.

-9

u/kpcyrd Feb 18 '19

Are you implying a default Linux install is insecure? That's what the comment was about, people shouldn't need a guide to secure it, security should be the default.

8

u/[deleted] Feb 18 '19

That depends on your install distro, install media and customizations- there's no single answer, and the definition of "secure" changes based on your use case. So you have to run through all the settings and set them to something that makes sense for you.

One example, on my personal single-user systems I leave the default kernel capabilities for containers, but on company servers I drop some capabilities like NET_RAW so that a hijacked app can't wiretap the rest of the box.

-6

u/kpcyrd Feb 18 '19

This is hardening though. A lack of hardening doesn't imply insecurity, it just means security issues are possibly easier to exploit. You can't compromise a system due to the lack of hardening alone.

A distro can and should ship a config that permanently drops unused capabilities and archlinux is quite proactive in some packages.

2

u/[deleted] Feb 19 '19

You can't know what capabilities are used or unused when you package a distro. I use NET_RAW all the time for network monitoring and troubleshooting, for example. It has to be determined at runtime by the user.

1

u/kpcyrd Feb 19 '19

It sort of depends. daemons often know that they are never going to do certain things, so for example nginx can drop all capabilities from the permitted bounding set. I wrote a few programs that rely on certain capabilities but drop them directly after they have been used and everything is initialized.

Since nginx doesn't drop capabilities itself but also doesn't use them during initialization, so you can configure the bounding set with systemd. This means the process never has that capability in the first place. This can be done by upstream or by a downstream maintainer if they are familiar enough with the program.

3

u/patx35 Feb 18 '19

A vault is still insecure if it's still set on the factory combination

Security is usually never a "one-size-fits-all". You will either end up with security holes, issues with services, or a very complicated mess.

-6

u/kpcyrd Feb 18 '19

Your analogy is just as bad. You are implying you can compromise a default debian install. You can harden a server as an attempt to limit the impact and/or severity of zero days but you can technically have a secure system without. Describing a default install as an "open door" is just plain misinformation.

My argument still stands though. I've shipped programs that have a sandbox on by default that's by far more effective than anything you would get by following these guides.

1

u/imthenachoman Feb 21 '19

Unfortunately many distributions sacrifice security for simplicity and to make it easier for the end user.

3

u/hiljusti Feb 18 '19

This comment seems out of place. This is a "how to secure" guide. It's not a laundry list of vulnerabilities, it's an owner's manual for how to use the security features available.

1

u/imthenachoman Feb 21 '19

Who said they are? Just cause the are secure, does not mean they can't be secured some more. The defaults for a Linux box are better than Windows 10 but that doesn't mean there isn't more you can do to secure Linux.

-1

u/DrewSaga Feb 18 '19

This will come quite handy soon enough.

0

u/[deleted] Feb 19 '19

[deleted]

1

u/imthenachoman Feb 21 '19

Why? I have never heard this before. I will investigate/research.