Manage file permissions
To manage file permissions, we have a command called chmod which we can use to change the permission of files and directories.
Method to use chmod command
There are 2 methods to use the command
- Symbolic method
- Numeric method
Symbolic Method
Syntax:
chmod whowhatwhich File|directory
- who is u(user) , g(group) , o(other)
- what is to +(add) ,-(remove) ,+(set)
- which is r (read), w(write), x(execute)
Let’s see an example: –
Suppose you want to remove read and write permission of group and others from the file
chmod go-rw <FILE> (can be any file name)
Example:
chmod go-rw luser_t04.sh. The new permission will be like this
-rwx–x–x 1 root root 0 Feb 22 02:28 luser_t04.sh
Numeric method
Syntax:
chmod ### file|directory
#: represents each digit the permission for user, group and others.
4 =read 2=write 1 = execute
Let’s see the example from above:
-rwx–x–x 1 root root 0 Feb 22 02:38 luser_t04.sh
- user permission has been represented by rwx (4+2+1=7)
- group permission has been described as –x (0+0+1 =1)
- other permission has been described as –x ( 0+0+1=1)
Let us take another example of a file
File with permission of 640 what it means in numeric method
-rw- r– — 1 root root 0 Feb 22 02:36 file1.txt
- user have read(4) and write(2) permission represented by rw- (4+2+0=6)
- the group has read(4) permission represented by r– (4+0+0=4)
- other have no permission(0) ,represented by — (0+0+0=0)
In this example to set file permission for a file (Example: FILE )
We want the user to be able to read, write and execute files but we don’t want groups to write and execute the file and others to only be able to execute the file so what command and syntax we should use
Solution:
chmod 741 FILE1 (can be any file name)
The new permission set will be rwx r– –x for FILE
-rwx r– –x 1 root root 0 Feb 22 02:28 file.txt
Bash Script – File Permissions
In this article, we will discuss file permission in Bash Script
To understand the scenario let’s take an example. Let’s consider there is a system admin A for company XYZ he designs a script that is to be executed by a user at 8:00 PM daily to send a report. He designs the script but forgets to give permission to the user to execute the script. Whenever a user tries to execute a script, he gets the error You are not authorized to take this action so why did this happen?
In the above case, we see that the system admin created a script but he did not give permission to the user as he was unable to execute it so to go into depth about this let’s explain how can we set file permission and how to check them.
Solution: Now for the above example the system admin only has to give or set the user file permission to execute so that he can execute that file