Securing SSH is crucial for protecting servers, cloud instances, and remote systems from unauthorized access. Here are the best security practices to follow when using SSH in Red Hat Linux.
1. Disable Root Login
Allowing direct SSH access for the root user is risky. Disable it to force users to log in as a regular user first and then escalate privileges.
How to Disable Root Login:
Edit the SSH configuration file:
sudo nano /etc/ssh/sshd_configFind the line:
PermitRootLogin yesChange it to:
PermitRootLogin noSave the file, then restart SSH:
sudo systemctl restart sshd2. Use SSH Keys Instead of Passwords
Passwords can be guessed or stolen, but SSH keys provide stronger authentication.
How to Set Up SSH Key Authentication:
- Generate SSH keys on your local machine:
ssh-keygen -t rsa -b 4096- Copy the public key to the remote server:
ssh-copy-id username@server-ip- Ensure the keys are in the correct location:
ls ~/.ssh/authorized_keys- Disable password authentication in SSH config:
sudo nano /etc/ssh/sshd_configChange:
PasswordAuthentication yesTo:
PasswordAuthentication noRestart SSH:
sudo systemctl restart sshd- Change the Default SSH Port
Attackers often target port 22, the default SSH port. Changing it can reduce automated attacks.
How to Change SSH Port:
Edit the SSH config file:
sudo nano /etc/ssh/sshd_configFind the line:
Port 22Change it to another number, such as 2222:
Port 2222Save the file and restart SSH:
sudo systemctl restart sshdNow, connect using the new port:
ssh -p 2222 username@server-ip- Use Fail2Ban to Block Repeated Login Attempts
Fail2Ban helps prevent brute-force attacks by blocking IPs that try too many incorrect logins.
How to Install and Configure Fail2Ban:
- Install Fail2Ban:
sudo yum install fail2ban -y- Create a configuration file:
sudo nano /etc/fail2ban/jail.local- Add the following rules:
[sshd]
enabled = true
port = ssh
filter = sshd
maxretry = 3
bantime = 600- Restart Fail2Ban:
sudo systemctl start fail2ban5. Limit SSH Access to Specific IPs
Restrict SSH access to trusted IP addresses to prevent unwanted login attempts.
How to Limit SSH Access:
Edit the SSH configuration file:
sudo nano /etc/ssh/sshd_configAdd this line:
AllowUsers username@your-trusted-ipRestart SSH:
sudo systemctl restart sshd6. Use Two-Factor Authentication (2FA) for SSH
Adding 2FA makes SSH logins even more secure.
How to Set Up 2FA:
- Install the Google Authenticator PAM module:
sudo yum install google-authenticator -y- Configure authentication:
google-authenticatorFollow the on-screen instructions.
- Edit the SSH PAM configuration:
sudo nano /etc/pam.d/sshdAdd:
auth required pam_google_authenticator.so- Modify the SSH configuration:
sudo nano /etc/ssh/sshd_configAdd:
ChallengeResponseAuthentication yes- Restart SSH:
sudo systemctl restart sshdSummary
By applying these advanced security practices, you can significantly reduce security risks when using SSH in Red Hat Linux. Whether you're managing cloud infrastructure, handling servers, or working remotely, securing SSH is a critical step in maintaining a safe computing environment.