Skip to main content

Command Palette

Search for a command to run...

How to Add User in Linux

Updated
6 min read

Adding a new user in Linux is a common task that you might need to do whether you're managing a personal computer or a large server. If you're new to Linux or just want to understand the best way to add users, this guide will walk you through the process clearly and simply. You’ll learn how to create users, set passwords, and manage user permissions.

We’ll cover everything from basic commands to some useful tips that make user management easier. By the end, you’ll feel confident adding users on any Linux system, whether it’s Ubuntu, CentOS, or another distribution.

Understanding Linux User Accounts

Linux is a multi-user operating system, which means it supports multiple users working on the same machine. Each user has a unique account with specific permissions and settings.

  • Users have a username and a user ID (UID).
  • Each user belongs to one or more groups.
  • User accounts help control access to files and system resources.
  • The root user is the superuser with full system control.

When you add a user, Linux creates a home directory for them, sets up default permissions, and allows you to assign passwords and groups. This keeps your system organized and secure.

Basic Command to Add a User

The most common command to add a user in Linux is useradd. It’s a simple tool that creates a new user account but requires some options to work fully.

Here’s the basic syntax:

sudo useradd username

However, this command alone doesn’t set a password or create a home directory by default on all systems. To add a user with a home directory and set a password, follow these steps:

  1. Create the user with a home directory:
sudo useradd -m username
  1. Set the user’s password:
sudo passwd username

You will be prompted to enter and confirm the new password.

What Does the -m Option Do?

The -m option tells Linux to create a home directory for the user at /home/username. This directory stores personal files and settings for that user.

Adding a User with More Options

Sometimes you want to customize the user account further. The useradd command supports several options:

  • -c "Comment": Add a description or full name.
  • -d /path/to/home: Specify a custom home directory.
  • -s /bin/bash: Set the default shell (e.g., bash, zsh).
  • -G group1,group2: Add the user to additional groups.

Example:

sudo useradd -m -c "John Doe" -s /bin/bash -G sudo,developers johndoe

This command creates a user named johndoe with a home directory, sets the shell to bash, adds a comment, and includes the user in the sudo and developers groups.

Using adduser for a Friendlier Experience

Many Linux distributions provide the adduser command, which is a more user-friendly script that simplifies adding users.

Just run:

sudo adduser username

This command will:

  • Create the user and home directory.
  • Prompt you to enter a password.
  • Ask for additional information like full name and phone number.
  • Set default permissions automatically.

adduser is often preferred by beginners because it guides you through the process interactively.

Managing User Groups

Groups in Linux help manage permissions for multiple users. When you add a user, you can specify which groups they belong to.

  • The primary group is usually the same as the username.
  • Secondary groups grant additional permissions.

To add a user to existing groups, use:

sudo usermod -aG groupname username

For example, to add johndoe to the docker group:

sudo usermod -aG docker johndoe

The -aG option appends the user to the group without removing them from other groups.

Checking User Information

After adding a user, you might want to verify their details.

  • To see user info:
id username

This shows the user ID, primary group, and supplementary groups.

  • To view the user’s home directory and shell, check the /etc/passwd file:
grep username /etc/passwd

The output includes the username, UID, GID, home directory, and default shell.

Deleting a User

If you need to remove a user, use the userdel command.

Basic syntax:

sudo userdel username

To delete the user and their home directory:

sudo userdel -r username

Be careful with this command because it permanently deletes user files.

Best Practices When Adding Users

When managing users on Linux, keep these tips in mind:

  • Always create a strong password for new users.
  • Use groups to manage permissions efficiently.
  • Avoid giving users root privileges unless necessary.
  • Regularly review user accounts and remove inactive ones.
  • Use adduser for ease, but useradd for scripting and automation.

Troubleshooting Common Issues

Sometimes adding users can run into problems. Here are common issues and fixes:

  • Home directory not created: Use the -m option with useradd.
  • User cannot log in: Check the shell is valid and not set to /usr/sbin/nologin.
  • Permission denied: Ensure you run commands with sudo or as root.
  • Password not set: Use passwd to assign a password after creating the user.

Automating User Creation with Scripts

If you manage many users, scripting user creation saves time.

Example bash script to add multiple users:

#!/bin/bash
for user in alice bob charlie
do
  sudo adduser --disabled-password --gecos "" $user
  echo "$user:password123" | sudo chpasswd
done

This script creates users alice, bob, and charlie with the password password123. Adjust as needed for your environment.

Summary Table of Commands

CommandPurposeExample
sudo useradd -m usernameAdd user with home directorysudo useradd -m alice
sudo passwd usernameSet user passwordsudo passwd alice
sudo adduser usernameInteractive user creationsudo adduser bob
sudo usermod -aG group userAdd user to groupssudo usermod -aG sudo alice
sudo userdel -r usernameDelete user and home directorysudo userdel -r bob
id usernameShow user infoid alice

Conclusion

Adding a user in Linux is straightforward once you know the right commands. Whether you use useradd for precise control or adduser for an interactive experience, you can quickly create accounts, set passwords, and manage groups. Remember to follow best practices like using strong passwords and limiting root access to keep your system secure.

With these tools and tips, you can confidently manage users on any Linux system. Practice these commands, and soon adding and managing users will become second nature.

FAQs

How do I add a user with a specific home directory?

Use the -d option with useradd. For example: sudo useradd -m -d /custom/path username creates a user with a home directory at /custom/path.

Can I add a user without a password?

Yes, but it’s not recommended. Use sudo useradd -m username and skip setting a password, but the user won’t be able to log in until a password is set.

How do I add a user to multiple groups?

Use the -G option with a comma-separated list: sudo useradd -m -G group1,group2 username.

What’s the difference between useradd and adduser?

useradd is a low-level command requiring options, while adduser is a friendly script that guides you through user creation interactively.

How do I check which groups a user belongs to?

Run id username to see the user’s UID, primary group, and supplementary groups.

More from this blog

L

LinuxBloke | Linux Tips, Tricks & Troubleshooting

672 posts