Guide
Linux
Intermediate
Permissions & User Commands
chmod, chown, useradd, usermod, sudo, and the everyday commands for managing file permissions, ownership, and user accounts.
Permissions: chmod
chmod 644 file.txt # owner rw-, group r--, others r-- (standard for files)
chmod 755 script.sh # owner rwx, group r-x, others r-x (standard for executables)
chmod u+x script.sh # add execute for owner only
chmod g-w file.txt # remove write for group
chmod -R 755 directory/ # apply recursively
chmod +t /shared/ # sticky bit — only owner can delete their own files
Ownership: chown / chgrp
sudo chown deploy file.txt # change owner
sudo chown deploy:developers file.txt # change owner AND group in one command
sudo chown -R www-data:www-data /var/www/ # recursive ownership change
sudo chgrp developers file.txt # change group only
Inspecting Permissions & Ownership
ls -l file.txt # rwx string + owner + group
stat file.txt # full metadata including octal permission mode
namei -l /var/www/html/index.html # shows permissions of EVERY directory in the path (great for debugging "permission denied")
getfacl file.txt # view ACL entries (finer-grained than owner/group/other)
User Management
sudo useradd -m -s /bin/bash deploy # create user, home directory, default shell
sudo passwd deploy # set/change password
sudo userdel -r deploy # delete user AND their home directory
sudo usermod -aG docker deploy # add to a supplementary group (note: -a is critical, appends rather than replaces)
sudo usermod -L deploy # lock an account (disable password login)
Group Management
sudo groupadd developers
sudo groupdel developers
groups deploy # list all groups a user belongs to
id deploy # UID, GID, and all group memberships
Switching Users & Privilege Escalation
sudo whoami # run a single command as root
sudo -u postgres psql # run a command as a specific (non-root) user
su - deploy # full login shell as another user
visudo # safely edit /etc/sudoers (validates syntax before saving)
Password & Account Policy
chage -l deploy # show password expiry/aging info
sudo chage -M 90 deploy # force password change every 90 days
sudo passwd -l deploy # lock the password (alternative to usermod -L)
Production Considerations
- Always edit
/etc/sudoerswithvisudo, never a plain text editor — a syntax error there can lock out sudo access system-wide. usermod -aG— forgetting the-a(append) flag replaces all existing supplementary groups instead of adding one, a classic and dangerous mistake.- Service accounts should be created with
--system(or anologinshell) and no usable password — they should only own processes/files, never be logged into interactively.
Quick Interview Answer
“
chmodsets read/write/execute permissions numerically (chmod 644) or symbolically (chmod u+x);chown/chgrpchange ownership. User accounts are managed withuseradd/usermod/userdel, and group membership withusermod -aG— the-amatters, since omitting it replaces rather than appends group memberships.sudoruns a single command with elevated privileges and logs it;/etc/sudoersshould only ever be edited viavisudo.”
Common Mistakes
- Running
usermod -G group userwithout-a, wiping out the user’s other group memberships. - Editing
/etc/sudoersdirectly instead of throughvisudo. - Using
chmod 777to “fix” a permission error instead of diagnosing the actual ownership/group mismatch.
Add More Questions to This Guide
Know a question that should be here? Share it and help the community!
Open Google Form