chmod / chown / chgrp
chmod changes file permissions (access rights), chown changes the owner, and chgrp changes the group. These commands are essential for managing files in a Linux multi-user environment.
Syntax
chmod [options] mode file chown [options] user[:group] file chgrp [options] group file
Command / Option List
| Command / Notation | Description |
|---|---|
| chmod 755 file | Sets permissions using octal notation (7=rwx, 5=r-x). |
| chmod +x file | Adds execute permission for all users. |
| chmod u+x file | Adds execute permission for the file owner (user). |
| chmod g-w file | Removes write permission from the group. |
| chmod o=r file | Sets permissions for others to read-only. |
| chmod -R mode directory | Recursively changes permissions for a directory and its contents. |
| chown user file | Changes the owner of a file. |
| chown user:group file | Changes both the owner and group at once. |
| chown -R user directory | Recursively changes the owner for a directory and its contents. |
| chgrp group file | Changes only the group of a file. |
Sample Code
The following examples use this directory structure.
Add execute permission to a script. This is the most common use case.
chmod +x deploy.sh ls -l deploy.sh -rwxr-xr-x 1 user staff 128 Mar 5 10:00 deploy.sh
Set permissions using octal notation. This sets owner to read/write/execute (7), group to read-only (4), and no access for others (0).
chmod 740 secret.conf ls -l secret.conf -rwxr----- 1 user staff 256 Mar 5 10:00 secret.conf
Set standard web server permissions.
chmod 644 public/index.html ls -l public/index.html -rw-r--r-- 1 user staff 2048 Mar 5 10:00 public/index.html
chmod 755 public/ ls -ld public/ drwxr-xr-x 2 user staff 4096 Mar 5 10:00 public/
Change permissions for all files in a directory at once.
chmod -R 755 public/
Change the owner and group together (requires root privileges).
sudo chown www-data:www-data /var/www/html ls -ld /var/www/html drwxr-xr-x 5 www-data www-data 4096 Mar 5 10:00 /var/www/html
Recursively change the owner for a directory and all its contents.
sudo chown -R deploy:deploy /var/www/app
Notes
Octal permission notation is the sum of 4=read (r), 2=write (w), and 1=execute (x). For example, rwxr-xr-x is 755 (7=4+2+1, 5=4+0+1). You can use either symbolic notation (u/g/o with +/-/=) or octal notation — both work, but octal is more explicit and easier to read in scripts.
You can check a file's current permissions and owner with $ ls -l. For detailed metadata, see stat.
If you find any errors or copyright issues, please contact us.