Securing SSH
1. Introduction
SSH (Secure Shell) is a protocol used to securely connect to remote servers. It provides encrypted communication and is essential for system administrators and developers. However, the default SSH configuration can be vulnerable to attacks. This tutorial will guide you through securing your SSH server.
2. Changing the Default SSH Port
By default, SSH runs on port 22. Changing the port can help obscure your server from automated attacks.
Edit the SSH configuration file:
Find the line:
Uncomment it and change the port number:
Save and close the file, then restart the SSH service:
3. Using SSH Key-Based Authentication
SSH keys provide a more secure way to login compared to passwords. Here’s how to set it up:
Generate a key pair on your local machine:
Press Enter to accept the default file location and optionally add a passphrase.
Copy the public key to the server:
Ensure the correct permissions are set on the server:
chmod 600 ~/.ssh/authorized_keys
4. Disabling Password Authentication
After setting up SSH keys, disable password authentication to prevent brute-force attacks.
Edit the SSH configuration file:
Find the lines:
#PermitEmptyPasswords no
Uncomment and change to:
PermitEmptyPasswords no
Save and close the file, then restart the SSH service:
5. Limiting SSH Access to Specific Users
Restrict SSH access to specific users for additional security.
Edit the SSH configuration file:
Add the following line:
Replace user1 and user2 with the actual usernames. Save and close the file, then restart the SSH service:
6. Enabling Two-Factor Authentication (2FA)
Adding two-factor authentication can provide an extra layer of security.
First, install the Google Authenticator PAM module:
Next, run the Google Authenticator setup for the user:
Answer the prompts to configure 2FA. Then, update the SSH configuration to use the new PAM module:
Add the following line at the end:
Finally, edit the SSH configuration file to enable ChallengeResponseAuthentication:
Find the line:
Uncomment and change to:
Save and close the file, then restart the SSH service:
7. Conclusion
Securing your SSH server is crucial for protecting your systems from unauthorized access. By following the steps in this tutorial, you can enhance the security of your SSH configuration and reduce the risk of attacks.