Linux Cheatsheet
Users and Groups
Use this Linux reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.
User Accounts
whoami # current username id # UID, GID, and supplementary groups id alice # info for another user who # logged-in users (current sessions) w # who + what they're doing last # login history last -n 20 # last 20 entries lastlog # last login of every account
Creating and Managing Users
useradd alice # create user (no home dir on some distros) useradd -m alice # create with home directory useradd -m -s /bin/bash alice # set login shell useradd -m -G sudo,docker alice # add to supplementary groups usermod -aG docker alice # add alice to docker group (-a = append!) usermod -s /bin/zsh alice # change shell usermod -l newname oldname # rename user userdel alice # delete user (keep home) userdel -r alice # delete user + home + mail passwd alice # set/change password passwd -l alice # lock account passwd -u alice # unlock account chage -l alice # view password expiry info chage -E 2025-12-31 alice # set account expiry
Key Files
| File | Contents |
|---|---|
/etc/passwd | user accounts (name:x:uid:gid:gecos:home:shell) |
/etc/shadow | hashed passwords + expiry (root-readable only) |
/etc/group | groups (name:x:gid:members) |
/etc/gshadow | group passwords |
/etc/skel/ | template files copied to new home dirs |
/etc/login.defs | default UID/GID ranges, password policies |
# Quick user info from /etc/passwd getent passwd alice # show alice's entry (also searches LDAP/NIS) getent group docker # show group members
Groups
groups # your current groups groups alice # alice's groups groupadd devs # create group groupdel devs # delete group groupmod -n newname devs # rename group gpasswd -a alice devs # add alice to devs gpasswd -d alice devs # remove alice from devs gpasswd -A alice devs # make alice admin of devs newgrp docker # switch primary group (current session)
Group membership changes take effect on next login. Use
newgrpor log out/in to apply immediately.
sudo — Run as Root
sudo command # run as root sudo -u alice command # run as alice sudo -i # interactive root shell (login shell) sudo -s # root shell (current env) sudo !! # re-run last command as root sudo -l # list your sudo permissions sudo -l -U alice # list alice's sudo permissions sudoedit /etc/hosts # edit file safely as root
/etc/sudoers
Edit only with visudo (syntax-checks before saving):
visudo # edit /etc/sudoers safely visudo -f /etc/sudoers.d/devops # edit a drop-in file
# /etc/sudoers examples alice ALL=(ALL:ALL) ALL # full sudo %devops ALL=(ALL) NOPASSWD: /bin/systemctl restart nginx bob ALL=(ALL) NOPASSWD: ALL # passwordless (use carefully)
Drop-in files go in /etc/sudoers.d/ (included automatically).
su — Switch User
su alice # switch to alice (keeps env) su - alice # switch to alice with login environment su - # switch to root with login environment su -c "cmd" alice # run single command as alice
PAM and Authentication Logs
/var/log/auth.log # auth events (Debian/Ubuntu) /var/log/secure # auth events (RHEL/CentOS) journalctl -u sshd # SSH auth via systemd journal faillock --user alice # show failed login attempts faillock --user alice --reset # reset lockout pam_tally2 --user alice # (older systems)