Linux Change File or Directory Permissions

Posted by Mordred on Mon, 24 Jun 2019 01:51:56 +0200

chmod (change mode) change permissions

  • What do you think of permissions?

[root@allinlinux-01 ~]# Total dosage of ls-l 8-rw-r--r--.1 root June 8 14:16 2.txt
-rw-------. 1 root root 7728 6 month   6 22:11 anaconda-ks.cfg.1

- rw-r--r--. This paragraph, except for the first one, represents the file type and the last one. It indicates whether the file is restricted by SELinux * (SELinux will not have this point if a new file/directory is created after disabled, otherwise there will be)*. The nine digits in the middle represent the permissions of the file or folder.

  • Decomposition of nine permissions, each three into a paragraph

The first three represent the permissions of the owner of the file/directory.

The middle three represent the permissions of all members of the group to which the file/directory belongs.

The last three represent the access rights of other users to the file/directory other than the owner and the group they belong to.

  • Explain rwx(- where the symbol is, indicating that there is no corresponding permission)

R: read - > read w: write - > write x: execute - > execute

- rw-r--r--. 1 Root root 8:14:162.txt

Root users have r and W privileges on 2.txt files, root groups have r privileges on 2.txt files without x privileges, other users have r privileges on 2.txt files without W and X privileges, and no w and X privileges on 2.txt files.

  • rwx can also be represented by numbers

The sum of the numbers of each paragraph of the second power of X - > 2, W - > 2, W - > 1, R - > 2, gives the corresponding user's permission number for the file, RW - R - - equal to 644.

  • Change File/Directory Permissions

  1. Permission to use digital representation

[root@allinlinux-01 ~]# chmod 700 2.txt[root@allinlinux-01 ~]# Total dosage of ls-l 8-rwx - - - 1. Root Root June 8 14:16 2.txt
-rw-------. 1 root root 7728 6 month   6 22:11 anaconda-ks.cfg.1
2. Permissions represented by r,w,x
[root@allinlinux-01 ~]# chmod u=rw,g=rx,o=x 2.txt[root@allinlinux-01 ~]# Total dosage of ls-l 8-rw-r-x--x.1 root June 8 14:16 2.txt
-rw-------. 1 root root 7728 6 month   6 22:11 anaconda-ks.cfg.1
> u --> user> g --> group> o --> other

**== Special attention: In this way, you can't write u = rw-, g = r-x, o= - x, which can't appear in the middle, although sometimes you can, but it's unreasonable to write this way. As you will see later, there is a way to modify permissions by using "+" or "-" numbers to control the increase and decrease of permissions. = ** 3. Use + numbers to control permissions.
[root@allinlinux-01 ~]# chmod a+x 2.txt[root@allinlinux-01 ~]# Total dosage of ls-l 8-rwxr-x--x.1 root June 8 14:16 2.txt
> A - > all, a + x, all users add x privileges to files / directories.
[root@allinlinux-01 ~]# chmod a-r 2.txt[root@allinlinux-01 ~]# Total dosage of ls-l 8-wx-x-x.1 root June 8-14:16 2.txt
> Reduction of r privileges for files/directories by all users
[root@allinlinux-01 ~]# chmod u+r 2.txt[root@allinlinux-01 ~]# Total dosage of ls-l 8-rwx-x-x.1 root June 8 14:16 2.txt
> For user s alone, increase r permissions.
  • - R option

Modify permissions to take effect for all files and subdirectories in the directory

[root@allinlinux-01 ~]# tree /tmp/allinlinux//tmp/allinlinux/
└── 1.txt0 directories, 1 file
[root@allinlinux-01 ~]# Ls-l/tmp/allinlinux/total dosage 0-rw-r-r--.1 Root June 0 8 14:58 1.txt
[root@allinlinux-01 ~]# Ls-l/tmp/allinlinux/1.txt-rw-r-r--.1 Root June 0 8 14:58/tmp/allinlinux/1.txt

/ There is a 1.txt file allinlinux/directory in tmp/allinlinux directory. The permission of the directory is rw-r--r--1.txt file is rw-r--r--

[root@allinlinux-01 ~]# chmod -R 777 /tmp/allinlinux/[root@allinlinux-01 ~]# Ls-l/tmp/allinlinux/total dosage 0-rwxrwxrwx.1 Root June 0 8 14:58 1.txt
[root@allinlinux-01 ~]# Ls-l/tmp/allinlinux/1.txt-rwxrwxrwx.1 Root June 0 8 14:58/tmp/allinlinux/1.txt

/ The permissions of the allinlinux / directory and the files therein have been changed.


Topics: Anaconda SELinux