In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
This article is about how to use chown in linux. Xiao Bian thinks it is quite practical, so share it for everyone to make a reference. Let's follow the editor and have a look.
Every file and directory on a Linux system is owned by someone, and owners have complete control over changing or deleting files they own. In addition to having an owner user, the file has an owner group.
You can check file ownership using ls -l command:
[pablo@workstation Downloads]$ ls -ltotal 2454732-rw-r--r--. 1 pablo pablo 1934753792 Jul 25 18:49 Fedora-Workstation-Live-x86_64-30-1.2.iso
The third and fourth columns of this output are ownership users and groups, which together are called ownership. The ISO file above is both pablo.
Ownership settings are set by the chmod command, which controls who is allowed to read, write, or run. You can change ownership (one or both) using the chown command.
Ownership often needs to change. Files and directories are always present in the system, but users are constantly changing. Ownership may also need to change as files and directories move across systems or from one system to another.
Ownership of files and directories in my home directory is my users and my primary group, represented as user:group. Suppose Susan is managing a Delta group that needs to edit a file called mynotes. You can change the user of the file to susan and the group to delta using the chown command:
$ chown susan:delta mynotesls -l-rw-rw-r--. 1 susan delta 0 Aug 1 12:04 mynotes
When the Delta group is set for the file, 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
Add a colon (:) after the user to assign both the user and the group back to me:
$ chown alan: mynotes$ ls -l mynotes-rw-rw-r--. 1 alan alan 0 Aug 1 12:04 mynotes
You can change only groups by prepending them with a colon. Members of the gamma group can now edit the file:
$ chown :gamma mynotes$ ls -l-rw-rw-r--. 1 alan gamma 0 Aug 1 12:04 mynotes
Some additional parameters of chown can be used on the command line and in scripts. Like many other Linux commands, chown has a recursive argument (-R) that tells the command to go into a directory to operate on all the files in it. Without the-R flag, you can only change permissions on folders, not the files in them. In this example, assume that the intent is to change permissions on the directory and all of its contents. Here I added the-v (detail) parameter so chown reports how it works:
$ ls -l . conf.: drwxrwxr-x 2 alan alan 4096 Aug 5 15:33 confconf:-rw-rw-r-- 1 alan alan 0 Aug 5 15:33 conf.xml$ chown -vR susan:delta confchanged ownership of 'conf/conf.xml' from alan:alan to susan:deltachanged ownership of 'conf' from alan:alan to susan:delta
Depending on your role, you may need to use sudo to change ownership of files.
Reference files (--reference=RFILE) can be used when changing the ownership of a file to match a particular configuration, or when you don't know ownership (such as when running a script). For example, you can copy users and groups from another file (RFILE, called a reference file) to undo the changes made above. Recall that dot (.) Represents the current working directory.
$ chown -vR --reference=. conf
reporting change
Most commands have parameters that control their output. The most common is-v (--verbose) to enable details, but chown also has the-c (--changes) parameter to indicate that chown reports only when changes are made. Chown also reports other conditions, such as actions that are not allowed.
The parameter-f (--silent, --quiet) is used to suppress most error messages. In the next section, I'll use-f and-c to show only the actual changes.
Keep the root directory
The root directory (/) of the Linux file system should be highly valued. If the command makes a mistake at this level, the consequences could render the system completely useless. Especially if you run a command that recursively modifies or even deletes. The chown command has a parameter that can be used to protect and preserve the root directory, which is--preserve-root. If you use this parameter with recursion in the root directory, nothing happens, but a message appears:
$ chown -cfR --preserve-root alan /chown: it is dangerous to operate recursively on '/'chown: use --no-preserve-root to override this failsafe
This option has no effect unless used in conjunction with--recursive. However, if the command is run by root, the permissions of/itself will be changed, but the permissions of other files or directories under it will not be changed:
$ chown -c --preserve-root alan /chown: changing ownership of '/': Operation not permitted[root@localhost /]# chown -c --preserve-root alan /changed ownership of '/' from root to alan
Ownership is security
File and directory ownership is part of good information security, so it is important to occasionally check and maintain file ownership to prevent unwanted access. The chown command is one of the most common and important commands in the Linux security command set.
After reading the above, do you have any further understanding of how to use chown command? If you want to learn more skills or want to know more about related content, welcome to pay attention to the industry information channel, thank you for reading.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.