When you deploy a new Ubuntu server — whether on Google Cloud, AWS, or any other platform — it’s essential to harden it immediately.
This minimizes attack surfaces and protects your system from unauthorized access.
Here’s a complete guide to basic, essential hardening for a fresh Ubuntu server.
Step 1: Set a Strong Password for Your User
If your server uses an automatically created user (like ubuntu
or a custom name), it might not have a secure password initially — often only SSH keys are set up.
Set a strong password manually:
Example:
You’ll be prompted to enter and confirm the new password.
Tip for passwords:
-
Minimum 12 characters.
-
Mix uppercase, lowercase, numbers, and symbols.
-
Example:
Sunshine!2024
orGcpServer#Secure1
Step 2: Disable Root Login over SSH
Allowing direct root login is a serious security risk.
Attackers frequently target root with brute-force attacks.
To disable root SSH login:
-
Open the SSH server configuration file:
-
Find the line:
or
-
Change it to:
-
Save and exit (
CTRL+O
,ENTER
,CTRL+X
). -
Restart the SSH service:
Now, even if someone knows the root password, they cannot SSH directly as root.
Step 3: Restrict SSH Access to Specific Users
You can further lock down SSH access by explicitly specifying allowed users.
Edit the same SSH configuration file:
Add at the end:
Example:
Then restart SSH:
Now, only the listed user(s) can connect over SSH.
Step 4: Install Fail2Ban to Block Attackers
Fail2Ban automatically bans IP addresses that show malicious signs — like too many failed login attempts.
Install Fail2Ban:
Fail2Ban works out of the box.
For basic setups, no configuration changes are needed.
If you want to fine-tune it, the config files are located in /etc/fail2ban/
.
Step 5: Keep Your System Updated
Security patches come out regularly. Always keep your system up to date.
Run:
Optional (remove unnecessary packages):
Finally, reboot to apply any critical kernel updates:
Extra Recommendations
-
Disable unused services:
Usesudo systemctl disable servicename
for services you don’t need. -
Set up a firewall:
Useufw
(Uncomplicated Firewall) to allow only specific traffic:
-
Change default SSH port:
Editing/etc/ssh/sshd_config
to use a non-standard port (like 2222) can reduce random attack noise (but won’t stop targeted attacks). -
Monitor logs:
Regularly check/var/log/auth.log
for suspicious activity.
Conclusion
Basic hardening steps like setting strong passwords, disabling root login, restricting SSH users, and installing tools like Fail2Ban are critical for protecting your Ubuntu server.
It only takes a few minutes but dramatically increases your server’s security.
Remember:
Security is not a one-time event — it’s an ongoing process.