Detailed explanation of the difference between chown and chmod commands in Linux

Detailed explanation of the difference between chown and chmod commands in Linux

In Linux system, both chmod and chown commands can be used to set permissions, but they are also different; chmod is used to set folder and file permissions. For example, if the files in our system are not readable or writable, we need to set 777 permissions; while chown is used to set user groups, such as authorizing a certain user group to facilitate the control of user permissions.

The two commands I want to share today are also commonly used in our daily lives. Although chmod and chown seem to have similar spellings, their uses are different. chmod is used to set folder and file permissions. For example, if we want to prevent files from being readable or writable in a VPS host, we need to set permissions to 777. Chown is used to set user groups, such as authorizing a certain user group, to facilitate control of user permissions.

We will use examples to explain the specific usage. I don’t like to share the so-called various parameter commands because they are not used in daily life. Just use the practical ones.

First, usage rights: All users

Usage: chmod [-cfvR] [--help] [--version] mode file...

Note: Linux/Unix file access permissions are divided into three levels: file owner, group, and others. Use chmod to control how files are accessed by others.

mode: permission setting string, the format is as follows: [ugoa...][[+-=][rwxX]...][,...] , where u represents the owner of the file, g represents people who belong to the same group as the owner of the file, o represents other people, and a represents all three.

  • + means adding permissions, - means canceling permissions, and = means setting permissions uniquely.
  • r means readable, w means writable, x means executable, and X means executable only when the file is a subdirectory or the file has been
  • Set as executable.
  • -c : If the file permissions have actually been changed, the change action will be displayed
  • -f : Do not display an error message if the file permissions cannot be changed
  • -v : Display detailed information about permission changes
  • -R: Make the same permission changes to all files and subdirectories in the current directory (i.e., change them one by one in a recursive manner)
  • --help : Display help instructions
  • --version : Display version

For example: chmod -R 777 /www/itbulu.com/wp-content/*

It means that all files in the above folder are set to be readable and writable.

cd wp-content
chmod -R 777 *

Second, command name: chown

User permissions: root

Usage: chown [-cfhvR] [--help] [--version] user[:group] file...

Note: Linux/Unix is ​​a multi-person, multi-tasking operating system, and all files have owners. Use chown to change the owner of a file. Generally speaking, this command is only used by the system administrator (root). Ordinary users do not have the authority to change the owner of other people's files, nor do they have the authority to change the owner of their own files to someone else. Only the system administrator (root) has such permission.

  • user : the user ID of the new file owner
  • group : the user group of the new file owner
  • -c or -change: Similar to -v, but only returns the modified part
  • -f or –quiet or –silent: Do not display error messages
  • -h or –no-dereference: only modify the symbolic link file without changing any other related files
  • -R or -recursive: recursive processing, all files and subdirectories under the specified directory are processed together
  • -v or –verbose: Displays the execution process of commands
  • –dereference: The effect is exactly the opposite of -h
  • –help: Display online help
  • –reference=<reference file or directory>: Set the owner and group of the specified file or directory to the same as the owner and group of the reference file or directory
  • –version: Display version information

chown -R www:www /home/wwwroot/*

Explanation: -R recursively processes all files and folders. The first www represents the owner name of the file, and the second www represents the group name to which it belongs.

Difference between chown and chmod commands

chown changes the user and group attributes of files and folders

1. To change the owner of the file hh.c, change it to the user codetc

chown codetc hh.c

This applies the user access rights of hh.c to codetc as the owner

2. Change the owner and group of the directory /tmp/sco to codetc and group net

chown -R codetc:net /tmp/sco

chmod changes the read-write and execute attributes of files and folders

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

chmod 777 hh.c

To change the attributes of all files in a directory to be writable, readable and executable

chmod 777 *.*

Just replace the folder name and suffix with *.

Similarly, if you want to modify the properties of all htm files

chmod 777 *.htm

2. Change the directory /tmp/sco to be writable, readable and executable

chmod 777 /tmp/sco

To modify the properties of all folders in a directory to be writable, readable and executable

chmod 777 *

Just replace the folder name with *

To modify the attributes of all files and folders under /tmp/sco and their subfolders to be writable, readable and executable

chmod -R 777 /tmp/sco

Can write w=4

Readable r=2

Executable x=1

777 means full authority. You can freely combine the permissions of users and groups as needed

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. If you want to learn more about this, please check out the following links

You may also be interested in:
  • Detailed Analysis of the chmod Command to Change File Permissions in Linux
  • Modify Linux file permissions command: chmod command detailed explanation
  • One shell command a day Linux text operation series - chmod command usage
  • Linux imitation chmod command
  • Detailed explanation of chmod command usage in Linux

<<:  MySQL optimization: use join instead of subquery

>>:  js canvas realizes random particle effects

Recommend

What are the benefits of semantic HTML structure?

one: 1. Semantic tags are just HTML, there is no ...

JavaScript realizes magnifying glass special effects

The effect to be achieved: When the mouse is plac...

mysql backup script and keep it for 7 days

Script requirements: Back up the MySQL database e...

MySQL uses SQL statements to modify table names

In MySQL, you can use the SQL statement rename ta...

Methods for deploying MySQL services in Docker and the pitfalls encountered

I have been learning porters recently. I feel lik...

Detailed steps to install the specified version of docker (1.12.6) using rpm

1. Reasons If the system is Centos7.3, the Docker...

React uses routing to redirect to the login interface

In the previous article, after configuring the we...

TortoiseSvn Little Turtle Installation Latest Detailed Graphics Tutorial

There were always problems when installing tortoi...

Sample code for seamless scrolling with flex layout

This article mainly introduces the sample code of...

How to detect file system integrity based on AIDE in Linux

1. AIDE AIDE (Advanced Intrusion Detection Enviro...

MySQL data types full analysis

Data Type: The basic rules that define what data ...

MySQL 5.7.17 compressed package installation-free configuration process diagram

There are two versions of MySQL database manageme...

Sharing of the fast recovery solution for Mysql large SQL files

Preface In the process of using MySQL database, i...