There is one night I still remember clearly. Two in the morning, my phone went off. Not an alarm, a monitoring notification: CPU on the server had climbed to one hundred percent and would not come down. I got up, opened the laptop with my eyes half shut, and logged into the server.
There was a process I did not recognise, running under a name that looked almost like a system process, eating the whole machine. No hacker breaking in like in a film. Just one account using the same password it had since the server was set up three years earlier, and an SSH port open to the entire internet.
That night I learned something no tutorial had taught me: servers are usually not attacked by very clever people. Servers usually get owned because one door was left unlocked, and that door gets opened by bots that walk the internet all day long.
Since then my view of security has changed. It is no longer about which tools are installed, but about which habits I repeat every week.
The short answer
If someone asks how to keep a server safe from hackers and malware, the honest answer is: most of the work is boring.
Closing doors nobody needs. Updating the system before someone else finds the hole. Making passwords and keys that cannot be guessed. Keeping a copy of the data somewhere else and actually trying to restore it. Reading logs once in a while, so you know what “normal” looks like.
That is seventy percent of your defence. Only then come the extra tools: firewalls, intrusion detection, malware scanners, and so on. All of them are useful, but if the door is still open, those tools are just alarms that ring after the goods are gone.
I should also clear one thing up. On the Linux servers I look after, a “virus” in the sense of a file that spreads from computer to computer is rare. What is common is malware that slips in through outdated software or leaked credentials, then quietly mines crypto, sends spam, or joins a botnet. The effect is just as unpleasant, and the prevention is the same.
The first thing I do: lock the SSH door
SSH is the main door into a server. Almost every automated attack I see in the logs tries to come in through it. So this is where I start.
Turn off root login. If a bot guesses the root password, the game is over in one attempt. I always create a normal user, then grant sudo access only when needed.
Use keys, not passwords. Passwords can be guessed, keys cannot. I generate a key pair, put the public key on the server, then disable password authentication entirely. Since then, failed login attempts in the log dropped from thousands a day to almost none.
Limit who may enter. I use AllowUsers in the SSH configuration so only specific accounts can log in. It looks trivial, but it closes the gap left by old accounts that are no longer used yet still alive.
Install an automatic lock. fail2ban reads the logs, and if one IP address fails to log in several times, that address gets blocked for a while. It does not stop a targeted attack, but it is enough to make a bot turn around and look for an easier server.
If the server has a fixed IP and I am the only one managing it, I also restrict the SSH port to my office and home IPs. But that carries risk: if my IP changes and I have no other access, I lock myself out. So I only do it when there is emergency access through the provider’s panel.
Updating the system, even though it is boring
This is the part most often postponed, and the part that most often causes incidents.
Picture a library used by millions of servers. One day someone finds a hole in it. The hole gets published, sometimes with example code showing how to exploit it. Within hours, bots around the world start scanning the internet for servers still running the old version.
As long as your server has not been updated, you are on that list.
My habit is simple: security updates run automatically, and I check the results every week. For packages that might change behaviour, I postpone them to a dedicated window and test on another server first. The point is not “always update without thinking”, it is “no security update sits unresolved for months”.
I also keep a note of runtime and database versions that are past their support window. This is the one that gets missed: the app runs perfectly well, but the database version stopped receiving security fixes a year ago. As long as nobody knows, nobody fixes it.
Firewall: open only the doors in use
The principle is one sentence: if nobody needs it, close it.
A web server usually only needs ports 80 and 443 open to the public. The SSH port is restricted. Database ports, Redis, admin panels and other internal ports should never face the internet.
I once found a database exposed to the public because “it makes it easy to connect from my laptop”. The port was not the default one, so it felt safe. Internet scanners do not care whether a port is standard or not, they scan all of them.
What I do now: every internal port is reachable only through a private network or an SSH tunnel. If I need a GUI for the database, I open a tunnel from my laptop, not a port to the world.
Secrets must never be committed
This is the mistake I see most often, and one I have made myself.
API keys, database passwords, tokens, all placed in a configuration file, and that file goes into Git. Deleted in the next commit, but still in the history. And if the repository is public, or ever was, assume every one of those keys has leaked.
My rule now: secrets live only in environment variables or a file that never enters the repository, and the example file contains only variable names with no values. If a key is ever committed by accident, I do not delete the commit and relax. I rotate the key immediately, because that is the only step that actually solves the problem.
I also check application logs. Sometimes we print a full request without realising it, including authorisation headers and passwords. A log kept for six months is exactly as leaked as an open configuration file.
The application itself is the biggest door
A tidy firewall and a hardened SSH do not help much if the application can be told to do anything.
Three things that most often become the way in:
Queries built from user text. If any part of the code joins user input straight into a database query, there is a hole there. The fix is not filtering odd characters, it is using bound parameters, so input is treated as data and never as a command.
Unlimited file uploads. Files uploaded by users should not be executable, should be stored outside the application folder, and their names should not be trusted. I have seen upload folders that could execute files, which is the same as installing a back door.
Dependencies nobody checks. Modern applications pull hundreds of packages from other people. If one of them is compromised, malicious code comes in without you writing a single line. I use an automated dependency scanner, and most importantly: I do not add a new package just because it is trending.
On top of that, I limit how many times someone can attempt a login, and separate permissions between users. An account that only needs to read data does not get delete rights. If that account leaks, the damage is contained.
Know what “normal” looks like, so you notice what is not
This is the part I used to skip. I installed monitoring, but never read it. As a result, when something happened, I had nothing to compare against.
Now I keep the basics: CPU and memory graphs, the list of running processes, who has logged in, and changes to important configuration files. Not because I enjoy looking at graphs, but because I want to know that at three in the morning CPU is usually ten percent. So if it is suddenly eighty, I know something is up.
I check the SSH log briefly every week. What I look for is not sophisticated attacks, just odd things: a successful login from a country I have never visited, an account I did not create, or a cron job I did not schedule. Most malware persists by installing a cron job or a service so it comes back after a restart.
A backup you have never restored is not a backup
This is the sentence I repeat to anyone who will listen.
The most damaging attack is not the one that steals data, it is the one that deletes it. Once an attacker is inside, they often delete backups reachable from the same server. So the copy must live somewhere else, and ideally cannot be deleted from that server.
I store daily backups in separate storage, keep them for a few weeks, and occasionally actually restore one database onto a test server. At first it felt like wasted time, until one day I needed it and discovered the backup files had been broken for a long time. Since then, a restore test is part of my monthly routine, not a plan.
Table: common attacks and the first line of defence
| What happens | How it gets in | First line of defence |
|---|---|---|
| SSH password guessing | Bots try thousands of combinations | SSH keys, no root login, fail2ban |
| Outdated software exploited | Old version with a public hole | Automatic and scheduled security updates |
| Database or Redis exposed | Internal port opened to the internet | Firewall, private network, SSH tunnel |
| Key leaked from a repository | Secrets committed to Git | Environment variables, rotate the key at once |
| Injection in the application | User input joined into a query | Bound parameters, validation on the server |
| Crypto-mining malware | Entry through credentials or a hole | Watch processes and CPU, limit privileges |
| Data deleted or encrypted | Full access from a leaked account | Off-site backups, tested restore |
This table is not exhaustive. But if you are just starting, doing the right-hand column already closes most of the cases I have actually run into.
What no tool can fix
There is one thing harder than configuration: human habits.
A password reused across five services. An SSH key shared in a group chat. An old server still alive because “you never know, it might still be used”, with nobody updating it. An ex-employee’s account never revoked. None of this is caught by any scanner, because technically nothing is broken.
What helps me is a simple list: which servers I look after, who has access, and when each was last updated. The list is ugly and often out of date, but it is far more useful than a sense of safety with nothing behind it.
And one more: do not wait for an incident to start. That two-in-the-morning night taught me that panic does not fix configuration. What fixes it is the small habits already done, back when everything was still quiet.
Closing
Keeping a server safe does not feel like heroic work. Most of the time goes into things nobody sees: running updates, glancing at logs, making sure backups can be restored, closing ports that are not used.
But that is exactly where the work is. An attacker only needs one unlocked door, and they try every day without tiring. We only need to make sure the door is locked, and to know when someone is trying the handle.
If you are just starting to manage a server, do not begin with a long list of tools. Start with three things: lock down SSH, update the system, and make a backup you can actually restore. Those three already make your server much harder to enter than most servers on the internet. The rest can follow. 🔐
Security Is a Habit, Not a Tool
No server is truly safe. There are only servers that are annoying enough to break into, and prepared enough for the day someone does. Choose the second, then maintain it every week.
Comments
comments powered by Disqus