When using a VPS/server, one of the very first things you probably do is creating a new user. In this tutorial you will learn how to create and delete users, create and delete groups, assign a user to a group and more.

Table of Contents

Login as root or use “sudo” with the following commands.

Create a user

useradd -m <username>
# or
adduser <username>
  • useradd command will create a locked user with no password, you can assign them a password (and unlock the user) with:
    passwd <username>
    
# create a new user and add it to an existing group
adduser --ingroup <group> <username>

Create a group

groupadd <group-name>
# or
addgroup <group-name>

Assign an existing user to an existing group

usermod -aG <group-name> <username>
# or
adduser <username> <group-name>
  • Example: adding user “ricardo” to “sudo” group.
    usermod -aG sudo ricardo
    # or
    adduser ricardo sudo
    
  • In some distros, you may need to install “sudo”, create “sudo” group and run visudo (this command will modify /etc/sudoers file after checking for errors) to grant root permissions to “sudo” group (look for the following line and uncomment it) before adding user to “sudo” group. You may need to run EDITOR=nano (or your preferred editor) before running visudo.
    ## Uncomment to allow members of group sudo to execute any command
    %sudo   ALL=(ALL) ALL
    

Display groups a user belongs to

groups <user>

Display groups and their users

cat /etc/group

Remove a user from a group

# include all groups (using a comma to separate them) except the one you want to leave
usermod -G <group list> <username>

Delete a user

deluser <username>
# you can add '--remove-home' option to delete home directory or '--remove-all-files' to delete all files owned by user
userdel <username>
# you can add '--remove' to delete home directory

Delete a group

delgroup <group-name>
# or
groupdel <group-name>

If you have any suggestion, feel free to contact me via social media or email.