How do you add a new user to a machine running the Ubuntu operating system?

In this article, we'll go over how to create a new user on Ubuntu, along with some additional actions like adding your user to the sudo group and deleting a user.

Let's jump right into it!

Create New User

To create a new user, you can use the adduser command provided natively by Ubuntu. This command will add a user to your machine according to the command line options and configuration information in the /etc/adduser.conf file.

Here's how the command looks (replace bob with your user's name):

      sudo adduser bob

You'll be prompted to set the password for your new user and also confirm it by retyping the value:

      Enter new UNIX password:
        Retype new UNIX password:
        passwd: password updated successfully

Next, you'll be prompted to set some information for your new user. You can fill them out or just press ENTER repeatedly to skip each one.

      Changing the user information for bob
      Enter the new value, or press ENTER for the default
            Full Name []:
            Room Number []:
            Work Phone []:
            Home Phone []:
            Other []:
      Is the information correct? [Y/n]

Once that command finishes, your new user will be created!

If you want to switch over and use your new user, use the su - command:

      su - bob

You'll be asked to enter the password for your new user before you're given access.

Add User to the Sudo Group

Now that you have a new user, you can add it to the sudo group and, therefore, give it sudo privileges.

Use this command to add your user to the sudo group:

      sudo usermod -aG sudo bob

Your user is now part of the sudo group!

To run a command with root permissions, all you need to do is prepend the command with sudo.

Delete New User

In the scenario that you ever need to delete your user, you can use the userdel command native to Ubuntu. userdel is a low level utility for removing users that modifies the system account files, deleting any entry that refers to a given user name.

To delete a user, use this command (replace bob with your user's name):

      sudo userdel bob

If the username supplied doesn't exist, the command will fail.

Then, you may also want to delete the /home directory associated with the deleted user account:

      sudo rm -r /home/username

You can also achieve this by adding the -r or --remove flag to the userdel command. This will remove the user's home directory along with the home directory itself.

The command with the --remove flag attached would look like this:

      sudo userdel --remove bob

One caveat is that the userdel command will not allow you to delete a user if there are running processes that belong to that user. The -f or --force option can force the deletion of this account.

The command would look like this:

      sudo userdel --force bob

After using the userdel command, you should also manually go through your system files and verify that no files owned by the user remain.

Thanks for reading and happy coding!