Locking down a fresh Linux server: What you actually need to do before bots find it
Locking down a fresh Linux server: What you actually need to do before bots find it
Linked article
unknownuniverse.uk ↗
Showing a saved copy: the original couldn't be reached just now. Last updated 11 days ago.
Adding a user to the docker group is equivalent with granting it passwordless root privileges, never do that unless you know precisely why and understand the risks involved.
On the positive side, forbidding password-based auth, installing and using a firewall and locking down the root account is good advice
Getting whatever you can (likely most, or all) into rootless podman, preferably with SELinux is worthwhile. Docker is a security nightmare.
I have a whole bunch of docker compose declared containers running on my server how much of an investment do you think it’d be to podman? I don’t like how insecure docker is but it seems like a pain to move.
Not going to lie, there is pain, but maintenance is much nicer. I've gotten to the point where I can just convert docker-compose to quadlet by eye, the syntax is familiar if you know systemd.
https://github.com/containers/podlet can be pointed at a docker-compose and spits out quadlets. It's been a while but last I used it, it would get it right 80%+ of the time on complex things and get you 85%+ there when it failed. There's also podman-compose which is a crutch but can establish compatibility.
/usr/libexec/podman/quadlet -dryrun -useris a useful diagnostic when conversions break.Sometimes I feel like a poser Linux user because I don’t know systemd at all lol. I guess it’s probably because it’s technically my secondary os. But yeah I’ll look into changing stuff this weekend. I’ve been having issues with tangled, and I want to self hosting my password manager so it’s a good time to just put in a bunch of effort into everything. I’m hoping that podman will also be a bit more ergonomic.
There's a bunch of examples here https://codeberg.org/herzenschein/herz-quadlet that may help.
If you're thinking bitwarden check up on their recent enshittification tendencies. More happy than ever with KeepassXC and syncthing myself, but you may need more features than me.
Honestly the issue is docker was never designed to be a security boundary. Docker is always from the beginning a way to deploy application without conflicting shared library requirements. Looser than isolation of VM, but also not as hard as every other existing solution at that time (dedicated chroot, static linking, appimage, flatpak)
Yeah makes sense. I originally just went with docker because I didn’t want to bother to learn new stuff since spending hours solving issues at work made me less inclined to want to do the same at home when I just want to actual maybe work on “fun” stuff. Same reason I still use nginx instead of caddy. But I have seen increasingly more projects that use podman instead of docker so maybe I can still be lazy ish with my self hosting and get some security.
That's the fun part. Podman skills transfer better to kubernetes if that's your end goal. Docker compose has a different format than what a kubernetes YAML expect. So you do get a bit safer container model AND devops skills
I would add changing the SSH port to one that isn't 22. This isn't any more secure, but does tamp down on automated scanner traffic.
You can also add endlesssh behind port 22 to slow down attackers, and of course install fail2ban
Thanks for sharing, never heard of endlesssh and tonight ima gonna reopen port 22 :)
Isn't adding a key more than enough?
keys are more than enough for security but keya doesn't reduce automated scanner traffic at all. for each automated scan, the server will open a connection and attempt to authenticate the client which consumes resources and spams logs. When I switched to a different port, my automated scanner traffic dropped to essentially zero.
Gotcha! Yeah, I never left default ports at my last job and my only remaining personal server is on 22, but it's under zero load.
Bookmarked 🥰
Is router side firewall (yes my home server is just a laptop on WiFi😔) only exposing HTTP/HTTPS ports connected to Caddy enough?
I'll have to find the post again (it was somewhere here on Lemmy) but UFW does not apply to docker containers.
Docker bypasses UFW because it uses iptables and UFW doesn't support firewall zones.
Any docker/podman ports not bound to localhost will be publicly accessible even if you have a block rule on UFW.
There are some wacky script workarounds but the real solution is to install & use a proper firewall like firewalld (firewall-cmd command) and not the joke utilities of Canonical/Ubuntu (ufw, netplan, etc).
At which point you might as well chuck Ubuntu into the trash since you'd be replacing a bunch if its defaults anyway, and just stick with Debian or another quality stable distro like OpenSUSE.
Wouldn't docker do the same with firewalld?
It acknowledges that it exists and just still uses it's own rules. You'd still have to configure it, configuration is just less stupid than ufw with docker.
My question is, who thought this was acceptable behavior at docker?
Is firewalld more intuitive than nftables.
I’ve been bitten by this before.
OP, this guide reeks of inexperience. It’s admirable that you’re trying to help, but there are footguns in just about every section of the post.
stares motherfuckerly
Turn off ipv4. Many bots do not scan ipv6.
Setup a dummy SSH daemon, instant black hole for a random span of 2~6 weeks for any IP that attempts to login as root.
I really appreciate the effort but... how to say it?... it might be useful to make sure you understand a topic before trying to teach it to others :)
Some notes:
Not as important as most people think (local privilege escalation is a thing, and it's also quite easy for your user to alias sudo to something that will capture your password the next time you type it).
Basically: if an hostile actor gets a shell on your server, you are fucked anyway.
This is the same as being root. Yep, the very same thing you went that whole length to avoid :)
Look into unattended upgrades. Also... it's not automation if you have to run a script manually :)
I keep telling EVERYONE this, and we continue to get "findings" around privilege escalation.
Not today, satan.
Passwordless root allows you to require another factor of authentication on the off chance you didn’t set up key only ssh login like the top of the guide says. That second factor is the administrator account name. Now you can make this as generic as you want but assuming your non root admin account isn’t named “administrator” or “admin” then you just increased the security of your system.
Suggesting that because privilege escalation exists there is no difference between a user and root is absurd on the face of it. Privesc requires vulnerable versions of vulnerable packages with the specific circumstances present that allow pre and post exploitation actions. In case you were operating under the assumption that most privesc goes from local administrator to root, usually you’re going from some relatively unprivileged user account that handles a service up to root and that’s harder than just invoking sudo.
You would still want to roll over your credentials and restore from backups but “safe cracking tools exist so just leave the cash register deposits in plain view on a shelf behind the desk” isn’t a strong argument for password root.
E: @talkingpumpkin@lemmy.world correctly pointed out that a nonstandard admin account name isn’t a second factor. It’s more like an additional secret.
A "secret" administrator account name is not a second authentication factor: it's just another "something you know", basically the same as using a slightly longer password.
On top of that, account names are generally not treated with the same confidentiality passwords or private keys are treated with: while you can trust that software and collaborators will not leak a password or private key, you cannot make the same assumption about usernames.
Yeah that was a bad choice of words on my part.
Consider though that everything I handle with a public facing ip gets 69 million attempts to login as “root”, “admin”, “docker” and everything else under the sun but not even once has someone tried to login as my admin user. It’s acting as more than a slightly longer password.
It's nice that your system has layers, but when the cutworms get on your onion they can still spoil the whole thing.
Sure, onions can go all the way bad. I’d rather put my special thing inside an onion than an apple.
Exactly, you shouldnt add your user to Docker and you should run your Docker containers as your user or another limited user rather than the default root.
Be very selective with whether you expose your docker socket to any container. I just give Beszel Read Only. Gives me container perfs and flags updates.
For the update script, you can just make it a cron job. Id add that to the guide. A daily update and weekly backup cron job is my setup.
Use podman instead, it doesn't need root. And putting the docker socket in ro mode doesn't help at all, because ro is for files, while this here is a connection.
Yeah, I'm evaluating moving to it, or just running everything baremetal or in jails from FreeBSD. Slowly moving away from Linux in general.
Of course privilege escalation exists, but why roll out the red carpet on an attacker that managed to get shell access? Maybe in the time that they take to figure out that privilege escalation you have the time to detect the attack and defend against them
Most shell users have enough privilege to read (exfiltrate) most data-value from a server, and they almost always have the capacity to damage it beyond functional operation.
If someone put a script on your server that tried to do privilege escalation you should just assume it was successful and act accordingly. It doesn’t really change what should happen.
replacing or reimaging it automatically when you see something like this feels like overkill.
Only if you haven't built the infrastructure to make that trivial, which you should have. If you have any data worth securing it's not even close to overkill.
I like write filters -> mostly immutable systems. SHTF? reboot ASAP.
No root but then they use sudo? That's going out from the frying pan into the fire.
sudo is a great tool, for people who play nicely.
It's like a lock on a glass door, with a rock garden outside.
I recommend self hosting something like a NTFY server to send you texts if certain events happen, like someone logging in. You can also log those events on a different server.
also for
why not just use
ssh-copy-id?edit: added more thoughts
You're completely right on both counts, full-upgrade handles dependency shifts properly without leaving packages held back, and ssh-copy-id is much cleaner. Updated the guide to reflect both.
I love ssh-copy-id, but end up doing it manually about half the time anyway. I've tried integrating it into build scripts and find what the op did more reliable.
Could just be me though... it is Linux, and there a dozen ways to skin Linus. We all have our assholes and opinions.
Editor's Note & Update
Following some comments on this guide, a couple of key clarifications are worth adding:
Docker Permissions & Rootless Containers: A few eagle-eyed readers rightly pointed out that adding your user to the docker group is functionally equivalent to giving yourself root-level access to the host machine (since container runtimes run with root privileges).
I have updated to remove that shortcut. While container security can go much deeper (such as setting up rootless Docker or Podman), this guide focuses on keeping things simple as a solid quick-start baseline for security.
System Updates, while fully automated background upgrades have their place, many admins including myself prefer a hands-on approach. Running a script manually and using needrestart gives you deliberate control over when services are recycled and ensures you aren't caught off guard by unexpected reboots.
Thanks you all for reading and I always welcome any feedback on my work.
I'm going to take exception right here:
"Install Essential Administrative Tools"
The first thing you do to lock down a server is: Uninstall everything that is non-essential.
Do you need any wireless access to the server? If not, remove all WiFi, Bluetooth, and other wireless support from the kernel (block the modules).
Do you need anyone to be able to plug in a USB dongle that can bridge to ethernet? Or anything else?
Remove support for everything you don't need first, un-necessary attack surface is the first foot-gun.
Are these administrative tools all essential? Keep / install what you need, sudo apt purge the rest.
Do you know WTF you're doing with Docker containers and firewalls / iptables? The thing to do is learn how that really works, before getting your application servers / containers up on the open internet.
Do you know what un-necessary attack surfaces exist within your container images? That's another step toward a hardened system - know what you're running, know what that means, and
above all else: run as little as possible.
More Docker hardening*
Consider rootless docker or Podman. If that is not for you:
* Note that these tips are less "fire-and-forget, sane defaults" and can be much more finicky. Whether they work well depends on the service in question. They may break container deployments and are often a matter of trial and error until you get them just right. (Feel free to hit me up on details; if I happen to be running the same services as you are, I may be able to tell you what works and what doesn't.)
You missed the first step: port knocking.
If there is no way to connect without having a cert or port sequence; then only the real threats show up in your logs.
Good guide as a base. Few thought...
I'd use unattended-upgrades instead of the script. A long know mehod and does similar, and the script doesn't reboot for a kernel CVE. I'd pair it alongside out-of-band console and an uptime check.
Fail2ban needs rsyslog enabled (or proper logging) or it silently reads nothing.
And lastly Journald needs retention set, or you can loose logs in reboots.
Also I'm not sure if 'core' is enough anymore for a public operating and accessed server, but it's absolutely vital as a start.