Linux系统之chown命令的基本使用
一、chown命令介绍
chown 命令在 Linux 系统中用于更改文件或目录的所有者和/或所属组。这个命令对于系统管理员来说是非常有用的,因为它允许他们管理文件的权限,确保只有授权用户才能访问特定的文件或目录。
二、chown命令的使用帮助
2.1 chown命令help帮助信息
在命令行终端中,我们使用
--help
查询chown命令的基本帮助信息。
root@jeven01:~# chown --help
Usage: chown [OPTION]... [OWNER][:[GROUP]] FILE...
or: chown [OPTION]... --reference=RFILE FILE...
Change the owner and/or group of each FILE to OWNER and/or GROUP.
With --reference, change the owner and group of each FILE to those of RFILE.
-c, --changes like verbose but report only when a change is made
-f, --silent, --quiet suppress most error messages
-v, --verbose output a diagnostic for every file processed
--dereference affect the referent of each symbolic link (this is
the default), rather than the symbolic link itself
-h, --no-dereference affect symbolic links instead of any referenced file
(useful only on systems that can change the
ownership of a symlink)
--from=CURRENT_OWNER:CURRENT_GROUP
change the owner and/or group of each file only if
its current owner and/or group match those specified
here. Either may be omitted, in which case a match
is not required for the omitted attribute
--no-preserve-root do not treat '/' specially (the default)
--preserve-root fail to operate recursively on '/'
--reference=RFILE use RFILE's owner and group rather than
specifying OWNER:GROUP values
-R, --recursive operate on files and directories recursively
The following options modify how a hierarchy is traversed when the -R
option is also specified. If more than one is specified, only the final
one takes effect.
-H if a command line argument is a symbolic link
to a directory, traverse it
-L traverse every symbolic link to a directory
encountered
-P do not traverse any symbolic links (default)
--help display this help and exit
--version output version information and exit
Owner is unchanged if missing. Group is unchanged if missing, but changed
to login group if implied by a ':' following a symbolic OWNER.
OWNER and GROUP may be numeric as well as symbolic.
Examples:
chown root /u Change the owner of /u to "root".
chown root:staff /u Likewise, but also change its group to "staff".
chown -hR root /u Change the owner of /u and subfiles to "root".
GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation <https://www.gnu.org/software/coreutils/chown>
or available locally via: info '(coreutils) chown invocation'
2.2 chown命令帮助解释
- 语法
chown [选项]... [所有者][:[组]] 文件...
或
chown [选项]... --reference=参考文件 文件...
- 选项
选项 | 描述 |
---|---|
-c, --changes |
类似于详细模式,但仅在更改发生时报告 |
-f, --silent, --quiet |
抑制大多数错误消息 |
-v, --verbose |
对处理的每个文件输出诊断信息 |
--dereference |
影响每个符号链接的目标文件(这是默认行为),而不是符号链接本身 |
-h, --no-dereference |
只影响符号链接,而不影响被引用的任何文件(仅当系统支持更改符号链接的所有者时才有用) |
--from=当前所有者:当前所属组 |
只有当文件的当前所有者和/或组与指定的匹配时才更改所有者和/或组。可以省略其中一个属性 |
--no-preserve-root |
不特殊对待“/”(默认行为) |
--preserve-root |
不允许在“/”上递归操作 |
--reference=<参考文件> |
使用指定<参考文件>的所有者和所属组信息,而非手工指定所有者:组的值 |
-R, --recursive |
递归地对文件和目录进行操作 |
在指定了 -R
选项时用于设置如何遍历目录结构体系的选项。如果指定了多于一个选项,那么只有最后一个会生效。
选项 | 描述 |
---|---|
-H |
如果命令行参数是一个指向目录的符号链接,则对其遍历 |
-L |
遍历每一个遇到的指向目录的符号链接 |
-P |
不遍历任何符号链接(默认) |
其他选项 | 描述 |
---|---|
--help |
显示帮助信息并退出 |
--version |
输出版本信息并退出 |
如果没有指定所有者,则不会更改所有者信息。若所属组没有指定也不会对其更改,但如果加上 :
,GROUP 会更改为指定所有者的主要组。所有者和所属组可以是数字或名称。
三、chown命令的基本使用
3.1 改变所有者
要改变一个文件或目录的所有者,可参考以下方式:
- 改变文件的所有者
root@jeven01:~# chown admin /test/test.file
root@jeven01:~#
root@jeven01:~# ll /test/test.file
-rw-r--r-- 1 admin root 2097152 Oct 3 20:35 /test/test.file
- 改变目录的所有者
root@jeven01:~# chown admin /test/
root@jeven01:~# ll -d /test
drwxr-xr-x 2 admin root 4096 Oct 3 20:57 /test/
3.2 改变所有组
要某个文件或者目录的所有者和所有组,例如修改/test目录的所有组也为admin,可参考以下命令:
root@jeven01:~# chown admin:admin /test
root@jeven01:~# ll -ld /test
drwxr-xr-x 2 admin admin 4096 Oct 3 20:57 /test/
3.3 递归地改变所有者和组
使用-R选项,递归地改变所有者和组。
chown -R admin:admin /test/
3.4 显示执行过程
加上-v选项,显示执行过程。
root@jeven01:~# chmod -Rv admin:admin /test/
chmod: invalid mode: ‘admin:admin’
Try 'chmod --help' for more information.
root@jeven01:~# chown -Rv admin:admin /test/
changed ownership of '/test/split_file010' from root:root to admin:admin
changed ownership of '/test/split_file001' from root:root to admin:admin
changed ownership of '/test/aa' from root:root to admin:admin
changed ownership of '/test/split_file006' from root:root to admin:admin
changed ownership of '/test/split_file000' from root:root to admin:admin
changed ownership of '/test/dd' from root:root to admin:admin
changed ownership of '/test/cc' from root:root to admin:admin
changed ownership of '/test/split_file007' from root:root to admin:admin
changed ownership of '/test/split_file009' from root:root to admin:admin
changed ownership of '/test/bb' from root:root to admin:admin
changed ownership of '/test/split_file005' from root:root to admin:admin
changed ownership of '/test/split_file008' from root:root to admin:admin
changed ownership of '/test/split_file004' from root:root to admin:admin
changed ownership of '/test/split_file002' from root:root to admin:admin
changed ownership of '/test/test.file' from admin:root to admin:admin
changed ownership of '/test/split_file003' from root:root to admin:admin
ownership of '/test/' retained as admin:admin
四、注意事项
- 权限要求:通常需要超级用户权限(如使用
sudo
)来更改文件或目录的所有者和组。 - 谨慎操作:在修改系统关键文件或目录的所有权之前,请确保了解该操作可能带来的影响。
- 递归选项小心使用:使用
-R
选项时要特别小心,因为它会递归地改变指定目录及其所有子目录下的文件所有权。 - 符号链接处理:使用
-h
或--no-dereference
选项可以只改变符号链接本身的所有权而不影响其指向的文件。 - 避免更改根目录:不要轻易尝试更改根目录
/
的所有权,这可能导致系统不稳定或无法启动。 - 验证更改:在执行
chown
后,应通过ls -l
或其他方法确认文件所有权确实已按预期更改。