Managing users in Ubuntu is a fundamental task to maintaining server security, organizing access control, and facilitating smooth operation within a multi-user environment.
Linux is a multi-user environment where two types of users are present: a superuser (root) and a regular user. Superuser is grated with unlimited administrative privileges where the regular user is granted specific permission and set restrictions to ensure unauthorized modification of the Ubuntu server. If you are a superuser, you can add, delete or modify users in the Ubuntu system and allocate users with proper permission.
In this blog post, I will cover how you can add users, set passwords for the user and update user attributes on Ubuntu 24.04.
Prerequisites
Before adding a new user in Ubuntu, you must meet the following requirements for creating a new user.
- You are a superuser, or you have root permission
- You have administrative privilege
- You have text editors like Nano or Vim
Understanding User Accounts in Ubuntu
Linux is a multi-user platform. Adding new users to the Linux system is important for maintaining the Ubuntu server. Before adding users in Ubuntu, you should know the basics of users in Linux.
User Types: Superuser and Regular Users
There are two types of users in Linux: the superuser (root) user and the regular user.
- Superuser (Root): Superuser in Linux is often called a root or sudo user. Superuser has unrestricted access to the system. Root users can modify the system anytime they want. Usually, few users have administrative privileges in a typical Linux system to ensure the proper security of the system.
- Regular Users: The user who doesn’t have root or administrative privileges is called a regular user. By default, new users are created as regular users. After adding a new as a regular user, you can add the necessary permission to them.
User Properties
When you add a new user in Ubuntu, you can define several user properties. Here is the reference table of the user properties.
Field | Description |
---|---|
Username | The username is a unique identifier for a user account. It is used to log in to the system and distinguish between users. |
User ID (UID) | The User ID is a numerical value associated with each user account. The UID helps the system identify and differentiate between users internally. Root has a UID of 0, while regular users are assigned non-zero UIDs. |
Group ID (GID) | Each user account is associated with a primary group identified by the Group ID (GID). The group allows users with similar access requirements to share common permissions and resources. |
Home Directory | A home directory is a dedicated space for each user on the system. It serves as the default location where a user’s personal files and configurations are stored. |
Shell | The shell is a command-line interface that provides an interactive environment for users to execute commands. It determines how users interact with the system and defines their command-line experience. |
Adding a New User With useradd Command
useradd
command is used to add a new user in Ubuntu. Here is the syntax of useradd
command
Syntax and Options: The basic syntax of usradd command
useradd [options] username
Common options for useradd
command include -u
to set a specific UID, -g
to assign a specific GID, -d
to specify the home directory and -s
to set the default shell.
Adding a User in Linux
Adding a user in Ubuntu with useradd
command is a fairly easy process. Type useradd
command followed by the username to add a new user.
sudo useradd Patrick
- The username must be limited to 32 characters
- The username must begin with Uppercase, Lowercase, or underscore.
- You can use a combination of uppercase, lowercase, and number
Setting Password For The User
Before using a new user account in Ubuntu, you need to create a password for the user. You can set a password with passwd
command.
sudo passwd Patrick
Remember to choose a strong password with upper and lower case letters to maximize system security.
Now that you have added a new your to the Ubuntu system, it is time to customize the new user, such as setting the home directory, setting up a default shell, or adding a group. In the next section, I’ll show how you can manage the attributes of newly added users in your Ubuntu System.
Managing User Attributes
usemod
command allows us to modify user attributes such as group, home address, shell, etc. Here is a reference table of available options for usermod
command.
Alternatively, you could use useradd
command to manage attributes. But with useradd
command, you need to define the user attribute when creating a new user, which is inconvenient.
Option | Description |
---|---|
-c, –comment | Allows you to add or modify the user’s comment field. |
-d, –home | Specifies the new home directory for the user. |
-s, –shell | Sets the user’s default shell to the specified one. |
-G, –groups | Modifies the user’s group memberships, adding the specified groups to the existing ones. |
-a, –append | Appends the user to the specified groups instead of replacing the current group memberships. |
-L, –lock | Locks the user account by placing a “!” in front of the encrypted password, disabling login. |
-U, –unlock | Unlocks a previously locked user account. |
Adding Users to Existing Groups
The group in Linux is a group of users who share the same permission. The group is very useful for managing user permission in Linux because It is easy to add permission to a group rather than to each user.
You can easily add a user to an existing group with usermod
command and -aG
option. For example, you want to add previously created user to the sysadmin group.
sudo usermod -aG sysadmin <username>
You can also add a user to multiple groups at a time. To add multiple groups, use the following command
sudo usermod -aG group1 group2 group3 <username>
Creating New Groups and Assigning Users
If you want to add a new user to a new group that still needs to be created, then you need to create the group before adding a user. To create a group, run the following command
sudo groupadd newgroup
Then you can add a new user to the group with usermod
command
sudo usermod -aG newgroup <username>
Add a Custom Comment to the User
You can add a custom comment to the newly created users. This is often useful to identify a specific user if you have multiple users in your Ubuntu System. To add a custom comment to new users, you can use usermod
command with -c
option.
usermod -c “The user should be deleted after 2 month” <username>
Disabling or Locking User Accounts
Sometimes, you may need to disable or lock user accounts for security or maintenance purposes. You can disable or lock user accounts with usermod
command with -L
options.
usermod -L <username>
Enabling or UnLocking User Accounts
You can unlock or enable a locked user account with usermod
command combined with -U
options.
usermod -U <username>
Set User’s Home Directory
By default /home/user
directory is set for new users, but you can change the default directory for the user with -d
option. For example, you want to set /var/user
as the home directory for a new user.
usermod -d /var/username <username>
Set User’s Shell
Bash shell is the default shell for new users, But you can change it to any shell you want with usermod
command and-s
option. For example, you want to assign zsh
shell for the user.
usermod -s zsh <username>
Conclusion
Adding and managing users is a fundamental task for system administrators. Adding a user with proper configuration and permission helps you secure your Ubuntu server. In this blog post, I’ve shown how you can add user in Ubuntu, set a password for the user and manage user attributes effectively with usermod
command. I use usermod
command instead of the useradd
command to manage user attributes conveniently.
Feel free to share your thoughts about this blog post in the comment section.