How to Manage Users from the Command Line in Linux
In this article, we’ll show you how to manage users from the command line in Linux.
Create a New User
To create a new user from the command line, use the ‘adduser’ command followed by the desired username. For example, to create a user named ‘johndoe’, run the following command:
“` sudo adduser johndoe“`
This command will prompt you to enter a password for the new user and set various information, such as their full name and contact information. You can also skip this interactive process by adding additional options to the ‘adduser’ command, such as ‘-gecos “John Doe”‘ to set the user’s full name directly.
Delete a User
To delete a user from the system, use the ‘deluser’ command followed by the username. For example, to delete the user ‘johndoe’, run the following command:
“` sudo deluser johndoe“`
This command will remove the user’s home directory and any files or directories owned by the user. It will also remove the user from any group memberships.
Modify User Properties
To modify a user’s properties, such as their password, shell, or group membership, use the ‘usermod’ command followed by the desired options. For example, to change the shell of ‘johndoe’ to ‘/bin/bash’, run the following command:
“` sudo usermod -s /bin/bash johndoe“`
This command updates the user’s login shell to ‘/bin/bash’, which is the default shell for most Linux distributions. You can also use the ‘passwd’ command to change a user’s password directly, like this:
“` sudo passwd johndoe“`
This command prompts you to enter the user’s new password twice for confirmation.
View User Information
To view information about a user, such as their group memberships or home directory, use the ‘id’ or ‘finger’ command followed by the username. For example, to view the groups that ‘johndoe’ is a member of, run the following command:
“` id johndoe“`
This command displays the user’s UID, primary group ID, and a list of supplementary group IDs.
Alternatively, you can use the ‘finger’ command to view additional information about the user, such as their real name and contact information, like this:
“` finger johndoe“`
This command displays the user’s login name, real name, home directory, and login shell.
Conclusion
Managing users from the command line in Linux can be a quick and efficient way to perform various tasks, such as creating new users, modifying user properties, and viewing user information. By using the commands we’ve discussed in this article, you can accomplish these tasks easily and quickly.