How to Add a User in Ubuntu via Command Line?

Managing users is a fundamental task in Linux administration, and Ubuntu provides straightforward command-line tools to handle this efficiently. This article provides a quick overview and step-by-step guide on how to add a new user to an Ubuntu system using the terminal. It covers the two primary commands available, adduser and useradd, explains how to grant administrative privileges via the sudo group, and demonstrates how to verify that the new account was created successfully.

The adduser command is a user-friendly, interactive script written in Perl that sits on top of the low-level useradd tool. It is the highly recommended method for Ubuntu because it automatically creates a home directory, sets up default configuration files, and prompts you to create a password right away.

To add a new user, open your terminal and run the following command (replace username with the actual name of the new user):

sudo adduser username

Once you execute this command, the system will guide you through a series of prompts:

The Low-Level Alternative: Using useradd

If you are writing automation scripts or prefer a more manual, low-level utility, you can use the useradd command. Unlike adduser, this command does not automatically create a home directory or prompt for a password unless you explicitly include the correct flags.

To create a user with a home directory using useradd, run:

sudo useradd -m username

The -m flag ensures the home directory is generated. After creating the user, you must manually set their password by running:

sudo passwd username

Granting Sudo (Administrative) Privileges

By default, newly created users are standard users without administrative capabilities. If the new user needs to install software or make system changes, you must add them to the sudo group.

Run the following command to grant administrative access:

sudo usermod -aG sudo username

Verifying the New User

To ensure the account was successfully created and configured, you can check the system’s user database or try logging into the new account directly from the terminal.

To check the user’s information and group memberships, use the id command:

id username

To test the login functionality and switch to the new user account immediately, execute:

su - username

If you granted the user sudo access, you can verify it by running a test command with administrative privileges from within their account:

sudo whoami

If the setup was successful, the terminal will prompt for the user’s password and output root.