Securing your new server
Any server on the internet becomes a target for password-guessing attempts within minutes of going online. The network-level firewall closes a limited number of dangerous ports, and everything else is open, so protecting your server from the inside is your responsibility. Carry out these steps in order after connecting to your server over SSH.
Some of these steps can lock you out if you get them wrong. Keep your current SSH session open, and test signing in from a new window after each change. If you do get locked out, you can always get in from the server's screen through the Web-console, which doesn't go through SSH or the firewall. It's also a good idea to take a snapshot before you start.
Create a user with sudo privileges
Instead of working day to day as root, create a regular user that can run administrator commands when needed by typing sudo before them. In the examples, the username is admin; choose your own.
adduser admin
usermod -aG sudo adminTest the new user from another window: ssh [email protected], then sudo whoami. It should print root.
Sign in with an SSH key instead of a password
An SSH key is a pair of files: a private key that stays on your computer and that you never share, and a public key that you put on the server. It can't be guessed the way passwords can.
Create the key on your computer (not on the server). The same command works in PowerShell on Windows and in Terminal on macOS and Linux:
ssh-keygen -t ed25519Press Enter to accept the save location, and choose a passphrase to protect the key in case your computer is stolen. The public key is saved in the id_ed25519.pub file inside the .ssh folder in your user folder.
Copy the public key to the server:
ssh-copy-id [email protected]Test signing in from a new window: ssh [email protected]. If you get in without being asked for the server password (you may be asked for the key's passphrase), the key works.
Disable password and root sign-in
Once you've confirmed that the key works for the new user, open the SSH configuration file:
sudo nano /etc/ssh/sshd_configAnd set these two lines (remove the # at the start of each if present):
PermitRootLogin no
PasswordAuthentication noOn some recent systems, there are additional files in the /etc/ssh/sshd_config.d/ folder that may re-enable passwords, so check them too. Then validate the configuration and restart the service:
sudo sshd -t && sudo systemctl restart sshdOn Ubuntu and Debian, the service may be called ssh. Test signing in from a new window before you close your current session.
After you disable password sign-in over SSH, changing the password from VMX Console is still useful for signing in from the Web-console in an emergency, since it isn't affected by the SSH settings.
Enable the firewall inside the server
Allow only the ports your services need. Allow SSH first before enabling it, or you'll lose your connection.
sudo apt install ufw
sudo ufw allow OpenSSH
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
sudo ufw statusPorts 80 and 443 are for websites, so remove them if your server doesn't host a site. For a database that another server connects to, allow its port for that server's address only, for example sudo ufw allow from 203.0.113.20 to any port 3306.
Panels such as cPanel, Plesk and ispmanager manage the firewall themselves and open their own ports. Follow the panel's settings instead of enabling ufw or firewalld manually on top of it.
Enable automatic security updates
sudo apt install unattended-upgrades
sudo dpkg-reconfigure --priority=low unattended-upgradesBlock guessing attempts (optional)
Fail2ban monitors sign-in logs and temporarily bans addresses that repeat failed attempts:
sudo apt install fail2ban # Ubuntu / Debian
sudo dnf install epel-release && sudo dnf install fail2ban # AlmaLinux / Rocky
sudo systemctl enable --now fail2banIts default configuration protects the SSH service. On CloudLinux plans, Imunify360 handles this automatically.
What's next
- Backups: security is no substitute for backups. See Backups.
- Application passwords: use long, unique passwords for panels and databases, and enable two-step verification where available.
- Unused services: stop any service you don't need, since every service listening on a port is a potential way in.
How is this guide?
Connecting to your server over SSH
How to connect to a Linux VPS over SSH from Windows, macOS and Linux, what the first-connection messages mean, and fixes for common errors.
Editing server settings
Change the main settings, resources and add-ons of an existing VPS: CPU, memory, storage, IP addresses, backups and Mail-ready.