Changing Permissions with chmod

Permission denied

hello.sh is a little script, a text file full of commands. Try to run it:

./hello.sh
ls -l hello.sh

In 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.sh

In 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

WhoActionPermission
u owner (user)+ addr read
g group- removew write
o others= set exactlyx 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.txt

In 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 shared

In 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/hostname

In the terminal

student@vulntech:~$ chmod a+w /etc/hostname
chmod: changing permissions of '/etc/hostname': Operation not permitted

Remember: chmod u+x file makes it runnable for you. Who (ugoa) + action (+ - =) + permission (rwx).

HOME LEARN COMMUNITY DASHBOARD
Practice terminal Starting…