> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# 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.

:::info

This guide assumes you are using a Linux or macOS system to access a remote CentOS server via SSH.

:::

:::warning

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:
```bash
adduser your_username
passwd your_username
```
Add the new user to the `wheel` group to grant sudo privileges:
```bash
usermod -aG wheel your_username
```
To test, log in with the new user and try switching to root:
```bash
ssh your_username@your_server_ip
sudo -s
```
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:
```bash
ssh-keygen -t rsa
```
Example:
```
[root@vps ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
```
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:
```bash
ssh-copy-id your_username@your_server_ip
```
### Disable root SSH login
To enhance security, disable SSH access for the root user. Open the SSH configuration file on your server:
```bash
nano /etc/ssh/sshd_config
```
Locate the `PermitRootLogin` line and change it to:
```
PermitRootLogin no
```
Restart SSH to apply the change:
```bash
systemctl restart sshd
```
### Install Fail2Ban
Fail2Ban protects your server from brute-force attacks by banning IP addresses that show malicious behavior. Install it:
```bash
yum install epel-release -y
yum install fail2ban -y
```
Enable and start the Fail2Ban service:
```bash
systemctl enable fail2ban
systemctl start fail2ban
```
### Configure the SSH jail
Create a local jail configuration to protect the SSH service:
```bash
nano /etc/fail2ban/jail.local
```
Add the following configuration:
```ini
[sshd]
enabled = true
port = ssh
logpath = /var/log/secure
maxretry = 5
```
Restart Fail2Ban:
```bash
systemctl 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.
