Skip to content
Linux

Users & Groups

Create users, manage groups, and grant sudo access.

By EZ4Code Team
usergroupsudo

Code

# Create user with home directory and shell
sudo useradd -m -s /bin/bash alice
sudo passwd alice

# Modify user
sudo usermod -aG docker,sudo alice
sudo usermod -L alice          # lock password
sudo usermod -U alice          # unlock password

# Groups
sudo groupadd developers
sudo gpasswd -d alice developers
getent group developers

# Sudo access
echo "alice ALL=(ALL) NOPASSWD: /usr/bin/systemctl restart nginx" \
  | sudo tee /etc/sudoers.d/alice
sudo visudo -c                 # validate sudoers

# Switching users
su - alice
sudo -iu alice                 # login shell as alice
id alice                       # show uid, gid, groups

Explanation

useradd -m creates a user with a home directory, and -aG appends them to supplementary groups without removing existing memberships. Always edit sudoers via visudo or /etc/sudoers.d snippets to avoid locking yourself out. usermod -L locks an account by prefixing the password hash, denying password logins.

More Linux Snippets