How to Create and Manage Users on Ubuntu: A Complete Guide to adduser, useradd, and sudo Permissions

目次

1. Introduction

Ubuntu is one of the most widely used Linux distributions and is highly popular as both a server environment and development platform. Among its features, user management is one of the most important aspects for maintaining system security and operational efficiency.

This article explains how to create users in Ubuntu and introduces both the GUI and Command Line Interface (CLI) methods in detail. It also covers how to grant sudo privileges to users and how to remove users when they are no longer needed.

By reading this guide, you will be able to manage users smoothly on Ubuntu and operate your system more securely and efficiently.

2. Creating and Managing Users with the GUI (Beginner-Friendly)

The simplest method for beginners who are not familiar with Linux is to use Ubuntu’s GUI (Graphical User Interface). If you are using a desktop environment, managing users through the GUI is intuitive and easy to understand.

2.1 Creating a New User via the GUI

  1. Open the Settings menu
  • Search for “Settings” from “Activities” in the upper-left corner of the screen and open it.
  • Click the “Users” section in the Settings menu.
  1. Add a user
  • Click the “Add User” button in the upper-right corner.
  • Select either “Administrator” or “Standard User”.
  • Enter the username, full name, and password.
  1. Complete the creation
  • Click “Add” and wait until the user is created.
  • The newly created user will appear in the list.

Key Points:

  • Standard users cannot modify critical system settings.
  • Administrator users have sudo privileges and can manage the system.

2.2 Assigning sudo Privileges via the GUI

If you want a user to have sudo privileges, simply enable the “Administrator” option when creating the user. To add sudo privileges to an existing account, follow these steps:

  1. Open “Users” from the Settings menu
  2. Select the user you want to modify
  3. Check “Administrator”
  4. Apply and save changes

The selected user will now have sudo privileges.

2.3 Deleting Users from the GUI

To remove a user that is no longer needed, follow the steps below:

  1. Open the “Users” section in Settings
  2. Select the user to delete
  3. Click the “Remove” button
  4. Choose whether to delete the user’s home directory
  5. Confirm the deletion

Note:

  • Deleting a user may also remove their home directory and data.
  • Back up important data beforehand if necessary.

3. Creating Users from the Command Line (CLI) for Intermediate and Advanced Users

In Ubuntu, using the command line allows you to configure users in greater detail. In server environments or when operating remotely, the CLI is often essential, making it beneficial to become familiar with Linux commands.

This section explains the differences between adduser and useradd, two major commands used to create users.

3.1 Creating Users with adduser

Basic Usage of adduser

The adduser command is a user-friendly, interactive tool that creates a new user account by simply entering requested information.

Steps

  1. Open the terminal (Ctrl + Alt + T or connect via SSH)
  2. Execute the following command
sudo adduser newusername
  1. Follow the on-screen prompts to enter required information
  • Set a password (required)
  • Full name and other details (optional)
  1. Confirm by entering “Y” when prompted

Execution Example

Adding user `testuser' ...
Adding new group `testuser' (1001) ...
Adding new user `testuser' (1001) with group `testuser' ...
Creating home directory `/home/testuser' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for testuser
Enter the new value, or press ENTER for the default
    Full Name []: Test User
Is the information correct? [Y/n] Y

3.2 Differences Between adduser and useradd

Ubuntu provides another command called useradd. It is a lower-level tool that creates users, but by default does not generate a home directory.

Basic Usage of useradd

sudo useradd -m -s /bin/bash newusername

Options:

  • -m : Automatically create a home directory
  • -s /bin/bash : Set Bash as the default shell

Points of Attention When Using useradd

  • No home directory by default → requires -m
  • No password set → configure via passwd
  • More advanced configuration required

Usage Comparison

CommandHome DirectoryPassword SetupRecommended Use
adduserAuto-createdConfigured interactivelyGeneral user creation
useraddNot created (-m needed)Requires passwdAdvanced configurations

adduser is recommended in most cases due to its simplicity.

4. Granting and Removing sudo Privileges

Ubuntu allows you to configure administrator (sudo-enabled) users. Users with sudo privileges can install software, modify configurations, and manage the system.

This section explains how to grant and remove sudo privileges.

4.1 Granting sudo Privileges

Method 1: Add the User to the sudo Group via usermod

  1. Open the terminal
  2. Execute the command below
sudo usermod -aG sudo username
  1. Log out and back in to apply changes
  2. Verify privileges
groups username

Method 2: Using gpasswd

sudo gpasswd -a username sudo

4.2 Removing sudo Privileges

Method 1: Using deluser

sudo deluser username sudo

Method 2: Using gpasswd

sudo gpasswd -d username sudo

Troubleshooting

groups username
dpkg -l | grep sudo
sudo apt update && sudo apt install sudo

4.3 sudo Security Tips

  • Do not grant sudo rights unnecessarily
  • Avoid working directly as root
  • Monitor sudo logs regularly
cat /var/log/auth.log | grep sudo

5. How to Delete Users

When removing a user in Ubuntu, you may also need to delete their home directory and manage associated files.

5.1 Deleting Users with deluser

sudo deluser username
$ sudo deluser testuser
Removing user `testuser' ...
Warning: group `testuser' has no more members.
Done.

This removes the user account but keeps the home directory.

5.2 Deleting the User and Home Directory

sudo deluser --remove-home username

5.3 Using userdel

sudo userdel username
sudo userdel -r username

5.4 Handling Remaining Files

sudo find / -uid $(id -u deleteduser) 2>/dev/null
sudo find / -uid $(id -u deleteduser) -exec rm -rf {} \;

6. Checking Users and Groups

6.1 Listing Existing Users

cat /etc/passwd
getent passwd
getent passwd username

6.2 Listing Groups

cat /etc/group
getent group sudo

6.3 Checking User Group Membership

groups username
id username

7. FAQ

7.1 Differences Between adduser and useradd

  • adduser is interactive, creates home directories, and sets passwords easily.
  • useradd is lower-level, requires options, and may require separate password setup.

7.2 How to Grant sudo Privileges?

sudo usermod -aG sudo username

7.3 What Happens if a sudo User Is Removed?

sudo deluser username sudo

7.4 Why Are Files Left After User Deletion?

sudo find / -uid $(id -u deleteduser) 2>/dev/null

8. Summary

This article has explained everything from basic to advanced Ubuntu user management, including user creation, sudo privilege configuration, deletion, and group administration.

8.1 Key Takeaways

1. User Creation

sudo adduser username

2. Granting sudo Privileges

sudo usermod -aG sudo username

3. Removing Users

sudo deluser username --remove-home

4. Checking Users and Groups

cat /etc/passwd
cat /etc/group

8.2 Best Practices for Efficient User Management

1️⃣ Regularly remove unused accounts
2️⃣ Keep sudo rights minimal
3️⃣ Monitor user activity through logs

cat /var/log/auth.log | grep sudo

4️⃣ Always back up important data

8.3 Final Thoughts

Proper user management in Ubuntu enhances both system security and operational efficiency. Use the knowledge provided in this article to maintain a well-organized and secure system.

侍エンジニア塾