Securing SSH Authentication on Your CentOS Server
Are you concerned about securing your CentOS server from unauthorized SSH access? This guide walks through the essential steps to harden your server: creating a non-root user, setting up SSH key authentication, disabling remote root logins, and installing Fail2Ban to defend against brute-force attacks.
This guide assumes you are using a Linux or macOS system to access a remote CentOS server via SSH.
Make sure you can log in successfully with the new user before you disable root SSH access, or you may lose remote access to the server. Keep your current root session open until you have confirmed that a new connection works.
Harden SSH Access
Create a new user with sudo privileges
To reduce the risk of compromise, it is best practice not to use the root account for day-to-day tasks. Log in to your server as root and create a new user:
Add the new user to the wheel group to grant sudo privileges:
To test, log in with the new user and try switching to root:
Replace your_username and your_server_ip with your actual username and server IP address.
Generate an SSH key pair
SSH key-based authentication is more secure than password-based logins. Generate the key pair on your server as root:
Example:
A new and unique key pair is generated each time you run this command.
Copy the SSH public key to your local machine
On your local system, copy the server's public key to enable password-less login:
Disable root SSH login
To enhance security, disable SSH access for the root user. Open the SSH configuration file on your server:
Locate the PermitRootLogin line and change it to:
Restart SSH to apply the change:
Install Fail2Ban
Fail2Ban protects your server from brute-force attacks by banning IP addresses that show malicious behavior. Install it:
Enable and start the Fail2Ban service:
Configure the SSH jail
Create a local jail configuration to protect the SSH service:
Add the following configuration:
Restart Fail2Ban:
Final Notes
Your CentOS server is now more secure against unauthorized access. Remember to:
- Use strong passwords and unique SSH keys.
- Disable root login whenever possible.
- Update your system regularly and monitor login activity.

