- Linux server with root/sudo access
- SSH access to your server
- Windows machine with command prompt access (for SSH key generation)
Creating a Personal User
Before disabling root login, create a personal user account with sudo privileges.Create a new user
username with your desired username):Add user to sudo group
Verify user creation
sudo whoami should return root.Setting Up SSH Keys
SSH keys provide a more secure authentication method than passwords. We’ll generate keys on Windows and transfer them to your server.Generating SSH Keys on Windows
Open Command Prompt
Generate SSH key pair
- Choose a file location (press Enter for default:
%USERPROFILE%\.ssh\id_rsa) - Set a passphrase (optional but recommended for extra security)
Verify key generation
- Private key:
%USERPROFILE%\.ssh\id_rsa(keep this secret!) - Public key:
%USERPROFILE%\.ssh\id_rsa.pub(this is what you’ll upload)
Uploading SSH Key to Server
Create .ssh directory on server
.ssh directory for your user:chmod 700 ensures only you can read, write, and execute in this directory.Upload public key via SCP
SERVER_IP with your server’s IP address or hostname.Set correct permissions
Test SSH key authentication
Adding SSH Key for Personal User
If you want to use SSH keys with your personal user account:Create .ssh directory for user
Copy authorized_keys
Securing SSH Configuration
Now we’ll harden your SSH configuration to prevent common attacks.Backup SSH configuration
Edit SSH configuration
Change SSH port
#Port 22 and change it to a random high port (between 1024 and 65535):Set AddressFamily to inet
#AddressFamily any and change it to:Disable root login
#PermitRootLogin yes and change it to:Disable password authentication
#PasswordAuthentication yes and change it to:Save and exit
Test configuration
Restart SSH service
Connecting After Configuration Changes
After securing SSH, you’ll need to connect differently:Connect with new port
23456 with your chosen port and username with your personal user.Update SSH config (optional)
%USERPROFILE%\.ssh\config on Windows to simplify connections:Complete SSH Configuration Example
Here’s a complete hardened SSH configuration (/etc/ssh/sshd_config) with recommended settings:
Verifying Security Settings
Check that your SSH configuration is secure:Troubleshooting
Locked out after disabling root login
Locked out after disabling root login
- Use your hosting provider’s console/KVM access
- Boot into recovery mode if available
- Mount the filesystem and edit
/etc/ssh/sshd_config - Change
PermitRootLogin notoPermitRootLogin yestemporarily - Restart SSH and fix your user account
Can't connect after changing port
Can't connect after changing port
- The new port is allowed in UFW:
sudo ufw allow 23456/tcp - Your firewall isn’t blocking the port
- You’re using the correct port:
ssh -p 23456 username@SERVER_IP - SSH service is running:
sudo systemctl status sshd
SSH key not working
SSH key not working
- Public key is in
~/.ssh/authorized_keys - Permissions are correct:
~/.sshshould be700~/.ssh/authorized_keysshould be600
- Private key matches public key on server
- SELinux isn’t blocking (if enabled):
sudo restorecon -R ~/.ssh
Permission denied errors
Permission denied errors
Security Checklist
✓ User Management
- Created personal user with sudo access
- Root login disabled
✓ SSH Keys
- Generated 4096-bit SSH key pair
- Public key uploaded to server
- Password authentication disabled
✓ SSH Configuration
- Changed to random high port
- AddressFamily set to inet
- Root login disabled
✓ Firewall
- UFW configured with new SSH port
- Only necessary ports open