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. For the r, w, and x permissions, you assign the numerical values 4, 2, and 1, respectively. You would do this for the user, group, and others positions, and then 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. To do this, I could do the following:

[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. This can be done like so:

[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 ~]$

If you want to view the numerical permissions of all the files at once, do this:

[donnie@donnie-ca ~]$ stat -c '%n %a ' *
dropbear 755
internal.txt 664
password.txt 664
pki-server.crt 664
pki-server.p12 644
yum_list.txt 664
[donnie@donnie-ca ~]$

Here, you can see the wildcard (*) at the end of the command, indicating that you want to view the settings for all the files. %n indicates that you want to view the filenames, along with the permissions settings. Since we're using two -c options, we have to enclose both of the options within a pair of single quotes. The only slight catch here is that this output doesn't show which of these items are files, and which are directories. However, since directories require executable permissions so that people can cd into them, we can guess that dropbear is probably a directory. To be sure though, just use ls -l, like so:

[donnie@donnie-ca ~]$ ls -l
total 2180
-rwxr-xr-x. 1 donnie donnie 277144 Apr 22 2018 dropbear
-rw-rw-r--. 1 donnie donnie 13 Sep 19 13:32 internal.txt
-rw-rw-r--. 1 donnie donnie 11 Sep 19 13:42 password.txt
-rw-rw-r--. 1 donnie donnie 1708 Sep 19 14:41 pki-server.crt
-rw-r--r--. 1 root root 1320 Sep 20 21:08 pki-server.p12
-rw-rw-r--. 1 donnie donnie 1933891 Sep 19 18:04 yum_list.txt
[donnie@donnie-ca ~]$

Now, let's move on to a couple of very special permissions settings.

..................Content has been hidden....................

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