How to completely secure an Ubuntu server

Search for a command to run...

I recently had to set up an Ubuntu 24.04 server and was struggling with managing permissions and installing MySQL. Vultr's guides came to the rescue! Their tutorial on how to use sudo on a cloud server helped me manage permissions securely, and their guide on installing MySQL on Ubuntu 24.04 made the database setup process a breeze. Highly recommend these resources for anyone setting up a cloud server!
Introduction I run a large WhatsApp group and I often need to mention everyone in the group. But there are no real solutions for mentioning all members. WhatsApp does not natively do it, and there was a Firefox browser extension, but it was abandoned...

Introduction This past week I was toying around with the HTML5 canvas element, trying to join two images together. At first, it seemed fine, but when I tried to reload the website, it was a mess. One image would load, but the other wouldn't. Investig...

Introduction I have worked on projects that required the production and testing database to be the same. This could be because some features work in MariaDB, but not in SQLite. Or some bugs appear in MySQL, but not in PostgreSQL. When you're working ...

Introduction I'm going to explain how to build a free plan for your Laravel Spark Next application. I will be using Paddle as the payment gateway, but the steps are almost identical with Stripe. Best of all? No credit card required for the free plan....

Introduction Sometimes you end up deploying an application to Dokku and then realize that you want to revert the changes you made. In this tutorial we'll go over how to roll back a Dokku deployment. Before we start Preface Keep in mind that rolling ...

This tutorial will help you to set up a secure Ubuntu server from scratch.
Keep in mind that this is not a common tutorial, this is the culmination of all the knowledge I have gathered from managing my own servers for more than three years.
While this tutorial is focused on Ubuntu 20.04, it can be used for many other versions, like 18.04 and 16.04. As they are very similar.
It doesn't matter if your server is hosted on DigitalOcean, Google Cloud Engine or Amazon Web Services, Ubuntu should be the same.
If you don't have a server you might want to look at the Useful resources step.
The first and probably most important step is to always keep the system up-to-date. To do so just open the terminal to update and upgrade the packages via apt.
sudo apt update # Update package information
sudo apt full-upgrade -y # Upgrade packages
sudo apt autoremove -y # Remove unnecessary packages
# One liner
sudo apt update && sudo apt dist-upgrade -y && sudo apt autoremove -y
Now that the packages are updated, we should install an automated solution to keep the system always up-to-date.
This tutorial on Linuxize will help you install and configure the unattended-upgrades package, which is exactly what is needed.
Using the default super user root is always bad practice, it does everything with the maximum level of permissions, allowing you to break anything; and more critically... Access to anything on the system.
Instead, we should use a normal user with super user privileges. This tutorial on DigitalOcean will guide you to do that.
Now that you have a new user with super user privileges, you might want to SSH in your server with it, but might find that you can't.
This is because the credentials were stored on the user you were using before, most likely root. Just SSH again with the previous user and copy the credentials to the new user with the rsync utility package.
Follow the 5th step of this tutorial on DigitalOcean to do so.
SSHD manages the SSH connections to the server. Its default configuration is good but some changes must be made, like disabling the root user login and changing the default SSH port.
Follow the first step of this tutorial on DigitalOcean to learn how to configure SSHD.
It is recommended that you change the default
SSHport
UFW is Ubuntu's default firewall and is extremely useful. By default it allows http and ssh connections, depending of your use case you might not need some of those rules.
Check out this tutorial on DigitalOcean to learn how to configure UFW.
If you changed the
SSHport on an earlier step, you might want to create a new UFW rule for that port.
Fail2Ban protects you from brute-force attacks. It bans bad actors from accessing the server for a specified quantity of time.
Learn how to install and configure Fail2Ban with this tutorial on Linuxize.
These are some quick specific guides that you should keep in mind if you use any of this software.
Nginx has various configuration files, its defaults are pretty good but you might want to take a look at it.
Use this tutorial on Acunetix as a starting point.
There is also this pretty nifty tool by DigitalOcean that allows you to configure Nginx in a visual manner. It includes popular presets, for example for NodeJS and PHP applications.
Apache might require more work, as its defaults leak some information about your system.
Start your configuration journey with this tutorial by Tecmint.
I have used MySQL and MariaDB on the past, by default their ports are opened externally, that shouldn't be allowed, as it is a security risk.
The database should only be allowed from local connections; or if ran externally, by whitelisted IPs.
Now that you have your own secure infrastructure, you might want to create applications and services.
Dokku is perfect for that. It allows you to containerize, build and run your applications with a simple git push.
Check out my own tutorial to learn how to set up and use Dokku.
There are some parts that you might want to skip, as they are similar to this tutorial.
If you have found this tutorial useful then you should follow me, I will be posting more interesting content! :')
Or support me financially. <3
Thanks to