Setting permissions with the numerical method

With the numerical method, you'll use an octal value to represent the permissions settings on a file or directory. To the r, w, and x permissions, you assign the numerical values 4, 2, and 1, respectively. Do this for the user, group, and others positions, and add them all up to get the permissions value for the file or directory:

User Group Others
rwx rwx rwx
421 421 421
7 7

7

So, if you have all the permissions set for everybody, the file or directory will have a value of 777. If I were to create a shell script file, by default, it would have the standard 664 permissions, meaning read and write for the user and group, and read-only for others:

-rw-rw-r--. 1 donnie donnie 0 Nov  6 19:18 donnie_script.sh
If you create a file with root privileges, either with sudo or from the root user command prompt, you'll see that the default permissions setting is the more restrictive 644.

Let's say that I want to make this script executable, but I want to be the only person in the whole world who can do anything with it. I could do:

[donnie@localhost ~]$ chmod 700 donnie_script.sh

[donnie@localhost ~]$ ls -l donnie_script.sh
-rwx------. 1 donnie donnie 0 Nov 6 19:18 donnie_script.sh
[donnie@localhost ~]$

With this one simple command, I've removed all permissions from the group and from others, and set the executable permission for myself. This is the sort of thing that makes the numerical method so handy for writing shell scripts.

Once you've been working with the numerical method for a while, looking at a file and figuring out its numerical permissions value will become second nature. In the meantime, you can use stat with the -c %a option to show you the values. For example:

[donnie@localhost ~]$ stat -c %a yum_list.txt
664
[donnie@localhost ~]$


[donnie@localhost ~]$ stat -c %a donnie_script.sh
700
[donnie@localhost ~]$

[donnie@localhost ~]$ stat -c %a /etc/fstab
644
[donnie@localhost ~]$
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.138.230