On this page
- Where does Linux keep users and groups?
- How do you add a new user?
- How do you give a user sudo rights?
- How do you create groups and share access?
- How do you change an existing account?
- How do you lock, expire or remove an account?
- How do you create a service account for an app?
- How do you see who is logged in and what they did?
- Why is a user or group change not working?
- Questions
Where does Linux keep users and groups?
Accounts live in plain text files under /etc, and the tools on this page edit them for you. /etc/passwd holds one line per user (name, UID, primary GID, comment, home, shell), /etc/shadow stores password hashes and ageing data readable only by root, and /etc/group lists groups and their extra members.
getent passwd deploy
getent group sudo wheel
id
grep -E ':[0-9]{4,}:' /etc/passwd | cut -d: -f1,3,7Normal people get UIDs from 1000 upwards (set in /etc/login.defs); lower numbers belong to the system and to service accounts. Avoid editing these files by hand: a broken line can stop logins. If you must, use sudo vipw and sudo vigr, which lock the files and check them.
How do you add a new user?
On Ubuntu and Debian use the friendly adduser script, which creates the home directory, copies the skeleton files from /etc/skel and asks for a password. The low-level useradd works everywhere and is the usual choice on Rocky Linux and AlmaLinux.
# Ubuntu / Debian
sudo adduser deploy
# Rocky Linux / AlmaLinux (and any distro)
sudo useradd -m -s /bin/bash -c "Deploy user" deploy
sudo passwd deploy
id deploy
ls -la /home/deployWith useradd, always pass -m on Debian-based systems, or no home directory is created. Give the account an SSH key rather than relying on the password; SSH keys and hardening shows how to copy one in. Keys you add on the console SSH keys page only reach root at deploy time, so new users need their own ~/.ssh/authorized_keys.
How do you give a user sudo rights?
Add the user to the admin group: sudo on Ubuntu and Debian, wheel on Rocky and Alma. Both groups are already allowed in the default sudoers file.
# Ubuntu / Debian
sudo usermod -aG sudo deploy
# Rocky Linux / AlmaLinux
sudo usermod -aG wheel deploy
# test from a new login as deploy
sudo -l
sudo whoamiThe -a flag is essential: without it, -G replaces the whole list of extra groups. Group changes apply at the next login, so open a new SSH session to test. For narrow rights (one command only), add a file under /etc/sudoers.d/ with sudo visudo -f /etc/sudoers.d/deploy. The full walk-through is in create a sudo user.
How do you create groups and share access?
Create a group with groupadd, add members, and give a shared directory to that group. This is the clean way to let two people, or a person and a service, work on the same files.
sudo groupadd webteam
sudo usermod -aG webteam deploy
sudo usermod -aG webteam alice
getent group webteam
sudo gpasswd -d alice webteam # remove one member
sudo groupdel webteam # delete the groupSetting the group owner and permissions of the shared folder is covered in ownership with chown and chgrp and file permissions. A user can switch the active group for one shell with newgrp webteam instead of logging out.
How do you change an existing account?
usermod changes nearly everything about a user. Run it while the user has no processes running, because renaming or moving a home under a live session can confuse programs.
sudo usermod -s /bin/zsh deploy # login shell
sudo usermod -c "Deploy and CI" deploy # comment field
sudo usermod -l deployer deploy # rename login
sudo usermod -d /home/deployer -m deployer # move home directory
sudo groupmod -n deployer deploy # rename matching group
sudo passwd deployer # set a new passwordCheck running processes first with pgrep -u deploy -l. Valid shells are listed in /etc/shells.
How do you lock, expire or remove an account?
Lock first and delete later: locking is reversible, deleting is not. A locked password still lets SSH keys in, so for a full block also expire the account.
sudo passwd -l alice # lock password
sudo usermod -e 1 alice # expire account, blocks key logins too
sudo usermod -e "" alice # undo the expiry
sudo passwd -u alice # unlock password
sudo chage -l alice # show ageing and expiry
# remove for good
sudo deluser --remove-home alice # Ubuntu / Debian
sudo userdel -r alice # Rocky / Alma / anyBefore removing, look for files the user owns outside the home with sudo find / -xdev -user alice, and check cron with sudo crontab -l -u alice. Our daily backups add-on is a good safety net before bulk cleanups.
How do you create a service account for an app?
Give each long-running app its own system user with no password and no login shell. If the app is ever compromised, the damage stays inside what that user can touch.
sudo useradd --system --create-home --home-dir /var/lib/myapp \
--shell /usr/sbin/nologin myapp
id myapp
sudo -u myapp whoamiThen set User=myapp in the systemd unit, as shown in run an app as a systemd service. On Ubuntu and Debian, sudo adduser --system --group myapp does the same job and also creates a matching group.
How do you see who is logged in and what they did?
A few read-only commands give a quick picture of account activity:
who # current sessions
w # sessions plus what each is running
last -n 10 # recent logins
lastlog | grep -v "Never" # last login per user
sudo journalctl _COMM=sudo --since todayOn Debian 13 the old lastlog tool is gone; install lastlog2 or use last for the same information. For alerts on each new login see SSH login alerts.
Why is a user or group change not working?
- "user is not in the sudoers file": the group was added but the user did not log in again, or you used the wrong group name for the distro.
- Lost other groups after usermod:
-Gwas used without-a. Re-add them with-aG. - "user is currently used by process": stop that user's processes (
sudo pkill -u name) before renaming or deleting. - Key login refused for a new user: the
.sshfolder must belong to the user with mode 700 andauthorized_keyswith mode 600.
Plans that fit this guide
Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.
Questions about Users and groups
What is the difference between adduser and useradd?
useradd is the low-level tool on every distro. On Ubuntu and Debian, adduser is a friendlier wrapper that creates the home, asks for a password and applies local defaults. On Rocky and Alma, adduser is simply a link to useradd.
How do I list all groups a user belongs to?
Run id username or groups username. The first group shown by groups is the primary group.
Should I keep logging in as root?
It works, but a named user with sudo is safer and leaves a clear record. Once the sudo user works, consider disabling root login.
Can two users share one UID?
Technically yes, but avoid it: the system cannot tell them apart in file ownership or logs. Give each person their own account.
Where is the root password for a new server?
On the Overview tab of the instance page in the console. Change it after the first login with sudo passwd root, or lock it once a sudo user is ready.
More in Linux command line
- Copy files and directories with cp
- Move and rename with mv
- Create directories and empty files
- List files with ls, find your place with pwd
- Read files with cat, less and tail
- Find files with find and locate
- Search text with grep
- Edit and report text with sed and awk
- echo, tee and wc
- Edit files with nano
- Edit files with Vim
- Read manual pages
- Ownership with chown and chgrp
- Symbolic and hard links
- Pipes, redirection and xargs