The difference between the chown and chmod commands in Linux

  
        

The difference between chown and chmod:

One, chown

File/directory permission setting command: chmod This is one of the most commonly used commands for Linux system administrators. It is used to change Access to a file or directory.

How to use: chmod [-cfvR] [--help] [--version] mode file…

[-cfvR]Interpretation:----

- c : If the file permission has indeed been changed, the change action is displayed

-f : Do not display the error message if the file permission cannot be changed

-v : Display the details of the permission change Data

-R : Make the same permission changes for all files and subdirectories in the current directory (ie, change them one by one in a recursive manner)

--help : Display auxiliary instructions

--version : Display version

There are two uses for this command:

First, use the text setting method containing letters and operator expressions

The syntax is: chmod [who] [opt] [mode] file/directory name

where who represents an object, which is one or a combination of the following letters:

u: indicates that the file is all

g: indicates the same group of users

o: indicates other users

a: indicates all users

opt is the representative operation, which can be: +: Tim Add a permission -: Cancel a permission =: Give a given permission, and cancel the original permission

mode means permission:

r: Readable

w: writable

x: executable means that only if the file is a subdirectory or the file has been set to executable

For example: add files to the same group of users a .txt read and write permissions: eg: chmod g+rw a.txt

Second, use the number setting method

The number setting rule is simpler: chmod [mode] file name or Chmod UPO file respectively indicates the permissions of User, Group, and Other

The key is the value of mode. In fact, it is very simple. We regard rwx as a binary number. If there is 1, there is 1, if not, there is 0. ,

Then rwx rx r-- can be expressed as: 111 101 100 and then convert each three digits into a decimal number, which is 754.

For example, we want a.txt The permissions of this file are:

Other users of the same group of users

Readable is Yes

Writable is

Executable

Then, we first get the permission string according to the above table: rw-rw-r--, then convert to binary number is 110 110 100,

then every three digits become A decimal number, you get 664,,

So we execute the command: chmod 664 a.txt

For example:

-rw------- (600) -- Only the owner has read and write permissions.

-rw-r--r-- (644) -- Only the owner has read and write permissions; while the group user and other users have read access only.

-rwx------ (700) -- Only the owner has read, write, and execute permissions.

-rwxrwxrwx (777) -- All users have read, write, execute permissions

-rwxrwx--- (770) -- only owners and groups have read and write Execution Permissions

Example Analysis:

chmod Modifying File and Folder Read and Write Execution Properties

This is one of the most commonly used commands for Linux system administrators. To change the access rights of files or directories

1, change the hh.c file to be writable and readable

chmod 777 hh.c

To modify a directory All file attributes are writable and readable executable

chmod 777 *.*

Replace the folder name and suffix with *.

If you want to modify the properties of all htm files

eg:chmod 777 *.htm

2. Change the directory /tmp/sco to writable and readable. Execute

chmod 777 /tmp/sco

To modify all folder properties in a directory to be writable and readable

chmod 777 *

Replace the folder name with *

To modify all files and folders and their subfolder properties under /tmp/sco to be writable and readable

Chmod -R 777 /tmp/sco

writable w=4

readable r=2

executable x=1

777 is owned Full access. User and group permissions can be freely combined as needed

Second, chown

Instruction name: chown

Usage rights: root

Usage: chown [ ,null,null,3],-cfhvR] [--help] [--version] user[:group] file…

chown Modify user permissions for files Modify user and user group properties for files and folders

[ ,null,null,3],-cfvR]Interpretation:----

-c : If the file permissions have indeed been changed, the change action is displayed

-f : Do not display if the file permissions cannot be changed Error message

-v : Display details of permission changes

-R : Make the same permission changes for all files and subdirectories in the current directory (ie, change them one by one in a recursive manner) )

--help : Display helper instructions

--version : Display version

user : ID of the user of the new file owner

Group : The user group of the new file owner

Example analysis:

1. To modify the owner of the file hh.c, Modify to HEHE this user all

chown HEHE hh.c

This will apply hh.c user access to HEHE as owner

2, directory /Tmp/sco The owner and group of this directory are changed to HEHE and group net

chown -R HEHE:net /tmp/sco

eg:chown -R oracle:oinstall /database/-------------------------------------------------- --

Linux/Unix is ​​a multi-person, multi-work system with all files owned by the owner. Use chown to change the owner of the file.

In general, this command is only used by the system administrator (root), the general user does not have the authority to change the file owner of others,

also has no permission to own The file owner is changed to someone else. Only the system administrator (root) has such permission

Copyright © Windows knowledge All Rights Reserved