How to Create a User in Linux: Simple Command Guide
To create a new user in Linux, use the
useradd command followed by the username, like sudo useradd -m username. You can also add a password with passwd username to enable login for the new user.Syntax
The basic syntax to create a user is:
sudo useradd [options] username: Creates a new user with the specified username.sudo passwd username: Sets or changes the password for the user.
Common options include:
-m: Create the user's home directory.-s: Specify the user's login shell.-G: Add the user to additional groups.
bash
sudo useradd -m -s /bin/bash username sudo passwd username
Example
This example creates a user named alice with a home directory and bash shell, then sets her password.
bash
sudo useradd -m -s /bin/bash alice sudo passwd alice
Output
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Common Pitfalls
Common mistakes when creating users include:
- Not using
-moption, which means no home directory is created. - Forgetting to set a password, so the user cannot log in.
- Running commands without
sudo, causing permission errors.
Always check if the username already exists to avoid errors.
bash
sudo useradd bob # This creates user bob but no home directory sudo useradd -m bob # Correct way to create user bob with home directory
Quick Reference
| Command | Description |
|---|---|
| sudo useradd -m username | Create user with home directory |
| sudo passwd username | Set or change user password |
| sudo useradd -s /bin/bash username | Set user's login shell to bash |
| sudo useradd -G group1,group2 username | Add user to extra groups |
Key Takeaways
Use
sudo useradd -m username to create a user with a home directory.Always set a password with
sudo passwd username so the user can log in.Include
-s option to specify the user's shell if needed.Run commands with
sudo to avoid permission errors.Check if the username exists before creating a new user.