Replace `your_vps_ip` with your server's IP address.
You'll be prompted for the root password (provided by your VPS host).
2、Create a Non-Root User (Recommended)
bash
adduser your_username Follow prompts to set a password
usermod -aG sudo your_username Grant sudo privileges
3、Set Up SSH Key Authentication (Safer than passwords)
On your local machine:
bash
ssh-keygen -t ed25519 Generate key pair (saves to ~/.ssh/id_ed25519)
ssh-copy-id -i ~/.ssh/id_ed25519.pub your_username@your_vps_ip
Enter the new user's password when prompted.
4、Secure the SSH Daemon
Edit the SSH config file:
bash
sudo nano /etc/ssh/sshd_config
Make these changes:
conf
Port 2222 Change default port (optional)
PermitRootLogin no Disable root login
PasswordAuthentication no Require SSH keys
AllowUsers your_username Restrict allowed users
6、Test the Connection
From your local machine:
bash
ssh -p 2222 your_username@your_vps_ip
If successful, you’ll log in without a password (using your SSH key).
Essential Security Tips
Use Strong Keys: Use `ed25519` or `RSA 4096` keys.
Backup SSH Keys: Securely store your private key (`id_ed25519`).
Fail2Ban: Install to block brute-force attacks:
bash
sudo apt install fail2ban Debian/Ubuntu
sudo systemctl enable fail2ban
Disable Passwords: Ensure `PasswordAuthentication no` is set in `/etc/ssh/sshd_config`.
Port Knocking: (Advanced) Hide SSH port using tools like `knockd`.
Troubleshooting
Locked out? Use your VPS provider’s web console (e.g., DigitalOcean Console Access).
Connection refused? Check firewall rules (`sudo ufw status`) and SSH port (`netstat -tuln`).
Permission denied? Verify `~/.ssh/authorized_keys` permissions (should be `600`).
Example Commands for Management
| Task | Command |
| List active SSH sessions | `who` or `w` |
| Revoke a user’s access | Remove their key from `~/.ssh/authorized_keys` |
| Check SSH logs | `sudo journalctl -u sshd` |
By following these steps, you’ll achieve a secure SSH setup resistant to common attacks、Always test changes before disconnecting! 🔒