Manage Linux File Permissions and Ownership
Read and change file permissions with chmod and ownership with chown, and understand why 'permission denied' happens in the first place.
Requirements
- A Linux terminal — local, VM, or over SSH
Read permissions with ls -l
$ ls -lExpected output
-rwxr-xr-- 1 alice devs 220 Aug 24 09:12 deploy.sh
The first character is the file type (- for a regular file, d for a directory). The next nine characters are three groups of rwx — read, write, execute — for the owner, the group, and everyone else, in that order.
Change permissions with chmod
chmod accepts either symbolic or octal notation. Symbolic is easier to reason about for a single change:
$ chmod u+x deploy.shThis adds execute permission for the owner only. Octal notation sets all three groups at once — each digit is a sum of read (4), write (2), and execute (1):
$ chmod 755 deploy.sh755 means owner gets read+write+execute (7), group and others get read+execute (5).
Warning
Avoid chmod 777 as a quick fix — it gives every user on the system full read/write/execute access, which is almost never actually what you want.
Change ownership with chown
Only root (or the current owner, in limited cases) can change who owns a file, so this needs sudo.
$ sudo chown alice:devs deploy.shThis sets the owner to alice and the group to devs. Omit the group (sudo chown alice deploy.sh) to change only the owner.
Apply changes recursively
Add -R to apply a change to a directory and everything inside it.
$ sudo chown -R alice:devs /var/www/myappTip
Double-check the path before running a recursive chown or chmod — it's easy to point it at more than you meant to.
Troubleshooting
The file doesn't have execute permission set, even though you can read it.
$ chmod +x ./script.sh$ ./script.sh