Permission denied
hello.sh is a little script, a text file full of commands. Try to run it:
./hello.sh
ls -l hello.shIn the terminal
student@vulntech:~$ ./hello.sh -bash: ./hello.sh: Permission denied student@vulntech:~$ ls -l hello.sh -rw-r--r-- 1 student student 40 Oct 17 06:29 hello.sh
It has no x, so Linux refuses to run it. chmod (change mode) adds it:
chmod u+x hello.sh
ls -l hello.sh
./hello.shIn the terminal
student@vulntech:~$ chmod u+x hello.sh student@vulntech:~$ ls -l hello.sh -rwxr--r-- 1 student student 40 Oct 17 06:29 hello.sh student@vulntech:~$ ./hello.sh Hello from a script!
The chmod letters
| Who | Action | Permission |
|---|---|---|
u owner (user) | + add | r read |
g group | - remove | w write |
o others | = set exactly | x execute |
a all three |
Combine them: who, action, permissions. Separate several changes with commas:
chmod go-r diary.txt
ls -l diary.txt
chmod u=rw,g=r,o= diary.txt
ls -l diary.txt
chmod a+r diary.txt
ls -l diary.txtIn the terminal
student@vulntech:~$ chmod go-r diary.txt student@vulntech:~$ ls -l diary.txt -rw------- 1 student student 35 Oct 17 06:30 diary.txt student@vulntech:~$ chmod u=rw,g=r,o= diary.txt student@vulntech:~$ ls -l diary.txt -rw-r----- 1 student student 35 Oct 17 06:30 diary.txt student@vulntech:~$ chmod a+r diary.txt student@vulntech:~$ ls -l diary.txt -rw-r--r-- 1 student student 35 Oct 17 06:30 diary.txt
chmod -R changes a directory and everything inside it:
chmod -R g+w shared
ls -ld sharedIn the terminal
student@vulntech:~$ chmod -R g+w shared student@vulntech:~$ ls -ld shared drwxrwxr-x 2 student student 0 Oct 17 06:30 shared
Only the owner can change permissions
You can change permissions on your own files. On anyone else's, chmod is refused (only root can override this):
chmod a+w /etc/hostnameIn the terminal
student@vulntech:~$ chmod a+w /etc/hostname chmod: changing permissions of '/etc/hostname': Operation not permitted
Remember:
chmod u+x filemakes it runnable for you. Who (ugoa) + action (+ - =) + permission (rwx).