How-to

The usermod Command: Change Linux Accounts Safely

usermod edits an account that already exists. Most of its flags are simple, but one of them, -G without -a, silently removes a user from every other group.

What can usermod change?

Everything stored about an account in /etc/passwd, /etc/shadow and /etc/group: name, UID, primary and supplementary groups, home folder, shell, comment, password lock and expiry date. It needs root, so run it with sudo. To create an account in the first place, use The adduser command or Create a Linux user.

How do I add a user to a group without breaking things?

sudo usermod -aG docker deploy
sudo usermod -a -G adm,systemd-journal deploy
id deploy

-a means append. Without it, -G replaces the whole list of supplementary groups, so usermod -G docker deploy would remove deploy from sudo and anything else. This is one of the most common ways people lose admin access on their own server. Always type -aG together. Group changes apply at the next login; run newgrp docker or log out and back in to pick them up in your session.

How do I remove a user from one group?

sudo gpasswd -d deploy docker
sudo usermod -rG docker deploy       # newer shadow-utils only

gpasswd -d works everywhere. The -r flag for usermod exists in shadow-utils 4.8 and later, which includes current Debian, Ubuntu and RHEL 9 family releases.

How do I change the primary group?

sudo usermod -g www-data deploy

Lowercase -g sets the primary group, which new files get by default. Existing files keep their old group; fix them with find /home/deploy -group deploy -exec chgrp www-data {} + if needed. More on ownership in The chown command.

How do I rename an account?

sudo pkill -u olddev
sudo usermod -l newdev -d /home/newdev -m olddev
sudo groupmod -n newdev olddev
sudo usermod -c "New Dev, platform team" newdev

-l sets the new login name, -d the new home path and -m moves the contents there. The user must not be logged in, which is why the first line ends their processes. Renaming does not rename the matching private group, so groupmod -n does that. Check cron jobs, sudoers rules and service units that mention the old name.

How do I change the home folder or shell?

sudo usermod -d /srv/apps/billing -m billing
sudo usermod -s /bin/bash billing
sudo usermod -s /usr/sbin/nologin billing

Without -m the home path changes but files stay where they were. A nologin shell stops interactive logins while the account keeps running services, which is right for app accounts. chsh does the same for the current user without root.

How do I lock or expire an account?

sudo usermod -L contractor          # lock the password
sudo usermod -e 1 contractor        # expire the account at once
sudo usermod -e 2027-03-31 intern   # expire on a date
sudo usermod -U -e "" contractor    # unlock and clear expiry

Locking the password alone does not stop SSH key logins. Setting the expiry date to 1, which means January 2, 1970, does, because OpenSSH refuses expired accounts through PAM. Use both when someone leaves and you want to keep their files for a while. chage -l contractor shows the result.

How do I change a UID safely?

sudo usermod -u 2050 appsvc
sudo groupmod -g 2050 appsvc
sudo find / -xdev -uid 1003 -exec chown -h 2050 {} +

usermod updates ownership inside the home folder, but not elsewhere. The find line fixes the rest on the root filesystem; repeat it for other mounts. Matching UIDs matters when you share NFS volumes or move container volumes between machines.

Which usermod flags are worth remembering?

FlagPurpose
-aGAppend supplementary groups
-gSet primary group
-lNew login name
-d -mNew home and move files
-sLogin shell
-L, -ULock, unlock password
-eAccount expiry date
-uNew UID
-cComment, often the full name

After any change, getent passwd name and id name confirm the result. For a wider view of account housekeeping, see Linux user management.

What if the user has running processes?

usermod refuses to change the name, UID or home of an account that has processes running, and prints which process blocked it. Stop the services that run as that account first with systemctl stop, or end leftover processes with pkill -u, and then repeat the command. Group, shell, comment and expiry changes work while the user is active, but running processes keep their old group list until they restart.

Affordable Machines for Shared Team Servers

ServerProcessorCores / threadsMemoryStorageNetworkPer monthSmallest order
E2Intel Xeon E3-1270 v34 / 832 GB DDR31x 480 GB SSD2x 1 Gbps$38.32$114.96 (3 months)
S1Intel Xeon E-2276G6 / 1264 GB DDR41x 1 TB NVMe2x 10 Gbps$53.32$53.32 (1 month)
S6Intel Xeon E-2288G8 / 1664 GB DDR42x 1 TB NVMe2x 10 Gbps$89.92$89.92 (1 month)

Keep Reading

Add a user to a group

Put an account into supplementary groups without dropping its others, change its primary group and make new membership take effect.

Open

Linux user management

The full life of an account on a server: create, group, change, expire, lock, audit and delete, with the files behind each step.

Open

List users in Linux

List every account on a Linux server, tell real users from system accounts, see who is logged in now and who has sudo rights.

Open

The /etc/passwd file

Read the seven fields of /etc/passwd, see how it links to /etc/shadow and /etc/group, and edit accounts safely with vipw and the user tools.

Open

FAQ

Quick Answers

Without -a, -G replaces all supplementary groups. Always use -aG to add one.

For new logins, yes. Existing sessions need newgrp or a fresh login.

No. End their sessions and processes first.

No. Expire the account with usermod -e 1 as well to stop key logins.

chsh changes only the shell and can be run by the user. usermod changes anything and needs root.