Every Linux server exposed to the internet faces SSH login attempts from automated bots within minutes. In this guide we explain step by step how to set up SSH key login, disable password login and apply basic hardening to your server.
Step 1: Create an SSH key pair
Create the key on your own computer (the same command works in Windows 10/11 PowerShell, macOS and Linux):
ssh-keygen -t ed25519 -C "[email protected]"Accept the default file location and set a passphrase to protect your key. This creates two files:
~/.ssh/id_ed25519: the private key; never share it with anyone.~/.ssh/id_ed25519.pub: the public key; this is the one you upload to the server.
Step 2: Copy the public key to the server
On macOS/Linux:
ssh-copy-id kullanici@SUNUCU_IP_ADRESIIn Windows PowerShell:
type $env:USERPROFILE\.ssh\id_ed25519.pub | ssh kullanici@SUNUCU_IP_ADRESI "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"Now test that you can connect without being asked for a password:
ssh kullanici@SUNUCU_IP_ADRESIStep 3: Create a user with sudo privileges
Instead of using root directly for everyday tasks, use a user with sudo privileges:
adduser yonetici
usermod -aG sudo yonetici # Ubuntu/Debian
usermod -aG wheel yonetici # AlmaLinux/RockyThen repeat step 2 for this user as well.
Step 4: Harden the SSH configuration
Before this step, make absolutely sure that key login works, and don't close your current SSH session. Otherwise you may lose access to the server.
Keeping your settings in a separate file prevents conflicts during updates:
sudo nano /etc/ssh/sshd_config.d/99-guvenlik.confPasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin prohibit-password
PubkeyAuthentication yes
MaxAuthTries 3
LoginGraceTime 30
X11Forwarding noTest the configuration and restart the service:
sudo sshd -t
sudo systemctl restart ssh # Ubuntu/Debian
sudo systemctl restart sshd # AlmaLinux/RockyTry connecting again from a new terminal window. If it succeeds, you can close the old session.
Step 5 (optional): Change the SSH port
Changing the default port 22 greatly reduces the log noise generated by automated scanning bots. Add the following line to the same file:
Port 2222Allow the new port in the firewall (before restarting!):
# UFW (Ubuntu)
sudo ufw allow 2222/tcp
# firewalld (AlmaLinux) + SELinux
sudo firewall-cmd --permanent --add-port=2222/tcp && sudo firewall-cmd --reload
sudo semanage port -a -t ssh_port_t -p tcp 2222On Ubuntu 24.04, SSH may run with systemd socket activation. After changing the port, you may also need to run sudo systemctl daemon-reload && sudo systemctl restart ssh.socket.
From now on you connect like this:
ssh -p 2222 kullanici@SUNUCU_IP_ADRESIStep 6: Additional measures
- Automatically block repeated failed logins with Fail2Ban: UFW and Fail2Ban guide.
- Enable the
unattended-upgradespackage on Ubuntu for automatic security updates. - Disable services you don't use and leave only the necessary ports open.
Conclusion
With these steps your server has become far more resilient against brute-force attacks. On MerkezHost VDS servers, these measures add to the network-level DDoS protection to give your server multi-layered security. Explore our VDS plans.
Frequently Asked Questions
Does changing the SSH port improve security?
Changing the port reduces the noise from automated scanning bots, but on its own it is not a security measure. The real protection is key-based login and disabling password login.
What happens if I lose my SSH key?
You can connect to the server via VNC/console and add a new key. That's why it's recommended to keep a secure backup of your key.
