In Linux, you can have multiple users working on the system simultaneously. That’s what qualifies Linux as a multi-user system. You can utilize the useradd commands in Linux to manage the system’s connected users. System administrators use this command frequently to create new users.
Having multiple users and managing their permissions properly will allow them to have private files on the system. You can also utilize user groups to further divide permissions among a set of users on the system.
In this tutorial, we will discuss the common applications of the useradd command and some of its options to help you best manage your system’s connected users and user groups.
In this tutorial you will learn:
- How to use the useradd command on Linux
|Requirements, Conventions or Software Version Used
|Any Linux distro
|Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
Frequently Used Options
useradd command in Linux Basic Examples
- The useradd command requires you to possess root permissions to create user accounts. Otherwise, you’ll receive a “permission denied” error. So, be sure to login to the root account or just prepend
sudoto your command.
$ sudo useradd user01
Adding a new user account to a Linux system with the useradd command is performed with specific settings. These settings can be viewed or altered in the
- Upon creating a new user, you will have to set a user password in order to log into this new account. We can achieve this by running the passwd command followed by name of the new user account. The
useraddcommand will make changes to the
/etc/gshadowfiles once the new user is created.
$ sudo passwd user01
- Depending on the Linux distribution, creating a new user account with the useradd command will not automatically create a new home directory for that user. You’ll have to create one manually after the fact. We can create a new home directory by passing the
-mcommand line option to the useradd commmand followed by the username. Alternatively, you can use the
--create-homecommand line option to create a user home directory.
$ sudo useradd -m user02
- Add the
-soption to your command to specify the default shell for the user. This is the shell they will be presented with every time they login.
$ sudo useradd -m user02 -s /bin/bash
You can always use the man command to read more about the useradd command and its official documentation. Click the previous link to see how to open the manual pages for any command on a Linux system.
All of the options for useradd allow us to modify certain settings of a user account upon its creation. Check some of the examples below to see more options.
useradd command in Linux Advanced Examples
- Once the home directory is created for the new user, the
useraddcommand will copy various files from the
/etc/skeldirectory. These files will be part of the initialization files for the new user’s home directory. These files can be viewed using the following syntax.
$ ls -la /home/user02
- By default, when creating a new user with the useradd command, the user will be given the next user identifier available from the
/etc/login.defsfile. But you can also use the useradd command to create a user ID of your choosing by running it with the
-ucommand line option.
$ useradd -u 4321 user03
- In Linux, groups are mainly used to effectively determine read, write, and execute permissions to multiple users at once. Every group in Linux carries a specific group ID. User IDs are very similar to group IDs, as one is used to identify a single user and another an entire group of users. So, there’s not much problem when trying to use the useradd command to create a specific group ID, just as we did with user IDs in the previous example. We can achieve this by invoking the
-gcommand line option.
$ sudo useradd -g users username
The process for assigning new group IDs and verifying them is the exact same as the one for user IDs.
- You can also utilize the useradd command with the
-ccommand line option to create a new user account with custom a comment. This comment can be anything, but it’s usually just the full name of the user or their contact information. Once a comment is created for a new user, it’ll be stored in the
/etc/passwdfile. You can view this comment with the grep command. We’ll show you an example of this in the screenshot, along with creating the new user below.
$ sudo useradd -c "Example Comment" user08
It’s important to note that in Linux, as can be observed in the screenshot above, you cannot type multiple words as one variable (like a custom comment) in a command line shell without wrapping it in
- You can also use the useradd command with the
-ecommand line option to create user accounts that will expire at a specified point in time. We can use this command to create temporary user accounts. The format for the specified date must be in YYYY-MM-DD.
$ sudo useradd -e 2022-02-14 user06
As you can see in the screenshot above, this date can always be viewed at any time with the chage command.
In this tutorial, we learned all about the useradd command on Linux. The useradd command is essential to master for users and administrators that frequently manage users and user groups in the Linux terminal.