Skip to content

Adding a New User

In this article

Adding users in Linux is a fundamental system administration task.
For security reasons, it is not recommended to work in Linux directly as the root user, as even a minor mistake in a command can lead to irreversible damage to the system or critical files.
A safer and more appropriate approach is to create a separate regular user (for example, vdsuser) and grant them the ability to run administrative commands via sudo. This allows root‑level tasks to be performed only when necessary, maintaining protection against accidental or malicious actions.

Creating a sudo User

There are several ways to add a user, but the most common commands are useradd and adduser.

  1. Connect to your server via SSH

    ssh root@ip_address
    
  2. Create a new user

    adduser vdsuser
    

    On Ubuntu/Debian, you will then be prompted to set the user’s password immediately. Enter it twice; note that the characters are not displayed as you type.

    On CentOS / AlmaLinux / Rocky Linux, the adduser command works as useradd and does not prompt for a password. To set the password, run:

    passwd vdsuser
    

    Then, on Ubuntu/Debian, provide additional user information (you can leave fields blank).

  3. Grant the user sudo privileges by adding them to the sudo group

    On Ubuntu/Debian, run:

    usermod -aG sudo vdsuser
    

    On CentOS / AlmaLinux / Rocky Linux, run:

    usermod -aG wheel vdsuser
    
  4. Verify that the user can use sudo

    Switch to the new user’s session:

    su - vdsuser
    

    Run a command with sudo privileges (for example, list the /root directory):

    sudo ls -la /root
    

    If the directory contents appear, the configuration is correct.

Adding a User Using an SSH Key

If your server uses an SSH key, password-based login is disabled. To allow a new user to log in successfully, copy your local public key (~/.ssh/authorized_keys) into the new user’s account.

  1. Copy the entire .ssh directory into the new user’s home

    cp -r ~/.ssh /home/vdsuser
    
  2. Set vdsuser as the owner of that directory using chown

    chown -R vdsuser:vdsuser /home/vdsuser/.ssh
    
  3. After completing the above steps, connect to the server using the SSH key

    ssh <vdsuser>@<ip_address>
    

Note

For Added Security, Disable Remote Access for the Root User

  • Open the file /etc/ssh/sshd_config, for example in the nano editor: sudo nano /etc/ssh/sshd_config.
  • Change the PermitRootLogin setting to no. In modern distributions (Ubuntu 22.04+, Debian 12+, AlmaLinux / Rocky Linux 9+), also check the files in the /etc/ssh/sshd_config.d/ directory — values set there may override the main file.
  • Press Ctrl‑x, then y, then Enter to save changes and exit the editor.
  • Restart the SSH service:

On Ubuntu/Debian:

sudo systemctl restart ssh
On CentOS / AlmaLinux / Rocky Linux:

sudo systemctl restart sshd

Before closing the current session, make sure you can log in to the server as the new user in a separate terminal window.

Useful User Management Commands

Basic User Management Commands

  • Get user information

    id username
    

  • Change a user’s password

    passwd username
    

  • Add a user to a supplementary group (the -a flag is required — without it, the user will be removed from all other supplementary groups)

    usermod -aG group_name username
    

  • Change a user’s primary group

    usermod -g group_name username
    

  • Change a user’s home directory (moving its contents)

    usermod -d /new/home/directory -m username
    

  • View all created users

    cat /etc/passwd
    

  • List users currently logged in and their activity

    w
    

  • Delete a user

    deluser username          # Ubuntu / Debian
    userdel username          # all distributions, including CentOS / AlmaLinux / Rocky Linux
    userdel -r username       # delete together with the home directory
    

Basic Group Management Commands

  • Create a group

    groupadd group_name
    

  • Delete a group

    delgroup group_name       # Ubuntu / Debian
    groupdel group_name       # all distributions
    

question_mark
Is there anything I can help you with?
question_mark
AI Assistant ×