当前位置:网站首页>Briefly introduce chown command
Briefly introduce chown command
2022-07-02 05:02:00 【Teacher Liu Trent】
Linux Every file and directory on the system is owned by someone , Owners have full control over changing or deleting files they own . In addition to having one owner , The file also has an ownership group .
You can use ls -l command View ownership of files :
[[email protected] Downloads]$ ls -l
total 2454732
-rw-r--r--. 1 pablo pablo 1934753792 Jul 25 18:49 Fedora-Workstation-Live-x86_64-30-1.2.iso
- 1.
- 2.
- 3.
The third and fourth columns of the output are owning users and groups , Together they are called ownership . The one above ISO Documents are both pablo.
Ownership is set by chmod Command to set , Control who is allowed to perform reads 、 Write or run operations . You can use chown Order a change of ownership ( One or both ).
Ownership often needs to change . Files and directories are always in the system , But users are constantly changing . When files and directories move around the system , Or moving from one system to another , Ownership may also need to be changed .
The ownership of files and directories in my home directory is my users and my main groups , With user:group Formal representation of . hypothesis Susan Managing Delta Group , The group needs to edit a file named mynotes The file of . You can use chown Command to change the user of the file to susan, Group changed to delta:
$ chown susan:delta mynotes
ls -l
-rw-rw-r--. 1 susan delta 0 Aug 1 12:04 mynotes
- 1.
- 2.
- 3.
When the file is set Delta Group time , It can be assigned back to me :
$ chown alan mynotes
$ ls -l mynotes
-rw-rw-r--. 1 alan delta 0 Aug 1 12:04 mynotes
- 1.
- 2.
- 3.
Add a colon after the user (:), You can assign users and groups back to me :
$ chown alan: mynotes
$ ls -l mynotes
-rw-rw-r--. 1 alan alan 0 Aug 1 12:04 mynotes
- 1.
- 2.
- 3.
By prefixing the group with a colon , You can change only groups . Now? ,gamma Members of the group can edit the file :
$ chown :gamma mynotes
$ ls -l
-rw-rw-r--. 1 alan gamma 0 Aug 1 12:04 mynotes
- 1.
- 2.
- 3.
chown Some of the additional parameters of can be used on the command line and Script in . Like many others Linux command ,chown There is a recursive parameter (-R), It tells the command to enter the directory to operate on all the files in it . No, -R sign , You can only change the permissions of the folder , It doesn't change the files . In this example , Suppose the purpose is to change the permissions of the directory and all its contents . Here I add -v( detailed ) Parameters , In order to chown Report on their work :
$ ls -l . conf
.:
drwxrwxr-x 2 alan alan 4096 Aug 5 15:33 conf
conf:
-rw-rw-r-- 1 alan alan 0 Aug 5 15:33 conf.xml
$ chown -vR susan:delta conf
changed ownership of 'conf/conf.xml' from alan:alan to susan:delta
changed ownership of 'conf' from alan:alan to susan:delta
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
According to your role , You may need to use sudo To change the ownership of the document .
When changing the ownership of a file to match a specific configuration , Or when you don't know the ownership ( For example, running Script when ), You can use references (--reference=RFILE). for example , You can copy another file (RFILE, It's called reference ) Users and groups , To undo the changes made above . Think about it , spot (.) Represents the current working directory .
$ chown -vR --reference=. conf
- 1.
Report changes
Most commands have parameters to control their output . The most common is -v(--verbose) To enable details , however chown Also has the -c(--changes) Parameters to indicate chown Report only when changes are made .chown There will be other reports , For example, operation not allowed .
Parameters -f(--silent、--quiet) Used to suppress most error messages . In the next section , I will use -f and -c, So that only the actual changes .
Keep the root directory
Linux The root directory of the file system (/) Should be highly valued . If the command makes a mistake at this level , Then the consequence may make the system completely useless . Especially when running a command that will recursively modify or even delete .chown The command has a parameter that can be used to protect and maintain the root , It is --preserve-root. If you use this parameter with recursion in the root directory , Then nothing will happen , But there will be a message :
$ chown -cfR --preserve-root alan /
chown: it is dangerous to operate recursively on '/'
chown: use --no-preserve-root to override this failsafe
- 1.
- 2.
- 3.
If not with --recursive Use a combination of , Then this option is invalid . however , If the order is made by root User run , be / Your own permissions will be changed , But the permissions of other files or directories under it will not change :
$ chown -c --preserve-root alan /
chown: changing ownership of '/': Operation not permitted
[[email protected] /]# chown -c --preserve-root alan /
changed ownership of '/' from root to alan
- 1.
- 2.
- 3.
- 4.
Ownership is security
File and directory ownership is part of good information security , therefore , It's important to occasionally check and maintain file ownership to prevent unnecessary access .chown The order is Linux One of the most common and important commands in the security command set .
边栏推荐
- Ansible installation and use
- 國產全中文-自動化測試軟件Apifox
- Leetcode- insert and sort the linked list
- JS interview collection test question 1
- 06 装饰(Decorator)模式
- Idea automatic package import and automatic package deletion settings
- leetcode两数相加go实现
- LM09丨费雪逆变换反转网格策略
- Differential identities (help find mean, variance, and other moments)
- A new attribute value must be added to the entity entity class in the code, but there is no corresponding column in the database table
猜你喜欢
About PROFIBUS: communication backbone network of production plant
LM09丨费雪逆变换反转网格策略
Analyze the space occupied by the table according to segments, clusters and pages
Promise all()
Idea automatic package import and automatic package deletion settings
[opencv] image binarization
Common errors of dmrman offline backup
List of common bugs in software testing
Application of intelligent robot in agricultural ecology
TypeScript函数详解
随机推荐
10 minute quick start UI automation ----- puppeter
Acelems Expressway microgrid energy efficiency management platform and intelligent lighting solution intelligent lighting tunnel
C # picture display occupancy problem
fastText文本分类
从数组中找出和为目标的下标
Exercise notes 13 (effective letter ectopic words)
Ruby replaces gem Alibaba image
2022 Alibaba global mathematics competition, question 4, huhushengwei (blind box problem, truck problem) solution ideas
Unity particle Foundation
How to modify data file path in DM database
What data does the main account of Zhengda Meiou 4 pay attention to?
Solution of DM database unable to open graphical interface
Embedded-c language-9-makefile/ structure / Consortium
Online incremental migration of DM database
Record my pytorch installation process and errors
Domestic all Chinese automatic test software apifox
DC-1靶场搭建及渗透实战详细过程(DC靶场系列)
Differential identities (help find mean, variance, and other moments)
C# 基于MQTTNet的服务端与客户端通信案例
Solution: the agent throws an exception error