Complete Guide to Creating and Managing Users in Ubuntu | Configuring, Removing Sudo Privileges & Group Management

目次

1. Introduction

Ubuntu is one of the most widely used Linux distributions, popular for both servers and development environments. Among its key aspects, user management plays a crucial role in system security and administration.

This article provides a detailed guide on how to create users in Ubuntu using both GUI and command-line (CLI) methods. Additionally, it covers how to grant and remove sudo privileges, as well as how to delete unnecessary users.

By following this guide, you will be able to manage Ubuntu users efficiently, ensuring a secure and well-organized system.

2. Creating and Managing Users via GUI (For Beginners)

For beginners unfamiliar with Linux, the easiest way to create users is by using Ubuntu’s GUI (Graphical User Interface). If you are using a desktop environment, GUI-based management is intuitive and highly recommended.

2.1 Creating a New User via GUI

  1. Open the Settings Menu
  • Click “Activities” in the top-left corner, search for “Settings,” and open it.
  • In the Settings menu, select the “Users” section.
  1. Add a New User
  • Click the “Add User” button in the top-right corner.
  • Select either “Administrator” or “Standard User.”
  • Enter the username, full name, and password.
  1. Complete the Setup
  • Click the “Add” button and wait for the user to be created.
  • The newly created user will appear in the list.

Key Points:

  • Standard Users cannot modify important system settings.
  • Administrator Users have sudo privileges and can manage the system.

2.2 Granting Sudo Privileges via GUI

If you want to create a user with sudo privileges, simply enable the “Administrator” option during the setup. However, if you need to grant sudo privileges to an existing user, follow these steps:

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

The selected user will now have sudo privileges.

2.3 Deleting a User via GUI

To remove an unnecessary user, follow these steps:

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

Note:

  • Deleting a user may also delete their home directory data, so proceed with caution.
  • It is recommended to back up important data before deletion.
年収訴求

3. Creating Users via Command Line (For Intermediate and Advanced Users)

In Ubuntu, using the command line allows for more detailed user configuration. For server management or remote operations, CLI-based user management is often essential.

This section explains the differences and usage of the main commands for creating users: adduser and useradd.

3.1 Creating a User with the adduser Command

Basic Usage of the adduser Command

In Ubuntu, the adduser command allows you to easily create a new user. This command is interactive, meaning it guides you through the setup step by step.

Steps

  1. Open a terminal (Ctrl + Alt + T or connect via SSH)
  2. Run the following command:
   sudo adduser new-username
  1. Follow the on-screen instructions to enter required information:
  • Set a password (required)
  • Enter full name, phone number, etc. (optional)
  1. Finally, when prompted “Is the information correct? [Y/n]”, confirm by pressing “Y”.

Example Output

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
    Room Number []:
    Work Phone []:
    Home Phone []:
    Other []:
Is the information correct? [Y/n] Y

3.2 Differences Between adduser and useradd

Ubuntu also provides the useradd command, which is a lower-level alternative to adduser. However, by default, useradd does not create a home directory.

Basic Usage of the useradd Command

To create a new user using useradd, run the following command:

sudo useradd -m -s /bin/bash new-username

Options explained:

  • -m: Automatically creates a home directory
  • -s /bin/bash: Sets the default shell to Bash

Important Notes When Using useradd

  • Home directory is not created by default → Use the -m option
  • Password is not set by default → Use the passwd command to set a password
  • More manual configuration is required compared to adduser

Comparison Table: adduser vs. useradd

CommandHome DirectoryPassword SetupRecommended Use
adduserAutomatically createdCan be set during setupGeneral user creation
useraddNot created (requires -m)Needs to be set separatelyAdvanced management

For most cases, adduser is recommended as it is easier to use.

4. Granting and Removing Sudo Privileges

In Ubuntu, you can assign a special role to certain users as administrators (sudo users).
A sudo user has the privilege to perform critical system changes, such as installing software, modifying system settings, and managing other users.

This section explains how to grant and remove sudo privileges and provides step-by-step instructions for properly configuring administrator users.

4.1 How to Grant Sudo Privileges

Method 1: Add the User to the Sudo Group Using usermod

The simplest way to grant a new user sudo privileges is to use the usermod command.

Steps

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

If sudo appears in the output, the user now has sudo privileges.

Method 2: Using gpasswd Command

You can also use the gpasswd command to add a user to the sudo group.

sudo gpasswd -a username sudo

This method achieves the same result as usermod.

4.2 How to Remove Sudo Privileges

Method 1: Using deluser Command

To remove a user from the sudo group, use the deluser command:

sudo deluser username sudo

After running this command, the user will no longer have administrative privileges.

Method 2: Removing the User from the Group Using gpasswd

The gpasswd command can also be used to remove a user from the sudo group:

sudo gpasswd -d username sudo

Troubleshooting Sudo Privileges

  1. Verify if the user is in the sudo group
groups username
  1. Log out and log back in after changes
  2. Ensure the sudo package is installed
dpkg -l | grep sudo

If sudo is not installed, install it using:

sudo apt update && sudo apt install sudo

4.3 Security Considerations for Sudo Privileges

  • Do not grant sudo privileges to unnecessary users
  • Avoid performing tasks as the root user when possible
  • Regularly check sudo activity logs
cat /var/log/auth.log | grep sudo

Monitoring logs helps detect unauthorized use of sudo commands.

5. How to Delete a User

When removing a user in Ubuntu, it’s important to not only delete the account but also manage home directory deletion and group assignments properly.
This section explains how to delete users using the deluser and userdel commands and highlights important considerations.

5.1 Deleting a User with the deluser Command

To delete a specific user, run the following command:

sudo deluser username

Example Output

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

This command removes the user account, but does not delete the home directory.

5.2 Deleting the Home Directory as Well

If you also want to remove the home directory, use:

sudo deluser --remove-home username

Example Output

$ sudo deluser --remove-home testuser
Removing user `testuser' ...
Removing home directory `/home/testuser' ...
Done.

🚨 Warning:
Deleted data cannot be recovered. Make sure to back up important files before deletion.

tar -czf /backup/testuser_backup.tar.gz /home/testuser

5.3 Deleting a User with the userdel Command

To delete a user using userdel, run:

sudo userdel username

To delete the user along with their home directory, use the -r option:

sudo userdel -r username

5.4 Managing Remaining Files After User Deletion

To check for files owned by a deleted user, run:

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

To remove unnecessary files, execute:

sudo find / -uid $(id -u deleted-username) -exec rm -rf {} ;

🚨 Warning: Ensure that you review the files before deletion to avoid accidental removal of critical system files.

6. Checking Users and Groups

In Ubuntu, it is essential to regularly check existing users and groups.
Understanding which users exist and their group memberships helps ensure proper permission management.

6.1 Checking the List of Existing Users

Method 1: View the /etc/passwd File

The /etc/passwd file stores registered user account information.

cat /etc/passwd

Example Output

root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
testuser:x:1001:1001:Test User,,,:/home/testuser:/bin/bash

Method 2: Use the getent Command

getent passwd

To check a specific user:

getent passwd testuser

6.2 Checking the List of Groups

Method 1: View the /etc/group File

cat /etc/group

Method 2: Check Members of a Specific Group

getent group sudo

Example Output

sudo:x:27:user1,user2,testuser

6.3 Checking a User’s Group Membership

groups username

Example Output

testuser : testuser sudo developers

7. Frequently Asked Questions (FAQ)

Managing users in Ubuntu can be challenging for beginners. This section covers frequently asked questions (FAQ) to clarify common doubts regarding user creation, sudo privileges, deletion, and group management.

7.1 What is the difference between adduser and useradd?

Features of adduser

  • User-friendly interactive process
  • Automatically creates a home directory
  • Allows password setup during creation

Features of useradd

  • A lower-level command
  • Does not create a home directory by default (requires -m)
  • Password must be set separately

Which one should I use?
For standard user creation, adduser is recommended.
useradd is useful when scripting bulk user creation or requiring more granular control.

7.2 How do I grant sudo privileges to a user?

sudo usermod -aG sudo username

To apply changes, the user must log out and log back in.

7.3 What happens if I remove a sudo user?

sudo deluser username sudo

If all sudo users are removed, administrative privileges will be lost, restricting system access.

7.4 Why are files still present after deleting a user?

To check for remaining files owned by a deleted user, run:

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

To delete the files:

sudo find / -uid $(id -u deleted-username) -exec rm -rf {} ;

8. Summary

This article has provided a comprehensive guide on Ubuntu user management, covering user creation, sudo privileges, deletion, and group management.

8.1 Key Takeaways

1. User Creation

✅ GUI (For beginners): Go to “Settings” → “Users” → “Add” for easy creation.
✅ CLI (For intermediate/advanced users):

sudo adduser username

2. Granting Sudo Privileges

sudo usermod -aG sudo username

3. User Deletion

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 review and remove unnecessary users
2️⃣ Minimize sudo privileges to essential users
3️⃣ Monitor user activities using logs

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

4️⃣ Ensure proper backups before making changes

tar -czf /backup/username_backup.tar.gz /home/username

8.3 Final Thoughts

By properly managing users in Ubuntu, you can enhance system security and efficiency. Use this guide to apply best practices and manage users effectively.