Linux File Permissions Explained | chmod chown Tutorial
file permissions, chmod, chown, read write execute, permission bits, octal notation
Linux File Permissions Explained
Linux file permissions are a core security mechanism that controls who can read, write, and execute files and directories. Every file and directory on a Linux system has an associated set of permissions and an owner. Understanding and managing permissions is an essential Linux skill for anyone working in system administration or development.
Reading Permission Strings
When you run ls -l, each file shows a 10-character permission string. The first character indicates the file type (- for regular file, d for directory, l for symbolic link). The next nine characters are three groups of three: the owner's permissions, the group's permissions, and everyone else's permissions. Each group uses r (read), w (write), and x (execute), with a hyphen meaning that permission is not granted.
The chmod Command
The chmod command changes file permissions. You can use symbolic notation (letters) or octal notation (numbers). In octal notation, read=4, write=2, execute=1. Add them together for each group.
chmod 755 script.sh
chmod 644 config.txt
chmod +x deploy.sh
chmod -w important.txt
chmod u+x,g-w,o-r file.txtThe permission 755 means the owner has full access (7=rwx) and group and others have read and execute (5=r-x). The permission 644 means owner can read and write (6=rw-) and everyone else can only read (4=r--).
The chown Command
The chown command changes the owner and optionally the group of a file or directory. Only root or the current owner can change ownership. The -R flag applies the change recursively to all files inside a directory.
sudo chown alice file.txt
sudo chown alice:developers project/
sudo chown -R www-data /var/www/htmlSpecial Permissions
Linux also has special permission bits: setuid (runs a file with the owner's privileges), setgid (runs with the group's privileges or inherits group for directories), and sticky bit (only the file owner can delete files in a shared directory, used on /tmp).
chmod u+s /usr/bin/passwd
chmod +t /tmp/shared