Bash Script – File Permissions

Configurare noua (How To)

Situatie

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 have to give or set  the user file permission to execute so that he can execute that file.

Solutie

Pasi de urmat
Basic list views of file and their permissions

The command we use is ls -l to show us the list of file/directory  in the current folder we are present. This screenshot is a common example to show permission (using normal centos7)

From the above screenshot, let’s take a file let’s say luser_t04.sh, and see its file permissions

rwx rwx rwx    1 root     root             0 Feb 27 17:35 luser_t04.sh

1     2      3

(r = read , w= write ,  x = execute)

  • 1 represents the permission of the user, they have all the 3 permission to read, write and execute the file
  • 2 represents the group to which the file is associated  it also has all the permissions
  • 3 represent others  which also contains all the 3 permissions
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

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 

Tip solutie

Permanent

Voteaza

(5 din 10 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?