How To Add User And Group In Linux Using Command Line Tools

Essential User and Group Management for Every Linux Administrator

You’ve just set up a new Ubuntu server for your web application, or perhaps you’re configuring a Rocky Linux workstation for a new team member. The first task staring you back from the terminal is foundational: you need to create user accounts. Maybe you need to grant your developer, Sarah, access to the deployment directory, or set up a service account for your database.

Managing users and groups isn’t just a one-time setup chore; it’s the bedrock of system security, resource control, and multi-user collaboration on any Linux system. Getting it wrong can lead to permission headaches, security vulnerabilities, or team members locked out of critical files.

This guide walks you through the precise commands and concepts you’ll use daily, from the basic useradd to advanced group permissions, complete with troubleshooting for the common pitfalls that trip up even seasoned sysadmins.

Understanding the Linux User and Group Model

Before typing any commands, it’s crucial to grasp what you’re actually managing. In Linux, every process and file is owned by a user and a group. This ownership dictates who can read, write, or execute.

The superuser, root, has ultimate power. Everyday users operate with limited privileges, a principle that confines damage from mistakes or breaches. Groups are collections of users, a logical way to grant shared access to files, directories, or applications without managing permissions for each individual.

Key system files, /etc/passwd and /etc/group, store user and group information respectively. While you can edit these files directly with vipw or vigr, it’s dangerous and error-prone. The dedicated command-line tools handle the details safely, locking files and maintaining consistency.

Prerequisites and Getting Ready

To follow along, you’ll need access to a Linux terminal. Most commands require superuser privileges. You’ll typically use sudo before commands or switch to the root user with su.

Always have a backup plan. When modifying user accounts, ensure you have another working administrative account or console access. A typo while editing the sudoers file or removing your own account can lock you out.

Verify your current user and permissions first. The id command shows your user ID (UID), group ID (GID), and all groups you belong to.

Adding a New User to Your System

The primary tool for creating a standard user account is useradd. Its companion, adduser, is a friendlier, interactive script available on Debian/Ubuntu systems. We’ll cover both.

Using the useradd Command

The useradd command is the standard, portable utility. At its simplest, creating a user named “john” is straightforward.

sudo useradd john

This command creates the user “john” with default settings: a UID picked from the system range (typically 1000+), a primary group named “john” created automatically, a home directory at /home/john, and the default shell from /etc/default/useradd, usually /bin/bash.

However, the bare command doesn’t set a password or create the home directory. You must do that separately.

sudo passwd john

You’ll be prompted to enter and confirm a new password. For a more complete setup in one go, use common options.

sudo useradd -m -s /bin/bash john

The -m flag ensures the home directory (/home/john) is created. The -s flag sets the login shell. To specify a custom home directory path, use -d /path/to/home. To add a comment (like a full name), use -c “John Doe”.

Using the adduser Command (Debian/Ubuntu)

On Debian-based distributions, adduser is a Perl script that interacts with you, simplifying the process.

sudo adduser john

The script will prompt you for a password, full name, room number, and other details. It creates the home directory, copies skeleton files from /etc/skel, and sets up the group automatically. It’s generally the safer, more intuitive choice for interactive use.

Verifying the User Creation

After creation, verify the account details. The id command is your first stop.

id john

This outputs the UID, GID, and group memberships. Check the /etc/passwd file to see the user’s entry.

grep john /etc/passwd

An entry looks like: john:x:1001:1001:John Doe:/home/john:/bin/bash. The fields are: username, password placeholder (x means the hash is in /etc/shadow), UID, GID, comment, home directory, and shell.

Finally, test logging in as the new user, either via su or SSH if configured.

how to add user and group in linux

su – john

Creating and Managing Groups

Groups are the mechanism for shared access. The command to create a new group is groupadd.

sudo groupadd developers

This creates a group named “developers” with the next available GID. To specify a custom GID, use the -g flag, like -g 2005. Verify the group exists with grep developers /etc/group.

Groups are useless without members. To add an existing user to a supplementary group, use the usermod command.

sudo usermod -aG developers john

The -a flag is critical. It means “append.” Without it, the command replaces the user’s entire supplementary group list, potentially removing them from other necessary groups. The -G flag specifies the group(s) to add. You can add multiple groups at once by separating them with commas.

To make a user’s primary group different from their default, use the -g flag with usermod. This changes the GID in /etc/passwd.

sudo usermod -g developers john

Be cautious: this changes the primary group ownership of all new files the user creates. It can also affect file access if permissions are set for the primary group only.

Setting Up Shared Directory Permissions

Creating a group is only half the battle. To use it for collaboration, you need a directory with the correct permissions.

First, create a directory and change its group ownership.

sudo mkdir /srv/app_deploy

sudo chgrp developers /srv/app_deploy

Next, set the directory permissions to allow group members to read, write, and execute (enter) the directory.

sudo chmod 2770 /srv/app_deploy

The mode 2770 breaks down: 2 sets the setgid bit, 7 for owner (rwx), 7 for group (rwx), 0 for others (no access). The setgid bit is crucial: it ensures any new file or subdirectory created within inherits the group ownership (developers), not the creator’s primary group.

Now, any user in the “developers” group can collaborate in /srv/app_deploy seamlessly.

Advanced User Management Operations

Beyond creation, you’ll often need to modify, deactivate, or remove accounts.

Modifying User Properties

The usermod command is your Swiss Army knife. To change a user’s login name, use -l.

sudo usermod -l jdoe john

This renames the user from “john” to “jdoe”. Note: the home directory name does not change automatically; you must rename it with mv and update the home directory reference with -d.

To lock an account, preventing password-based login, use -L.

sudo usermod -L jdoe

This places an exclamation mark in front of the password hash in /etc/shadow. To unlock it, use -U.

To set an account expiration date, use -e YYYY-MM-DD. This is useful for temporary contractors.

how to add user and group in linux

Deleting Users and Groups

Removing a user requires care. The userdel command has two main modes.

sudo userdel jdoe

This removes the user from /etc/passwd and /etc/shadow but leaves the home directory intact. To remove the home directory and mail spool as well, use the -r flag.

sudo userdel -r jdoe

Warning: This is irreversible. Ensure you have backups of any needed data from /home/jdoe and /var/mail/jdoe.

To delete a group, use groupdel. You cannot delete a group that is the primary group of any existing user. You must change those users’ primary GIDs first or delete the users.

sudo groupdel developers

Troubleshooting Common Permission and Access Issues

Even with correct commands, things can go wrong. Here’s how to diagnose and fix common problems.

A new user cannot log in. Check if the account is locked (usermod -L), has an expired password (chage -l username), or if the shell is set to /bin/false or /sbin/nologin. Verify the home directory exists and has correct ownership (user:user) and permissions (drwx——).

A user in a group cannot write to a shared directory. First, confirm group membership. The groups command or id shows active groups, but changes require the user to log out and back in. Check directory permissions with ls -ld. Ensure the group has write (w) permission. Crucially, verify the setgid bit (drwxrws—) is set if inheritance is needed.

The useradd command fails with “user already exists” or “group already exists.” Check /etc/passwd and /etc/group. The error might point to a UID/GID conflict. Use useradd -u to specify a unique UID.

Password authentication fails despite a correct password. Check /etc/shadow permissions (should be 640) and ownership (root:shadow). Ensure PAM configuration isn’t blocking the login method.

Automating User Creation with Scripts

For provisioning multiple users, such as in a classroom or corporate onboarding, a shell script saves time. Here’s a basic template.

#!/bin/bash

USER_LIST=”alice bob charlie”

for USER in $USER_LIST; do

sudo useradd -m -s /bin/bash -c “Automated Account” “$USER”

echo “$USER:initialPassword123” | sudo chpasswd

sudo usermod -aG developers “$USER”

sudo chage -d 0 “$USER” # Force password change on first login

done

Store passwords securely, never in plain text in a script. For production, use SSH key injection, vault systems, or random one-time passwords delivered via a secure channel.

Strategic Next Steps for System Security

Basic user creation is just the start. To harden your system, implement these practices. Enforce a strong password policy using pam_pwquality. Regularly audit user accounts with lastlog or faillog. Remove or disable dormant accounts. Utilize sudoers configuration to grant precise administrative privileges instead of sharing the root password.

Consider centralized authentication like LDAP or FreeIPA for large-scale environments. This provides a single source of truth for users and groups across multiple servers.

Mastering user and group management transforms you from someone who can follow instructions to a system architect who can design secure, collaborative environments. The commands are simple, but their thoughtful application is what defines professional Linux administration.

Start by adding a test user and group on a non-critical system. Create a shared directory and experiment with permissions. The hands-on experience will cement these concepts far more than any guide. Your next server setup will be more secure, organized, and ready for collaboration from the first command.

Leave a Comment

close