Securing a dedicated hosting environment is just as important as optimizing its performance. One of the most essential aspects of server security is protecting your SSH access. Without proper techniques, SSH can become a target for attackers. This guide will show you the most effective techniques for SSH Hardening for Dedicated Hosting.
At PerLod Hosting, we provide powerful and reliable dedicated hosting solutions designed for high performance and security. With the right SSH hardening strategies, you can ensure that your hosting environment remains fast, stable, and secure.
Let's dive into the guide steps.
Prerequisites for SSH Hardening for Dedicated Hosting
Important safety note before we start: Always keep at least one active SSH session open while testing changes. If you lock yourself out, you will need console or provider recovery to fix it.
Prerequisites:
You must have a working shell with sudo or root access.
OpenSSH server (sshd) is installed on your Linux server.
A second terminal or separate machine to test SSH connectivity.
Check that OpenSSH is installed and running on your server:
Always review the kernel updates; they may need reboots.
2. Create a Sudo User in Dedicated Server
Because we want to limit root logins to improve accountability and reduce attack surface, you should not use the root user. You must create a user with sudo privileges.
To create the user, you can run the following command:
3. Use SSH Key Authentication (Strongly Recommended)
One of the most important techniques for hardening SSH for dedicated hosting is to use SSH key authentication.
It is recommended to use the "ed25519" key on your client because it is compact, secure, and fast. To generate the SSH key on your local machine, you can run the command below:
Note: We use ed25519 over RSA because it is smaller, safer, and efficient.
4. Disable Password Authentication After SSH Key Setup
After you have confirmed that SSH key-based login works in a separate session, you can disable passwords and root logins for security.
To do this, open the sshd config file:
Bash
sudonano/etc/ssh/sshd_config
Set the following lines as shown below:
Bash
PermitRootLogin<spanstyle="color:#cf2e2e;">no</span>PasswordAuthentication<spanstyle="color:#cf2e2e;">no</span>ChallengeResponseAuthentication<spanstyle="color:#cf2e2e;">no</span>UsePAM<spanstyle="color:#cf2e2e;">yes</span>#keep PAM if your distro relies on it for sudo/2FA.PubkeyAuthentication<spanstyle="color:#cf2e2e;">yes</span>AuthorizedKeysFile<spanstyle="color:#cf2e2e;">.ssh/authorized_keys</span>
Then, reload the sshd to apply the changes:
Bash
sudosystemctlreloadsshd
You can also run the following command instead of editing the file manually:
6. Disable or Restrict SSH Weak Algorithms and Protocol Settings
When you connect to a server using SSH, your computer and the server need to agree on how to talk securely. They use things like:
KEX (Key Exchange): How they agree on a secret key.
Ciphers: How your messages get scrambled so nobody can read them.
MACs (Message Authentication Codes): How they make sure nobody changes the messages while they travel.
By default, OpenSSH already uses Protocol 2, which is safe. But you can make it even more secure by telling SSH which modern and strong algorithms it should use.
Important Warning: If you make SSH too strict, old computers or tools that don’t understand these modern algorithms won’t be able to connect. That’s why you should test first and make sure your clients can still log in before applying these changes permanently.
You can use these modern algorithms in the sshd config file:
7. Reduce Attack Surface: Change SSH Port and Control Max Attempts
By default, SSH runs on port 22, which attackers constantly scan. You can change it to a non-standard port. This doesn’t make your server “unhackable,” but it reduces the noise from automated bots trying to break in.
You can limit the number of password attempts and login grace time. Also, you can be sure no one can log in with a blank password.
Open the sshd config file and modify these options:
Another option is to throttle connections (DoS Protection). This setting controls how many simultaneous new SSH connections are allowed.
From the sshd config file, modify:
Bash
MaxStartups10:30:60
After changes, test and reload SSH:
Bash
sudosshd-tsudosystemctlreloadsshd
8. Allow Only Required IPs and Ports for SSH Security
It is recommended to allow only required IPs and ports. You can use your server's firewall, like UFW and FirewallD, or your hosting provider's security group.
For example, if you change the SSH port to 2222, allow it through your firewall:
Note: If you want to lock access to specific IP ranges, add those rules. Always ensure your management IP is allowed before enabling a firewall.
9. Use Fail2Ban to Block Brute-Force SSH Attempts
As you must know, Fail2Ban watches auth logs and blocks IPs that exceed failed attempts. You can create a local override to avoid upgrades overwriting with the following command:
Bash
sudotee/etc/fail2ban/jail.d/ssh.local>/dev/null<<'EOF'[sshd]enabled=trueport=2222filter=sshdmaxretry=5bantime=3600#ban for 1 hourfindtime=600#10 minutes windowEOF
Verify logs in /var/log/auth.log (Debian/Ubuntu) or /var/log/secure (RHEL).
10. Add Two-Factor Authentication (2FA) To Secure SSH Logins
You can make your SSH logins much more secure by adding Two-Factor Authentication (2FA). It means you first log in with your password or key, then you also confirm your identity with a one-time code from an app or a hardware key.
Two methods to add 2AF:
TOTP codes (Google Authenticator, Authy, etc.)
Hardware keys (like YubiKey)
Warning: If you misconfigure 2FA, you might lock yourself out, so test carefully. Hardware security keys are preferred for enterprise-grade control.
For example, we want to set up Google Authenticator (TOTP):
Install the package with the command below:
Bash
sudoaptinstalllibpam-google-authenticator-y
For each user, run this (as that user):
Bash
google-authenticator
It shows a QR code. Scan it with Google Authenticator on your phone. Save the backup codes somewhere safe.
Next, you must tell the system to require 2FA by editing /etc/pam.d/sshd file. Add this line at the top before other auth lines:
Bash
authrequiredpam_google_authenticator.sonullok
Make sure these are set in the /etc/ssh/sshd_config file:
Bash
UsePAMyesChallengeResponseAuthenticationyes
Finally, reload SSH:
Bash
sudosystemctlreloadsshd
11. SSH Certificates and Centralized Authentication (For Scaling)
If you manage lots of servers, it is recommended not to copy keys everywhere. Instead, use an SSH Certificate Authority (CA). The CA can issue short-lived certificates that prove a user’s identity. It has easy revocation, shorter lifetimes, and no need to edit every server’s authorized_keys.
For a high-level process, create a CA keypair on a secure (offline) machine. Then, add the CA’s public key to the /etc/ssh/sshd_config file:
This example makes a certificate valid for 52 weeks.
12. SSH Logging, Auditing, and Detection
You must be sure all SSH login attempts are sent to your central log system, like rsyslog. You can use Auditd to track changes to critical files like /etc/ssh/sshd_config or ~/.ssh/authorized_keys. Also, use regular scans with tools like ssh-audit, Lynis, or OpenSCAP to detect weak settings.
Always protect private keys with a passphrase. You can use ssh-agent so you don’t need to retype passphrases constantly. Rotate keys if an employee leaves or on a set schedule. If using certificates, revoke them instead of editing every server.
Tip: GitHub’s docs on SSH agent and key management are a good reference.
14. SSH Backup and Emergency Access Plan
Always keep a backup admin account in case your main account is locked. Remember to have out-of-band access, like your provider’s VNC or serial console.
When changing ports or firewall rules, keep the current session open until you’ve confirmed the new settings work.
Before editing, save a backup with:
Bash
cp/etc/ssh/sshd_config/etc/ssh/sshd_config.bak
Example Hardened sshd_config File for a Secure SSH Setup
Here is a starting point for a secure SSH setup. You can customize it for your environment.
Bash
# BasicPort2222Protocol2AddressFamilyinet# IPv4 only; use 'any' or 'inet6' if needed# AuthenticationPermitRootLoginnoPasswordAuthenticationnoChallengeResponseAuthenticationnoPubkeyAuthenticationyesAuthorizedKeysFile.ssh/authorized_keys# User restrictionsAllowUsers<spanstyle="color:#cf2e2e;">username</span># or AllowGroups sshusers# Timeouts and limitsLoginGraceTime30MaxAuthTries3MaxSessions2# Connection throttlingMaxStartups10:30:60# PerSourceMaxStartups 2:30:10 # if supported# Keys & algorithms (test clients)HostKey/etc/ssh/ssh_host_ed25519_keyKexAlgorithmscurve25519-sha256@libssh.org,diffie-hellman-group-exchange-sha256Cipherschacha20-poly1305@openssh.com,aes256-gcm@openssh.comMACshmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com# Logging & auditingSyslogFacilityAUTHLogLevelVERBOSE# MiscAllowTcpForwardingnoX11ForwardingnoClientAliveInterval300ClientAliveCountMax2
Check config before reloading:
Bash
sudosshd-t
If no errors, then reload SSH:
Bash
sudosystemctlreloadsshd
That's it, you are done with SSH Hardening for Dedicated Hosting.
Conclusion
SSH hardening for dedicated hosting environment is one of the most essential steps in server security. By creating non-root administrative users, enforcing key-based authentication, disabling password logins, and implementing tools such as Fail2Ban and firewalls, you can reduce the risk of compromise.
Following the practices outlined in this guide will help ensure your dedicated hosting environment remains secure, stable, and accessible to authorized administrators.
Disabling root login prevents attackers from brute-forcing the root account, which is a common target. Instead, you use a regular user with sudo privileges for better accountability and reduced risk.
The ed25519 keys are recommended because they are smaller, faster, and more secure than older RSA keys. If compatibility is a concern, RSA 4096-bit is still acceptable.
You will be locked out unless you have another user account with a key or direct console access. Always keep at least one backup user and ensure console access is available before disabling password login.
We use cookies to run PerLod and to understand how the site is used. You can accept everything, or open Manage to turn individual categories off. Cookie policy.