Permission management commands in linux (chmod/chown/chgrp/unmask)

Keywords: Linux vim shell

Catalog

  • chmod
  • chown
  • chgrp
  • umask

chmod

explain

Command name: chmod
 change the permissions mode of a file
 Command path / bin/chmod
 Execute permission: all users
 Function Description: change file or directory permissions

grammar

chmod [{ugoa}{+-=}{rwx}] [File or directory] 
chmod [mode=421] [File or directory]
    -R Recursive modification
    
# The first way to modify chmod [{ugoa}{+-=}{rwx}] [file or directory]
ugoa:
    u:owner
    g:Subordinate group
    o:Someone else
    a:All
+-=:
    +:Add a permission to a file or directory
    -:Reduce a permission for a file or directory
    =:Give new permissions to files or directories,Subject to the authority at this time
    
# chmod [mode=421] [file or directory]
rwx:
    r:4
    w:2
    x:1
rwxrw-r--
    //Authority: 764 (4 + 2 + 1 = 7 / 4 + 2 = 6 / 4)

Example

# The first way to add permissions
 chmod g+x test.txt
 
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls -l test.txt
-rw-r--r-- 1 root root 11 Nov 28 15:39 test.txt
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# chmod g+x test.txt
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls -l test.txt
-rw-r-xr-- 1 root root 11 Nov 28 15:39 test.txt


# The second way to increase authority
chmod 777 test.txt

[root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls -l test.txt
-rw-r-xr-- 1 root root 11 Nov 28 15:39 test.txt
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# chmod 777 test.txt
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls -l test.txt
-rwxrwxrwx 1 root root 11 Nov 28 15:39 test.txt

Special attention to authority

When the root user enters, it will be displayed, and the normal user will be displayed$

# Create a new folder test under / tmp
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# mkdir test

# Create a new test.txt in the / tmp/test folder
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# touch test/test.txt

# View the files under the test file
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls -l test
total 0
-rw-r--r-- 1 root root 0 Nov 28 17:54 test.txt

# Permissions to view the / tmp/test folder
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls -ld test
drwxr-xr-x 2 root root 4096 Nov 28 17:54 test

# Give all permissions to / tmp/test folder
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# chmod 777 test
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls -ld test
drwxrwxrwx 2 root root 4096 Nov 28 17:54 test

[root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls -l test/test.txt
-rw-r--r-- 1 root root 0 Nov 28 17:54 test/test.txt

# Add a new normal user and change the password
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# useradd eternity
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# passwd eternity




# Log in to the server with the identity account, password 123456
# View current directory
[eternity@izm5e2q95pbpe1hh0kkwoiz ~]$ pwd
/home/eternity

# Enter / tmp directory
[eternity@izm5e2q95pbpe1hh0kkwoiz ~]$ cd /tmp

# View the permissions of / tmp/test directory and have all permissions
[eternity@izm5e2q95pbpe1hh0kkwoiz tmp]$ ls -ld test
drwxrwxrwx 2 root root 4096 Nov 28 17:54 test

# /test.txt exists in tmp/test directory and has read permission
[eternity@izm5e2q95pbpe1hh0kkwoiz tmp]$ ls -l test/test.txt
-rw-r--r-- 1 root root 0 Nov 28 17:54 test/test.txt

# Delete the test.txt file under / tmp/test
[eternity@izm5e2q95pbpe1hh0kkwoiz tmp]$ rm test/test.txt
rm: remove write-protected regular empty file 'test/test.txt'? y

# The deletion succeeded. At this time, the test.txt in the / tmp/test directory is no longer available
[eternity@izm5e2q95pbpe1hh0kkwoiz tmp]$ ls -l test/test.txt
ls: cannot access test/test.txt: No such file or directory

Only the administrator has the rw read / write permission, and the group and others have the read permission. But at this time, ordinary users delete the files with the r read permission. Why????

Summary of file directory permissions

Representative character Jurisdiction Meaning of documents Meaning of contents
r Read permission Can view file content You can list the contents of a catalog
w Write permission File content can be modified You can create and delete files in a directory
x Executive authority Executable Can enter the directory

Analysis

If you have write permission for a file, it only means that you can modify the contents of the file, but not delete the file

You have write permission to the directory. You can create and delete files in the directory

Because the permissions of the / tmp/test directory above are 777
 Therefore, ordinary users also have the right to create and delete files for the / tmp/test directory
 Therefore, ordinary users can also delete the / tmp/test/test.txt file
 However, ordinary users cannot edit the / tmp/test/test.txt file. When using vim to edit the file, they will be prompted "warning: changing a readonly file"

chown

explain

Command name: chown
 change file ownership
 Command path / bin/chown
 Execute permission: all users
 Function Description: change the owner of a file or directory

grammar

chown [user] [file or directory]

In linux, only root can change the owner of the file, not even the Creator

Example

# Change the owner of the file (change the owner of test.txt from endurance to root)
chown root /tmp/test/test.txt

[root@izm5e2q95pbpe1hh0kkwoiz ~]# pwd
/root
[root@izm5e2q95pbpe1hh0kkwoiz ~]# ls -l /tmp/test/test.txt
-rw-r--r-- 1 eternity eternity 7 Nov 28 18:15 /tmp/test/test.txt
[root@izm5e2q95pbpe1hh0kkwoiz ~]# chown root /tmp/test/test.txt
[root@izm5e2q95pbpe1hh0kkwoiz ~]# ls -l /tmp/test/test.txt
-rw-r--r-- 1 root eternity 7 Nov 28 18:15 /tmp/test/test.txt

chgrp

explain

Command name: chgrp
 change file group ownership
 Command path / bin/chgrp
 Execute permission: all users
 Function Description: change the group of file or directory

grammar

chgrp [user group] [file or directory]

Example

# Change the group of the file (change the group of test.txt from tenentity to tenentityz)
chgrp eternityz /tmp/test/test.txt

# current directory
[root@izm5e2q95pbpe1hh0kkwoiz ~]# pwd
/root
# View details
[root@izm5e2q95pbpe1hh0kkwoiz ~]# ls -l /tmp/test/test.txt
-rw-r--r-- 1 root eternity 7 Nov 28 18:15 /tmp/test/test.txt
# Adding the etrentityz group
[root@izm5e2q95pbpe1hh0kkwoiz ~]# groupadd eternityz
# Change group
[root@izm5e2q95pbpe1hh0kkwoiz ~]# chgrp eternityz /tmp/test/test.txt
[root@izm5e2q95pbpe1hh0kkwoiz ~]# ls -l /tmp/test/test.txt
-rw-r--r-- 1 root eternityz 7 Nov 28 18:15 /tmp/test/test.txt

umask

explain

Command name: umask
 The user file creation mask
 Command path: shell built-in command
 Execute permission: all users
 Function Description: display / set default permissions of files

grammar

umask [-S]
    -S with rwx Default permissions for new files(Capitalized S)

Example

# Default permissions for viewing files
umask -S

# View umask
umask

[root@izm5e2q95pbpe1hh0kkwoiz ~]# umask
0022

0022 in
0   special competencies
022 ----w--w-

# Exclusive or operation is performed through all permissions 777 and 022, and default permissions are obtained
777  rwx rwx rwx
022  --- -w- -w-
================
//Directory rwx r-x r-x
//File rwx r-- r--


# Change the umask value to change the default permissions
umask 077

# After changing the umask value, the default permissions change to
777  rwx rwx rwx
077  --- rwx rwx
================
//Directory rwx ------
//File rw ------

# The following experiments match the settings for changing the default permissions
[root@izm5e2q95pbpe1hh0kkwoiz ~]# umask 077
[root@izm5e2q95pbpe1hh0kkwoiz ~]# mkdir /tmp/lyf
[root@izm5e2q95pbpe1hh0kkwoiz ~]# ls -ld /tmp/lyf
drwx------ 2 root root 4096 Nov 29 10:55 /tmp/lyf
[root@izm5e2q95pbpe1hh0kkwoiz ~]# touch /tmp/lyf/lyf
[root@izm5e2q95pbpe1hh0kkwoiz ~]# ls -l /tmp/lyf/lyf
-rw------- 1 root root 0 Nov 29 10:56 /tmp/lyf/lyf

In linux, only root can change the owner of the file, not even the Creator

The creator of the file is the default owner, and the default group is also the creator of the file

The default permission of the folder in linux is rwxrxrxrx. The default permission of the file is rw-r--r --. The newly created file does not have executable permission

Posted by sir nitr0z on Fri, 28 Feb 2020 00:48:39 -0800