In short
To secure a new Ubuntu server, work in this order: update packages, create a separate sudo user, add an SSH key, disable root and password logins, enable the UFW firewall, install Fail2ban, turn on automatic security updates, audit listening ports, monitor login records and keep backups off the server. The whole checklist takes about half an hour and blocks most automated attacks from day one.
In this guide
- 1. Update the System
- 2. Create a Separate User with sudo Privileges
- 3. Log In with an SSH Key
- 4. Harden the SSH Server
- 5. Enable the UFW Firewall
- 6. Block Brute-Force Attempts with Fail2ban
- 7. Turn On Automatic Security Updates
- 8. Audit Open Ports
- 9. Time, Logs and Login Audits
- 10. Keep Backups Off the Server
- Bonus: Three Details People Often Miss
- Checklist
- Next Step: Start on a Secure Foundation
A freshly installed Ubuntu server becomes a target for automated scanners the moment its IP address goes live; password-guessing attempts against the SSH port usually start within the first few hours. The good news: the 10 steps below take about half an hour and shut down the vast majority of attacks from the start. Follow the steps in the order given; the order matters so you do not lock yourself out of your own server.
Golden rule: Never close your current session while changing SSH or firewall settings. Always test a new setting by connecting from a second terminal window.
1. Update the System
The install image does not include security patches released after it was published. Update every package first:
sudo apt update && sudo apt full-upgrade -y
# If the kernel was updated, a reboot is required:
[ -f /var/run/reboot-required ] && sudo reboot2. Create a Separate User with sudo Privileges
Doing daily work as root means a single wrong command or a stolen password affects the entire system. Create a separate user for administration and give it sudo privileges. sudo lets a regular user run commands with admin rights when needed, with every use logged.
adduser deploy
usermod -aG sudo deploy
# Test: switch to the new user and verify the privilege
su - deploy
sudo whoami # output: root3. Log In with an SSH Key
Passwords can be guessed and brute-forced; ed25519 SSH keys are practically unbreakable. Generate a key on your own computer and copy it to the new user:
# On your own computer:
ssh-keygen -t ed25519
ssh-copy-id deploy@203.0.113.10
# Then test logging in with the key:
ssh deploy@203.0.113.10Windows does not include ssh-copy-id; you will find an alternative method and more details in How to Create an SSH Key and Disable Password Login.
4. Harden the SSH Server
Once you have confirmed that key-based login works, disable root login and password login. Instead of editing the main file, use a separate file under /etc/ssh/sshd_config.d/. Because OpenSSH uses the first value it reads for a setting, start the file name with a low number so it is read before any files added by installation tools.
sudo nano /etc/ssh/sshd_config.d/01-sertlestirme.confPermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
MaxAuthTries 3
AllowUsers deploysudo sshd -t # prints nothing if there are no syntax errors
sudo systemctl reload ssh
sudo sshd -T | grep -E 'permitrootlogin|passwordauthentication'Now try connecting from a second terminal with ssh deploy@203.0.113.10. If it works, you can close the first session. On newer Ubuntu releases, SSH runs through systemd socket activation; if you change the port, you also need sudo systemctl daemon-reload and sudo systemctl restart ssh.socket.
5. Enable the UFW Firewall
UFW (Uncomplicated Firewall) is Ubuntu's simple management tool for the Linux packet filter. Allow SSH first, then enable it; if you do it the other way around, your connection will drop.
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow OpenSSH
sudo ufw allow 80,443/tcp # if a web server will run
sudo ufw enable
sudo ufw status verboseFor IP restrictions, application profiles and Docker bypassing UFW, see How to Set Up a Linux Firewall with UFW.
6. Block Brute-Force Attempts with Fail2ban
Fail2ban watches log files and bans IP addresses that fail to log in repeatedly for a set period. Even with password login disabled, it cuts down log noise and unnecessary load.
sudo apt install fail2ban -y
sudo nano /etc/fail2ban/jail.local[sshd]
enabled = true
maxretry = 5
findtime = 10m
bantime = 1hsudo systemctl restart fail2ban
sudo fail2ban-client status sshdIf your system keeps authentication logs only in journald, add the line backend = systemd to the [sshd] section. The ignoreip setting, which keeps you from accidentally banning your own IP, and other jail examples are covered in How to Install Fail2ban and Stop Brute-Force Attacks.
7. Turn On Automatic Security Updates
The unattended-upgrades package installs security updates without you having to log in. It usually comes preinstalled on Ubuntu Server; confirm that it is enabled:
sudo apt install unattended-upgrades -y
sudo dpkg-reconfigure --priority=low unattended-upgrades
cat /etc/apt/apt.conf.d/20auto-upgradesYou should see these two lines in the file:
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";To test that it works, run sudo unattended-upgrade --dry-run --debug. Kernel patches require a reboot; make it a habit to check for the /var/run/reboot-required file during your maintenance window.
8. Audit Open Ports
The firewall is your last line of defense; the real goal is not to expose services that never need to be reachable from outside. See which service is listening on which address:
sudo ss -tulpn- Any service listening on
0.0.0.0or[::]is reachable from the internet (unless the firewall blocks it). - Services such as MySQL/MariaDB, Redis and PostgreSQL should listen only on
127.0.0.1if they are used only from the same server. - Ports published with Docker can bypass UFW rules; where possible, bind container ports to the local address only, in the form
127.0.0.1:8080:80. - Shut down services you do not use:
sudo systemctl disable --now servis_adi
For an outside view, scan from your own computer with nmap 203.0.113.10 and confirm that only the ports you expect are open. Only scan servers you own.
9. Time, Logs and Login Audits
Accurate time is required to interpret logs and for TLS certificates to work. Set the time zone (the example uses Istanbul; use your own) and check logins regularly:
sudo timedatectl set-timezone Europe/Istanbul
timedatectl # NTP sync should be "yes"
last -n 10 # recent successful logins
sudo lastb -n 10 # recent failed logins
sudo journalctl -u ssh --since today # today's SSH logsIf you see a successful login from an IP you do not recognize, rotate your keys and investigate the server.
10. Keep Backups Off the Server
No security measure is 100% effective; ransomware or a mistaken command can wipe your data. That is why backups stored on the same server do not count as backups. Send database dumps and application files to another location on a regular schedule, and test a restore at least once. A system whose backups have never been restored is not much different from a system with no backups at all.
As a simple start, set up a cron job that takes a compressed MySQL/MariaDB dump every night, then push that folder to off-server storage with rclone or rsync:
# sudo crontab -e
30 3 * * * mysqldump --all-databases --single-transaction | gzip > /var/backups/db-$(date +\%F).sql.gz
# delete dumps older than 7 days
45 3 * * * find /var/backups -name 'db-*.sql.gz' -mtime +7 -deleteNote that the % character has a special meaning in crontab lines, so it is escaped with a backslash in the date format.
Bonus: Three Details People Often Miss
- Unused packages: Every service you do not need is attack surface that has to be kept updated. Review installed packages with
apt list --installedand remove the ones you do not use. - AppArmor: Do not disable this mandatory access control layer, which is enabled by default on Ubuntu; check its status with
sudo aa-status. - Password vault: Store database, panel and API passwords in a password manager, not in plain-text notes. Make sure
.envfiles containing application secrets cannot be reached from the web root.
Checklist
| # | Step | Verification command |
|---|---|---|
| 1 | Package updates | apt list --upgradable |
| 2 | sudo user | groups deploy |
| 3 | SSH key | cat ~/.ssh/authorized_keys |
| 4 | SSH hardening | sudo sshd -T | grep -E 'permitroot|password' |
| 5 | UFW | sudo ufw status verbose |
| 6 | Fail2ban | sudo fail2ban-client status sshd |
| 7 | Automatic updates | systemctl status unattended-upgrades |
| 8 | Open ports | sudo ss -tulpn |
| 9 | Time and logs | timedatectl, last |
| 10 | Off-server backup | Test restore |
Next Step: Start on a Secure Foundation
This checklist works the same way on any KVM-based server with root access. Sunucu Atlas VDS plans include layered DDoS protection at the network layer, while hardening inside the operating system stays in your hands with the steps above.
- For web applications that need high single-core performance, check out Ryzen Premium VDS plans with the AMD Ryzen 9 5900X;
- for parallel workloads such as Docker and queue workers, check out Intel Premium VDS plans, which offer more vCPUs per GB of RAM.
Both families run in our Istanbul data center with NVMe storage and automated setup. Apply these 10 steps right after setup; if you get stuck, open a support ticket and our team will help. To size your server correctly from day one, see the Ryzen VDS plan comparison.
Frequently asked questions
What should I do first on a new Ubuntu server?
The first job is updating all packages with apt update and apt full-upgrade. After that, create a separate user with sudo privileges and set up SSH key login.
Is it necessary to disable root login over SSH?
Yes. Most automated attacks target the root user. After testing that key-based login works with a separate sudo user, disable root login with the PermitRootLogin no setting.
Will enabling UFW drop my SSH connection?
Not if you allowed SSH with ufw allow OpenSSH before enabling it. If you reverse the order, your current connection may drop and you may lose access to the server.
Does changing the SSH port improve security?
It reduces automated scan noise in your logs, but it is not a security measure on its own. Real protection comes from key-based login, disabling password login and Fail2ban.
Can Ubuntu install security updates automatically?
Yes, the unattended-upgrades package installs security updates automatically. For kernel updates to take effect, you need to reboot the server at a scheduled time.