When deploying an Ubuntu Linux server or setting up a desktop for multiple developers, proper user management is critical for security. Using the “root” account for daily tasks is highly dangerous, as a single typo can destroy the operating system. Instead, you must create individual user accounts and manage their permissions using groups.
In this guide, you will learn the essential terminal commands for creating users, assigning them to groups, and managing sudo privileges on Ubuntu Linux.
Prerequisites
To execute these commands, you must either be logged in as the `root` user or be logged in as a standard user with `sudo` privileges. If you are using a standard user, remember to prefix the administrative commands with sudo.
1. Creating a New User Account
There are two commands for adding users in Ubuntu: `useradd` and `adduser`. Always use adduser. It is a friendly, interactive script that automatically creates the user’s home directory, sets up default configuration files, and prompts you for a password.
To create a new user named “jane”:
sudo adduser jane
The terminal will ask you to:
- Enter and confirm a strong password for the new user.
- Provide optional user information (Full Name, Room Number, etc.). You can just press Enter to skip these.
- Confirm the information is correct by typing
Y.
The user “jane” can now log into the system, but she will be a standard user with limited permissions.
2. Granting Administrative (sudo) Privileges
If the new user needs to perform administrative tasks (like installing software or editing system files), they must be granted `sudo` access.
In Ubuntu, sudo privileges are managed via a special group called the sudo group. Anyone added to this group can run administrative commands.
To grant “jane” sudo privileges, use the usermod command with the -aG flags (Append to Group):
sudo usermod -aG sudo jane
The next time Jane logs in, she will be able to execute commands with `sudo`.
3. Creating and Managing Custom Groups
For better security, you often want users to have access to specific folders without giving them full root access. For example, you might have a “developers” group that has access to the `/var/www` web directory.
Creating a New Group
To create a group named “developers”:
sudo groupadd developers
Adding a User to the Group
Just as you added Jane to the sudo group, use the `usermod` command to add her to the developers group:
sudo usermod -aG developers jane
Viewing a User’s Groups
To verify which groups a user belongs to, simply type the groups command followed by their username:
groups jane
Output: jane : jane sudo developers
4. Deleting Users and Groups
When an employee leaves or an account is no longer needed, you must remove their access promptly to maintain system security.
Deleting a User
To delete the user account “jane”:
sudo deluser jane
Important Note: The basic deluser command removes the account but leaves the user’s files intact in their home directory (e.g., `/home/jane`). If you want to delete the user and securely wipe their home directory and files simultaneously, use the --remove-home flag:
sudo deluser --remove-home jane
Deleting a Group
If the “developers” group is no longer required, you can remove it using:
sudo delgroup developers
This does not delete the users who were inside the group, it merely removes the group structure itself.
By mastering these fundamental user management commands, you ensure your Ubuntu server adheres to the principle of least privilege, drastically reducing the risk of accidental damage or security breaches.