Table of contents
Introduction
Welcome to our blog, today we'll be diving into the useradd command and explore its various options and syntax. Whether you're a beginner or a seasoned user, we'll make sure to cover everything in a friendly and approachable manner. So let's get started!
useradd
The useradd command in Linux is used to create a new user account. It allows you to specify various options and arguments to customize the user creation process. The basic syntax of the useradd command is as follows:
useradd <option> <argument> <username>
useradd username <option> <argument>
Now, let's take a closer look at some of the most commonly used options and their examples:
Options :
-u (userid): This option allows you to specify a specific user ID (UID) for the new user. For example, to create a user named "john" with the UID 1008, you would use the following command:
[root@192 Desktop]# useradd -u 1008 john
-g (groupid): With this option, you can assign a primary group ID (GID) for the new user. For instance, to create a user named "jane" and assign her the primary group ID 1008, you would use the following command:
[root@192 Desktop]# useradd -g 1008 jane
-c (comments): This option allows you to add a descriptive comment or information about the user. For example, to create a user named "sam" with the comment "System Administrator," you can use the following command:
[root@192 Desktop]# useradd -c "System Administration" sam
-d (home directory): This option lets you specify a custom home directory for the user. For instance, to create a user named "sushmita" with the home directory "/home/shush," you would use the following command:
[root@192 Desktop]# useradd -d /home/shush shushmita
-b (base directory): This option lets you specify a custom home directory for the user. For instance, to create a user named "saksham" with the base directory "/data/newdir," you would use the following command:
[root@192 Desktop]# useradd -b /data/newdir saksham
-s (login shell): Use this option to set the login shell for the new user. For example, to create a user named "mark" with the login shell "/bin/zsh," you can use the following command:
[root@192 Desktop]# useradd -s /bin/sh mark
-G (group assign): This option allows you to assign additional group memberships to the user. For instance, let's say you have an existing group called "developers" (GID 5222) and you want to add the user "alex" to that group. You can use the following command:
[root@192 Desktop]# useradd -G 5222 alex
-f (Inactive user): The -f option allows you to create a user which gets inactive after 10 days if password is not changed. An inactive user is unable to log in until their account is activated. For example, to create an inactive user named "inactiveuser," you can use the following command:
[root@192 Desktop]# useradd -f 10 inactiveuser
-e (Account expiry): With the -e option, you can set an expiration date for the user account. After the specified date, the user account will be automatically disabled. For instance, to create a user named "expiringuser" with an account that will expire on a specific date [31st July 2023 in our case], you would use the following command:
[root@192 Desktop]# useradd -e '31 July 2023' expiringuser
-P (Password - Not Recommended): The -P option allows you to assign a password to the user account during its creation. However, note that using this option is not recommended as the password will be visible in the command history and may pose a security risk. It's better to set the password separately using the passwd command. Nonetheless, if you still want to proceed, here's an example of how to create a user named "userwithpassword" with a specific password:
[root@192 Desktop]# useradd -P 'password' expiringuser
-r (Create system user): The -r option is used to create a system user account. System users are typically used for running system services and processes, rather than for interactive login. For example, to create a system user named "systemuser," you can use the following command:
[root@192 Desktop]# useradd -r systemuser
-o (Non-unique - Duplicate): The -o option allows you to create a user with a non-unique (duplicate) UID. This can be useful in certain scenarios where you need to create multiple users with the same UID. For instance, to create a duplicate user named "duplicateuser" with UID 1001, you would use the following command:
[root@192 Desktop]# useradd -o -u 1001 duplicateuser
Conclusion
Hope you learned a lot about user administration in Linux, thanks for being a part of the journey see you in the next one till then happy linuxing