Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

A brief introduction to chown command

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Every file and directory on the Linux system is owned by someone, and the owner has complete control over changing or deleting the files they own. In addition to having one owning user, the file also has an owning group.

You can use the ls-l command to view the ownership of the file:

[pablo@workstation Downloads] $ls-ltotal 2454732 RW Murray. 1 pablo pablo 1934753792 Jul 25 18:49 Fedora-Workstation-Live-x86_64-30-1.2.iso

The third and fourth columns of the 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 perform read, write, or run operations. You can use the chown command to change ownership (one or both).

Ownership often needs to be changed. Files and directories always exist in the system, but users are constantly changing. Ownership may also need to change when files and directories are moved in the system, or from one system to another.

The ownership of files and directories in my home directory is my users and my main group, expressed in the form of user:group. Suppose Susan is managing the Delta group, which needs to edit a file named mynotes. You can use the chown command to change the user of the file to susan and the group to delta:

$chown susan:delta mynotesls-1 Murray RW RW Murray RW Murray. 1 susan delta 0 Aug 1 12:04 mynotes

When the Delta group is set for this 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 (:) to the user, and you can assign the user and 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 the group only by adding a colon before the group. Members of the gamma group can now edit the file:

$chown: gamma mynotes$ ls-l Murray RW Murray RW Murray. 1 alan gamma 0 Aug 1 12:04 mynotes

Some of the additional parameters of chown can be used on the command line and in scripts. Like many other Linux commands, chown has a recursive parameter (- R) that tells the command to enter the directory to operate on all the files in it. Without the-R flag, you can only change the permissions of the folder, not the files in it. In this example, it is assumed that the purpose is to change the permissions of the directory and all its contents. Here I added the-v (detailed) parameter so that chown can report its work:

$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 the ownership of the file.

You can use a reference file (--reference=RFILE) when changing the ownership of a file to match a specific configuration, or when you don't know the ownership (for example, when running a script). For example, you can copy the users and groups of another file (RFILE, called a reference file) to undo the changes made above. Recall that the dot (.) indicates the current working directory.

$chown-vR-reference=. Conf

Report changes

Most commands have parameters to control their output. The most common is-v (--verbose) to enable details, but chown also has a-c (--changes) parameter to instruct chown to report 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 the display of 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 may make the system completely useless. Especially when running a command that will be recursively modified or even deleted. The chown command has one parameter that can be used to protect and maintain 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 if it is not used with-- recursive. However, if the command is run by the root user, 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 unnecessary access. The chown command is one of the most common and important commands in the Linux security command set.

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report