In Linux-based systems, permissions allow controlling who can access what. Each file or directory has permissions associated with three entities:
Permissions define the allowed actions for each entity:
Permissions are visible with the ls -l command, which displays one line per file or directory:
-rwxr-xr-- 1 user group 12345 date file
rwx:
r (read): permission to read the file content.w (write): permission to modify or delete the file.x (execute): permission to execute the file, like a program or script.r (read): permission to list the directory content.w (write): permission to create, delete or rename files or subdirectories inside.x (execute): permission to enter the directory (use cd) and access files inside (if file permissions allow it).- for a file, d for a directory, etc.), followed by permissions for owner, group and others.-rwxr-xr--rwx: owner permissions (read, write, execute).r-x: group permissions (read, execute).r--: others permissions (read only).chmodThe chmod command allows modifying permissions of a file or directory.
chmod [options] mode file
u: owner (user).g: group.o: others.a: all.+ (add), - (remove), = (set).Example:
chmod u+x file # Adds execution for owner.
chmod u+rw file # Adds read and write for owner.
chmod g-w file # Removes write for group.
chmod a=r file # Gives only read to all.
Example:
chmod 755 file # rwx for owner, r-x for others.
chmod 644 file # rw- for owner, r-- for others.
chownThe chown command allows changing the owner or group of a file or directory.
chown [owner][:group] file
Examples:
chown alice file # Changes owner to "alice".
chown alice:developers file # Changes owner and group.
chown :developers file # Changes group
chgrpThe chgrp command modifies only the group.
chgrp developers file
Some files or directories may have additional permissions:
s instead of x in owner permissions.chmod u+s file
s instead of x in group permissions.chmod g+s file
t instead of x in others permissions.chmod +t directory
Permissions are essential for Linux system security and management. By understanding their operation and manipulation, you can precisely control access to your files and directories. This helps prevent errors and protect sensitive data.