Master Linux users, groups, and the powerful permission system
Click the green 💻 Terminal button in the bottom-right corner to open an interactive terminal. You can practice all the commands on this page in real-time!
Linux is a multi-user operating system with a robust permission system. Every file and directory has an owner, a group, and a set of permissions that determine who can read, write, or execute it.
{When you run ls -l, you see detailed information about files and directories:}
ls -l
Expected Output:
-rw-r--r-- 1 user user 4096 Dec 03 10:30 README.txt
drwxr-xr-x 2 user user 4096 Dec 03 10:30 documents/
Let's break down what this means:
The first 10 characters show the type and permissions:
chmod🔐 chmod - Change File Mode/PermissionsModifies file or directory permissions to control who can read, write, or execute.Symbolic Mode$ chmod u+x script.sh▸ Give owner execute permissionNumeric Mode$ chmod 755 file.txt▸ rwxr-xr-x (owner: rwx, group: r-x, others: r-x)Common Pattern$ chmod 644 document.txt▸ rw-r--r-- (owner: rw, group/others: r))Command: chmod [who][+/-][permissions] filename
Examples:
# Give owner execute permission
touch script.sh
chmod u+x script.sh
ls -l script.sh
# Remove write permission for others
touch document.txt
chmod o-w document.txt
ls -l document.txt
# Add read and execute for everyone
touch public_file.txt
chmod a+rx public_file.txt
ls -l public_file.txt
Permissions can be represented as numbers:
Add them up: 7 = 4+2+1 = rwx, 6 = 4+2 = rw-, 5 = 4+1 = r-x
Examples:
# rwx for owner, r-x for group, r-- for others
touch file.txt
chmod 754 file.txt
ls -l file.txt
# rw- for owner and group, r-- for others
touch document.txt
chmod 664 document.txt
ls -l document.txt
# rwx for everyone
touch script.sh
chmod 777 script.sh
ls -l script.sh
Be careful with chmod 777! This gives full permissions to everyone and is generally insecure. Only use it when you fully understand the implications.
chown👤 chown - Change File Owner and GroupChanges the owner and/or group of files and directories. Usually requires sudo privileges.Change Owner$ chown john file.txt▸ Changes owner to 'john'Change Owner and Group$ chown john:staff file.txt▸ Changes owner to 'john' and group to 'staff'Change Only Group$ chown :developers project/▸ Changes group to 'developers')chown [owner][:group] filenameExamples:
# Change owner to 'john'
touch file.txt
sudo chown john file.txt
ls -l file.txt
# Change owner to 'john' and group to 'staff'
touch file.txt
sudo chown john:staff file.txt
ls -l file.txt
# Change only the group
sudo groupadd developers
mkdir project
sudo chown :developers project/
ls -ld project/
Note: Requires sudo (superuser) privileges. Only root can change file ownership.
# Show current user
whoami
# Show user ID and groups
id
# List all users (first 10)
cat /etc/passwd | head -10
# List all groups
cat /etc/group
useradd username - Create a new userusermod -aG groupname username - Add user to groupuserdel username - Delete a userpasswd username - Change user passwordsu - username - Switch to another usersudo command - Execute command as superuser# Create a new group
sudo groupadd developers
# Add user to group
sudo usermod -aG developers john
# View groups for current user
groups
# View groups for specific user
groups john
# Create a script
echo '#!/bin/bash' > myscript.sh
echo 'echo "Hello, World!"' >> myscript.sh
# Make it executable
chmod +x myscript.sh
# Run it
./myscript.sh
# Create directory for team project
sudo mkdir -p /var/projects/team-app
# Create users alice and bob
sudo useradd -m alice
sudo useradd -m bob
# Create developers group
sudo groupadd developers
# Add users to group
sudo usermod -aG developers alice
sudo usermod -aG developers bob
# Set ownership and permissions
sudo chown :developers /var/projects/team-app
sudo chmod 775 /var/projects/team-app
# Verify the setup
ls -ld /var/projects/team-app
groups alice
groups bob
# Create a file with sensitive data
touch private.key
# Make it readable only by owner
chmod 600 private.key
# Verify permissions
ls -l private.key
# Output: -rw------- 1 user user 0 Dec 03 10:30 private.key
Ready to practice? Head over to the Interactive Terminal to try permission commands!