Secure Your Cloud Server: Tips and Best Practices for Effective Security

·Server Pundits Engineering

  • Security
  • Linux
  • Cloud Servers
  • SSH
  • AWS

Cloud servers run most modern businesses, and they are under constant attack. A new Linux VM with a public IP address usually sees automated SSH login attempts within minutes of booting. The good news is that most breaches still come from a short list of avoidable mistakes: exposed SSH, weak credentials, missing patches, overly broad cloud permissions and backups that do not work.

This guide covers 12 practical steps to secure a cloud-based Linux server, with commands for Ubuntu and Debian. Most of them apply to any distribution and any cloud provider.

1. Use SSH keys and disable password logins

Passwords can be guessed, reused or phished. SSH keys cannot be brute-forced in any practical sense: the private key stays on your computer, and only the public key goes on the server. If you are on Windows, see our guide to generating an SSH key in Windows.

Once key login works, turn off password and root logins. Create a drop-in file such as /etc/ssh/sshd_config.d/01-hardening.conf:

PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no

Then validate the configuration and reload SSH:

sudo sshd -t && sudo systemctl reload ssh

Two things to watch:

  • File name order matters. For most settings OpenSSH uses the first value it reads, and it reads drop-in files in alphabetical order. Ubuntu cloud images ship a 60-cloudimg-settings.conf, so a file named 01-... wins and a file named 99-... may be ignored.
  • Do not lock yourself out. Keep your current session open and test a new login in a second terminal before disconnecting.

2. Do not expose SSH to the internet at all

The most effective way to stop SSH attacks is to give attackers nothing to connect to. Good options:

  • AWS Systems Manager Session Manager: shell access with no inbound port 22, controlled by IAM, and sessions can be logged to CloudWatch and S3. See our guide to tracking session activity with AWS Systems Manager.
  • A VPN: connect to your private network first, then SSH to private IP addresses. See our site-to-site VPN guide.
  • A bastion host: a single hardened jump server, reachable only from your office or VPN.

If port 22 must stay open, restrict it to known IP addresses rather than 0.0.0.0/0.

Warning: only allowlist an IP address that is static. Most home connections, and many office ones, have a dynamic public IP that your internet provider can change at any time. When it changes, the firewall will block you and you will be locked out of SSH. If your IP is dynamic, use Session Manager or a VPN with a fixed exit IP instead. Either way, know your break-glass access before you need it, such as the EC2 Serial Console or the DigitalOcean Droplet Console.

3. Use a firewall at two layers

Use your cloud provider's firewall, such as AWS security groups or DigitalOcean Cloud Firewalls, as the first layer, and a host firewall as the second. Both should deny everything by default and allow only what the server needs.

On Ubuntu, ufw is the simplest host firewall:

sudo ufw default deny incoming
sudo ufw allow from 203.0.113.10 to any port 22 proto tcp
sudo ufw allow 443/tcp
sudo ufw enable

Replace 203.0.113.10 with your own static IP address or your VPN's address range. As covered above, do not use this rule with a dynamic IP: when your address changes, you will be locked out.

Watch out for Docker: ports published with docker run -p bypass ufw, because Docker writes its own iptables rules. On Docker hosts, rely on the cloud firewall, or bind containers to 127.0.0.1 and put a reverse proxy in front.

4. Patch automatically, and restart what you patched

Most attacks exploit known vulnerabilities that already have fixes. Turn on automatic security updates:

sudo apt install unattended-upgrades
sudo dpkg-reconfigure -plow unattended-upgrades

A patched package does nothing for a process still running the old code. On Ubuntu, needrestart lists services that need restarting after updates, and the file /var/run/reboot-required tells you when a kernel update needs a reboot. Plan regular maintenance windows for both.

For major upgrades, test on a staging server first. New package versions occasionally break applications that depend on older behaviour.

5. Patch your applications and runtimes too

apt only covers operating system packages. Your Java, Node.js, Python and PHP runtimes, your container base images and your application dependencies all need patching separately, and vendor software often bundles its own runtime.

Java4Shell (CVE-2026-114938) is a recent example: a critical flaw in Java itself that OS updates alone did not fix on many servers. Keep an inventory of what runs where, so you can answer "are we affected?" in minutes when the next critical CVE lands.

6. Lock down your cloud account

Securing a server means little if someone can take over the cloud account it runs in.

  • Enable MFA on every console user, and on the root or owner account above all. Use a hardware key or an authenticator app.
  • Use the root account only for the few tasks that require it.
  • Grant least privilege: give people and services only the permissions they need.
  • Use IAM roles, not access keys, on servers. An EC2 instance role provides short-lived credentials automatically, with no long-lived keys on disk to leak.

7. Require IMDSv2 on EC2

The EC2 instance metadata service hands out the instance role's credentials. Its original version (IMDSv1) can be abused through server-side request forgery (SSRF) bugs in your applications. IMDSv2 requires a session token, which blocks most of these attacks:

aws ec2 modify-instance-metadata-options \
  --instance-id i-0123456789abcdef0 \
  --http-tokens required --http-endpoint enabled

Make it the default for new instances in your launch templates.

8. Keep secrets out of code and config files

Database passwords, API keys and tokens do not belong in Git repositories, Docker images or .env files copied between servers. Use a secrets manager, such as AWS Secrets Manager, Parameter Store or your platform's equivalent, and grant access through IAM roles. For Kubernetes, see our guide to integrating AWS Secrets Manager with EKS.

9. Block brute-force attempts with fail2ban

If any login service faces the internet, fail2ban bans IP addresses that fail repeatedly:

sudo apt install fail2ban

Create /etc/fail2ban/jail.local:

[sshd]
enabled = true
maxretry = 5
findtime = 10m
bantime = 1h
ignoreip = 127.0.0.1/8 203.0.113.10

Then restart it with sudo systemctl restart fail2ban. Adding your own static IP to ignoreip stops you from banning yourself. If your IP is dynamic, leave it out rather than whitelisting an address that may later belong to someone else. Check current bans with sudo fail2ban-client status sshd.

10. Monitor logs and alert on what matters

Logs only help if someone looks at them. To check failed SSH logins on Ubuntu:

sudo journalctl -u ssh --since today | grep -E "Failed password|Invalid user"

Manual checks do not scale. Ship logs to a central system (CloudWatch, Loki or Elasticsearch, for example) and alert on the events that matter: root or sudo activity, new user accounts, logins from unusual locations, and sudden changes in traffic or CPU. Turn on AWS CloudTrail, or your provider's audit logs, to record who changed what in the cloud account.

11. Back up, and test your restores

Backups are your last line of defence against ransomware, accidental deletion and failed upgrades.

  • Take automated snapshots, for example with AWS Backup or DigitalOcean backups.
  • Keep at least one copy in a separate account or location that the server's credentials cannot delete.
  • Test restores on a schedule. A backup you have never restored is a hope, not a plan.

12. Add malware scanning where it helps

Malware on Linux servers usually arrives through a vulnerable application or stolen credentials, so the steps above matter far more than antivirus. Scanning still helps on servers that accept file uploads or serve files to Windows users, and ClamAV is a common open-source choice for scanning uploads.

Need help securing your servers?

Our Managed Infrastructure team hardens, patches and monitors Linux servers on AWS, GCP, Azure and DigitalOcean around the clock. We can audit your current setup against this checklist, fix the gaps, and keep it that way. Talk to our team.

← All posts