This guide walks through creating a non-root user with sudo privileges, copying SSH keys, testing connection, and disabling direct root logins over SSH.
| Step | Task | Command |
|---|---|---|
| 1. Create User | Add an unprivileged user | adduser <username> |
| 2. Grant Sudo | Add user to administrative group | usermod -aG sudo <username> |
| 3. Copy SSH Keys | Transfer keys from root to new user | rsync --archive --chown=<user>:<user> ~/.ssh /home/<user>/ |
| 4. Test Access | Verify login in a new terminal session | ssh <username>@<server_ip> |
| 5. Harden SSH | Disable direct root logins | PermitRootLogin no in /etc/ssh/sshd_config |
Logged in as root on your fresh VPS, create your new unprivileged user account (replace sammy with your desired username):
adduser sammyYou will be prompted to set and confirm a password. You can press Enter to skip the optional user information fields (Full Name, Room Number, etc.).
Add the newly created user to the sudo group so you can execute administrative commands when necessary:
usermod -aG sudo sammyIf you log in using an SSH key pair, you must copy your root user's authorized keys to the new user's home directory.
Run this command while still logged in as root:
rsync --archive --chown=sammy:sammy ~/.ssh /home/sammy/Alternatively, switch to the new user and manually paste your public key:
# Switch to the new user
su - sammy
# Create .ssh directory with correct permissions
mkdir -p ~/.ssh
chmod 700 ~/.ssh
# Paste your public SSH key into authorized_keys
nano ~/.ssh/authorized_keys
# Set strict file permissions
chmod 600 ~/.ssh/authorized_keys
exit
⚠️ CRITICAL: Do not close your activerootSSH session yet! Keep it open as a fallback in case there is a connection issue.
Open a new terminal window on your local machine and test logging in with your new user account:
ssh sammy@YOUR_VPS_IPOnce connected, test sudo access:
sudo apt updateIf prompted, enter your user password. If the package list updates successfully, your user setup is complete and functional.
To secure your VPS against brute-force attacks, disable direct root logins.
-
Open the SSH server configuration file:
sudo nano /etc/ssh/sshd_config
-
Locate the line
PermitRootLoginand update it tono:PermitRootLogin no -
Save the file (
Ctrl + O, thenEnter) and exitnano(Ctrl + X). -
Restart the SSH service to apply changes:
sudo systemctl restart ssh
- Logged in via SSH as non-root user
- Confirmed
sudoprivilege execution - Direct
rootSSH login blocked (PermitRootLogin no) - Optional: Configured UFW firewall (
sudo ufw allow OpenSSH && sudo ufw enable)