When you spin up a fresh public Linux instance, automated scanners start knocking on your doors within milliseconds.

It doesn’t take much to get locked down properly, but missing a few basics can leave you exposed.

I’ve put together a straightforward guide covering the essential hardening steps from UFW and SSH key-only auth to secure Docker bindings and automated maintenance.


Original content by The Unknown Universe, written by a human and not AI generated!

  • UnknownUniverse@lemmy.mlOP
    link
    fedilink
    arrow-up
    1
    ·
    36 minutes ago

    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.

  • TeaWithDani@lemmy.world
    link
    fedilink
    arrow-up
    9
    ·
    5 hours ago

    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.

  • bacon_pdp@lemmy.world
    link
    fedilink
    arrow-up
    4
    ·
    6 hours ago

    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.

  • talkingpumpkin@lemmy.world
    link
    fedilink
    arrow-up
    40
    ·
    13 hours ago

    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:

    Create a Non-Root Administrative User

    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.

    sudo usermod -aG docker your_username

    This is the same as being root. Yep, the very same thing you went that whole length to avoid :)

    Automate Maintenance with a System Update Script

    Look into unattended upgrades. Also… it’s not automation if you have to run a script manually :)

    • TeaWithDani@lemmy.world
      link
      fedilink
      arrow-up
      4
      ·
      5 hours ago

      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.

    • TrollAccount69@lemmy.ml
      link
      fedilink
      arrow-up
      10
      ·
      9 hours ago

      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.

    • iByteABit@lemmy.ml
      link
      fedilink
      arrow-up
      12
      arrow-down
      1
      ·
      13 hours ago

      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

      • underisk@lemmy.ml
        link
        fedilink
        arrow-up
        8
        ·
        10 hours ago

        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.

        • eldavi@lemmy.ml
          link
          fedilink
          English
          arrow-up
          2
          ·
          8 hours ago

          replacing or reimaging it automatically when you see something like this feels like overkill.

          • underisk@lemmy.ml
            link
            fedilink
            arrow-up
            3
            ·
            7 hours ago

            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.

  • CallMeAl (like Alan)@piefed.world
    link
    fedilink
    English
    arrow-up
    29
    ·
    15 hours ago

    sudo apt update && sudo apt upgrade -y
    should be
    sudo apt update && sudo apt full-upgrade -y

    also for

    echo “PASTE_YOUR_PUBLIC_KEY_HERE” | ssh your_username@your_server_ip “mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys”

    why not just use ssh-copy-id ?

    edit: added more thoughts

    • UnknownUniverse@lemmy.mlOP
      link
      fedilink
      arrow-up
      14
      ·
      15 hours ago

      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.

    • blueworld@piefed.world
      link
      fedilink
      English
      arrow-up
      3
      ·
      14 hours ago

      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.

      • CallMeAl (like Alan)@piefed.world
        link
        fedilink
        English
        arrow-up
        3
        ·
        14 hours ago

        I’ve tried integrating it into build scripts In a script I would also write out all the commands I want to run. After all, ssh-copy-id is a shell script itself. For instructions for someone to run interactively, I think ssh-copy-id makes the most sense.

  • LewdLemon@sh.itjust.works
    link
    fedilink
    arrow-up
    12
    ·
    14 hours ago

    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.)

  • blueworld@piefed.world
    link
    fedilink
    English
    arrow-up
    4
    ·
    14 hours ago

    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.