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 Recommended Method:
Using adduser
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 usernameOnce you execute this command, the system will guide you through a series of prompts:
- Enter new UNIX password: Type a secure password for the new user (the characters will not show on the screen as you type).
- Retype new UNIX password: Confirm the password.
- User Information: The system will ask for optional details like Full Name, Room Number, Work Phone, Home Phone, and Other. You can fill these out or simply press Enter to leave them blank.
- Is the information correct? [Y/n]: Type
yand press Enter to finalize the creation.
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 usernameThe -m flag ensures the home directory is generated.
After creating the user, you must manually set their password by
running:
sudo passwd usernameGranting 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- The
-aflag stands for “append,” ensuring the user is added to the new group without being removed from their current groups. - The
-Gflag specifies the target group, which issudoon Ubuntu systems.
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 usernameTo test the login functionality and switch to the new user account immediately, execute:
su - usernameIf you granted the user sudo access, you can verify it
by running a test command with administrative privileges from within
their account:
sudo whoamiIf the setup was successful, the terminal will prompt for the user’s
password and output root.