Changing File Permissions

The chmod command is used to change the permissions of a file or directory. Only the root user or the user who owns the file is able to change the permissions of a file.
Consider This!
Why is the command named chmod instead of chperm? Permissions used to be referred to as modes of access, so the command chmod really means change the modes of access.
There are two techniques of changing permissions with the chmod command: symbolic and octal. The symbolic method is good for changing one set of permissions at a time. The octal or numeric method requires knowledge of the octal value of each of the permissions and requires all three sets of permissions (user, group, other) to be specified every time. For the sake of simplicity, only the symbolic method will be covered. (at least for now)
Follow Along
Use the following command to switch to the Documents directory:
sysadmin@localhost:~$ cd ~/Documents

The Symbolic Method

chmod []... FILE
To use the symbolic method of chmod first indicate which set of permissions is being changed:
chmod []... FILE
SymbolMeaning
uUser: The user who owns the file.
gGroup: The group who owns the file.
oOthers: Anyone other than the user owner or member of the group owner.
aAll: Refers to the user, group and others.
Next, specify an action symbol:
chmod []... FILE
SymbolMeaning
+Add the permission, if necessary
=Specify the exact permission
-Remove the permission, if necessary
After an action symbol, specify one or more permissions to be acted upon.
chmod []... FILE
SymbolMeaning
rread
wwrite
xexecute
Finally, a space and the pathnames for the files to assign those permissions.
chmod []... FILE
The file hello.sh used in the examples on the previous page is a script. A script is a file that can be executed, similar to a command:
sysadmin@localhost:~/Documents$ ls -l hello.sh                                  
-rw-rw-r-- 1 sysadmin sysadmin 21 Aug  1 02:35 hello.sh   
However currently, the execute permission is not set for any of the permission groups:
-rw-rw-r-- 1 sysadmin sysadmin 21 Aug  1 02:35 hello.sh 
Attempting to execute this script using the following syntax fails:
sysadmin@localhost:~/Documents$ ./hello.sh                                      
-bash: ./hello.sh: Permission denied  
​⁠​‌‌⁠⁠⁠⁠​
Since the system is currently logged in as the sysadmin user, and sysadmin is the owner of this file, giving the user owner the execute permission should allow you to execute this script. Using the chmod command with the u character to represent the user owner permission set, the + character to indicate a permission is being added, and the xcharacter to represent the execute permission, the command should be executed as follows:
sysadmin@localhost:~/Documents$ chmod u+x hello.sh
No output indicates the command succeeded. which can be Confirmed by checking the permissions using the ls -lcommand:
sysadmin@localhost:~/Documents$ ls -l hello.sh                                  
-rwxrw-r-- 1 root sysadmin 112 Aug  1 03:48 hello.sh  
The user owner now has the execute permission listed:
-rwxrw-r-- 1 sysadmin sysadmin 21 Aug  1 02:35 hello.sh 
Finally, attempting to execute the script again using the command syntax shown below will work now:
./hello.sh
sysadmin@localhost:~/Documents$ ./hello.sh                                      
 ______________                                                                 
( Hello World! )                                                                
 --------------                                                                 
        \                                                                       
         \                                                                      
           <(^)                                                                 
            ( ) 
Consider This
Notice that to execute the script in the previous example, a ./ character combination was placed before the script name.
./hello.sh
This indicates the "command" should be run from the current directory.

Comments