trio.so
Explained

Linux User Management: Commands and Best Practices

Linux user management with the commands you need (useradd, usermod -aG, gpasswd, sudo) and best practices for accounts, groups, SSH keys and auditing.

Mountain landscape representing leadership perspective and vision
Written by
Atena Ladchartabi

Content writer, trio.so

Published on
Modified on

To add a user to a group in Linux, run sudo usermod -aG groupname username, then have the user sign out and back in. The -a matters: without it, -G replaces the user's other groups. Below are the commands for creating, changing and removing users and groups on Debian, Ubuntu and RHEL-based systems, and the practices that keep accounts under control.

Add a user to a group

sudo usermod -aG developers alice     # add alice to developers, keep her other groups
sudo gpasswd -a alice developers      # same result, one group at a time
groups alice                          # check which groups alice is in
id alice                              # same, with numeric IDs

Group changes apply at the next login. To use the new group in the current shell without logging out, run newgrp developers. To remove a user from a group, use sudo gpasswd -d alice developers.

Create, change and remove users

Create a user

sudo useradd -m -s /bin/bash alice    # create alice with a home directory and bash
sudo passwd alice                     # set her password

On Debian and Ubuntu, sudo adduser alice is the friendlier option: it creates the home directory, asks for a password and fills in details interactively.

useradd vs adduser

useradd is the low-level tool available on every distribution; it does only what its flags say. adduser is a Debian and Ubuntu script that wraps it with sensible defaults. On RHEL, Fedora and their relatives, adduser is just another name for useradd.

Change a user

sudo usermod -s /bin/zsh alice        # change login shell
sudo usermod -l alice2 alice          # rename the account (home directory stays)
sudo chage -M 90 alice                # password must change every 90 days
sudo chage -l alice                   # show password ageing settings

Lock or remove a user

sudo usermod -L alice                 # lock the password
sudo usermod -e 1 alice               # expire the account so no login method works
sudo userdel -r alice                 # delete alice and her home directory

Locking only the password does not stop a user who logs in with an SSH key; expiring the account does. When someone leaves, lock and expire first, archive their files, then delete.

Give a user sudo access

sudo usermod -aG sudo alice           # Debian and Ubuntu
sudo usermod -aG wheel alice          # RHEL, Fedora, Rocky, AlmaLinux

For narrower rights, add a file under /etc/sudoers.d/ with sudo visudo -f /etc/sudoers.d/deploy, for example allowing one group to restart one service only. Always edit sudoers with visudo: it checks the syntax, and a broken sudoers file can lock everyone out of sudo.

Groups

sudo groupadd developers              # create a group
sudo groupdel developers              # delete a group
getent group developers               # list a group's members

Give permissions to groups, not to individual users. When someone changes team, you change one membership instead of many file permissions.

Linux user management best practices

  1. No shared accounts. One person, one account, so actions can be traced.
  2. No direct root login. Set PermitRootLogin no in /etc/ssh/sshd_config and use sudo.
  3. SSH keys, not passwords, for remote access, with PasswordAuthentication no once keys work.
  4. Least privilege. Grant sudo only where needed, and prefer specific commands in /etc/sudoers.d/ over full sudo.
  5. Service accounts for applications, created with useradd -r -s /usr/sbin/nologin so nobody can log in as them.
  6. Central identity for more than a handful of machines: connect servers to your directory (for example with SSSD to Active Directory or LDAP) instead of creating local users everywhere.
  7. Review regularly. List users with a login shell and members of sudo or wheel, and remove what nobody can explain.
  8. Log and audit. Keep auth logs (/var/log/auth.log or /var/log/secure) and send them off the machine.
getent passwd | awk -F: '$7 !~ /(nologin|false)$/ {print $1}'   # users who can log in
getent group sudo wheel                                         # who has sudo

Managing this across many devices

Running the same user change on ten machines by SSH, one at a time, is where mistakes creep in. trio.so runs scripts and opens a live terminal on enrolled Linux devices from the console, so a tested usermod script can go to the machines you choose. See remote terminal.

The trio.so Create script wizard with a bash script that adds a user to the developers group on Linux.
A bash script that adds a user to a group, ready to run on enrolled Linux devices from trio.so.

Related reading: server security best practices and what is the most secure operating system.

Ready-to-use Templates

Must-have Template Toolkit for IT Admins

Explore All
Template Toolkit

Run one user script on many Linux machines

trio.so runs scripts and opens a live terminal on enrolled Linux devices.

Start free trial

Manage Every Device From One Console

trio.so brings device management, patching and security policies for Windows, macOS, Linux, iOS/iPadOS and Android into one console. Enroll a device and see it in the console within minutes.

14-day free trial. No credit card required.

trio.so brings device management, patching and security policies for Windows, macOS, Linux, iOS/iPadOS and Android into one console. Enroll a device and see it in the console within minutes.

Smiling womanAbstract geometric patternAbstract geometric patternSmiling womanSmiling woman

Frequently Asked Questions (FAQ)

Have questions? We've got answers. This section covers some of the most commonly asked questions related to this topic.

Run sudo usermod -aG groupname username, then sign out and back in. Without -a, -G replaces the user's other groups.

useradd is the low-level command on every distribution. adduser is a friendlier Debian and Ubuntu script that wraps it; on RHEL it is just another name for useradd.

Add them to the sudo group on Debian and Ubuntu, or the wheel group on RHEL-based systems, or give narrower rights in /etc/sudoers.d/ with visudo.

One account per person, no direct root login, SSH keys, least privilege, service accounts without login, central identity for many machines, regular reviews and audit logs.

Related

From the blog

The related industry news, interviews, technologies, and resources.