When you deploy a new Linux Virtual Private Server (VPS), you are typically provided with administrative access as the root user. Operating continuously as the root superuser poses substantial security risks: any command is executed without confirmation, and unauthorized attackers frequently target the root username with automated brute-force scripts.

The standard security best practice on Linux is to create a dedicated non-root user with sudo (superuser do) privileges for daily administrative tasks, set up secure SSH key access for that user, and restrict direct root login.

In this step-by-step guide, you will learn how to create a new sudo user, grant administrative rights, configure SSH key authentication, and harden SSH daemon security on Ubuntu 26.04 LTS, Ubuntu 24.04 LTS, Ubuntu 22.04 LTS, and Ubuntu 20.04 LTS on your Aveshost VPS.


Prerequisites


Step-by-Step Guide: How to Create a Sudo User on Ubuntu

Step 1: Log in as the Root User

Connect to your Ubuntu server using your terminal:

ssh root@YOUR_SERVER_IP

Step 2: Create a New User Account

Run the adduser command to create a new user (replace sammy with your desired username):

adduser sammy

You will be prompted to:

  1. Enter and confirm a strong password for the new user account.
  2. Optionally enter additional contact details (Full Name, Room Number, etc.) or press ENTER to skip.
  3. Type Y and press ENTER to confirm the information is correct.

Step 3: Add the User to the Sudo Group

On Ubuntu, members of the sudo group are granted elevated administrative privileges using the sudo command prefix. Add your new user to the sudo group:

usermod -aG sudo sammy

Step 4: Test Sudo Access for the New User

  1. Switch to your new user account:
    su - sammy
  2. Test running a command with sudo:
    sudo whoami
  3. Enter your user password when prompted. If the output returns root, your user has full administrative privileges!

Step 5: Set Up SSH Key Authentication for the New User

To enable passwordless, secure SSH login for your new user account:

  1. While logged in as your new user (e.g. sammy), create the .ssh directory:
    mkdir -p ~/.ssh
    chmod 700 ~/.ssh
  2. Create the authorized_keys file:
    nano ~/.ssh/authorized_keys
  3. Paste your local computer’s public SSH key into the file, save, and exit (CTRL + O, ENTER, CTRL + X).
  4. Secure file permissions:
    chmod 600 ~/.ssh/authorized_keys

Step 6: (Security Hardening) Disable Direct Root SSH Login

Now that your sudo user can administer the server and log in via SSH keys, disable direct root SSH logins to prevent unauthorized access:

  1. Open the SSH daemon configuration file:
    sudo nano /etc/ssh/sshd_config
  2. Find the PermitRootLogin line and change it to no:
    PermitRootLogin no
  3. (Optional) Disable password authentication as well:
    PasswordAuthentication no
  4. Save and exit (CTRL + O, ENTER, CTRL + X).
  5. Validate SSH configuration syntax and restart the SSH daemon:
    sudo sshd -t
    sudo systemctl restart ssh
    # On Ubuntu 20.04/22.04:
    # sudo systemctl restart sshd
IMPORTANT TIP: Before closing your current terminal window, open a new terminal tab and verify that you can successfully log in with your new sudo user: ssh sammy@YOUR_SERVER_IP.

Frequently Asked Questions & Troubleshooting

Q: What is the difference between adduser and useradd?
A: adduser is an interactive, friendly Perl wrapper on Ubuntu that creates the home directory and sets passwords automatically. useradd is a low-level system binary that requires multiple manual flags.

Q: How can I remove a user if I no longer need it?
A: Run sudo deluser --remove-home username to delete the user and their home directory.

Q: How do I execute root commands once root SSH login is disabled?
A: Simply prefix administrative commands with sudo (e.g. sudo apt update) or open an interactive root shell when needed using sudo -i.


Need Further Assistance?

If you have questions about configuring sudo user privileges, securing SSH daemon settings, or managing user accounts on your Ubuntu VPS, our technical support team is available 24/7. Feel free to submit a support ticket through your client area for expert assistance.

Esta resposta foi útil? 0 Os usuários acharam isso útil (0 Votos)