Create user in Linux CLI

Creating a user in Linux Command Line Interface (CLI) is a simple task that can be accomplished using the “useradd” command. In this article, we will take a look at how to create a new user in Linux CLI, and also discuss some of the options that can be used with the “useradd” command.

Before we begin, it is important to note that in order to create a new user in Linux, you must have root or superuser privileges. This means that you will need to log in as the root user or use the “sudo” command to execute the “useradd” command.

Creating a New User

The basic syntax for the “useradd” command is as follows:

useradd [options] username

Where “username” is the name of the user you want to create. For example, to create a new user named “john”, you would use the following command:

useradd john

This will create a new user with the username “john”, and will assign a default home directory and shell to the user. The home directory will be located in the “/home” directory and the default shell will be “bash”.

Options for the “useradd” Command

There are a number of options that can be used with the “useradd” command to customize the new user account. Some of the most commonly used options include:

-d: This option is used to specify the home directory for the new user. For example, if you want to create a new user with a home directory located in the “/home/john” directory, you would use the following command:

useradd -d /home/john john

-m: This option is used to create the home directory for the new user. This is useful if you want to create a new user and their home directory at the same time. For example, to create a new user named “jane” with a home directory located in the “/home/jane” directory, you would use the following command:

useradd -m -d /home/jane jane

-s: This option is used to specify the shell for the new user. For example, if you want to create a new user with the “bash” shell, you would use the following command:

useradd -s /bin/bash john

-g: This option is used to specify the primary group for the new user. For example, if you want to create a new user and assign them to the “users” group, you would use the following command:

useradd -g users john

-G: This option is used to specify additional groups for the new user. For example, if you want to create a new user and assign them to the “users” and “admin” groups, you would use the following command:

useradd -G users,admin john

-u: This option is used to specify the user ID (UID) for the new user. For example, if you want to create a new user with a UID of 1000, you would use the following command:

useradd -u 1000 john

-e: This option is used to specify the expiration date for the new user. For example, if you want to create a new user that expires on December 31, 2022, you would use the following command:

useradd -e 2022-12-31 john

-p: This option is used to specify the password for the new user. For example, if you want to create a new user with the password “password123”, you would use the following command:

useradd -p password123 john

Please note that it is not recommended to use this option as it will store the password in plain text in the command history and can be a security risk. It is better to use the “passwd” command to set the password for the new user after the account has been created.

Managing Users in Linux CLI

Once you have created a new user in Linux CLI, there are a few commands that you can use to manage and modify the user account.

The “usermod” command can be used to modify an existing user account. For example, to change the home directory of the user “john” to “/home/john2”, you would use the following command:

usermod -d /home/john2 john

The “userdel” command can be used to delete an existing user account. For example, to delete the user “john”, you would use the following command:

userdel john

Please note that when you use the “userdel” command, it will also delete the user’s home directory and any files in it. If you want to keep the user’s files, you can use the “-r” option with the “userdel” command.

The “passwd” command can be used to change the password for a user. For example, to change the password for the user “john”, you would use the following command:

passwd john

This will prompt you to enter the new password for the user.

Conclusion

In conclusion, creating a user in Linux CLI is a simple task that can be accomplished using the “useradd” command. With the options provided by the command, you can customize the user account to your liking. Additionally, managing and modifying existing user accounts is also easy to do with commands such as “usermod” and “userdel”. As always, be sure to use these commands with caution, especially when working with the root or superuser account.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Related Articles