chown命令概述与基本语法
chown命令的作用是将指定文件的所有权变更为用户或组,其中用户可以是名称或ID,组可以是名称或ID;待修改的文件列表以空格分隔,并支持通配符匹配。系统管理员常在将文件复制到其他用户目录下后使用此命令,以便赋予新用户访问权限。
1.命令格式:
chown [选项]... [所有者][:[组]] 文件...
2.命令功能:
利用chown指令调整文件的所有者与所属群组,设置时可采用用户名或用户识别码。出于安全考虑,普通用户无权将自身拥有的文件转让给其他主体,因此该功能的执行权限通常归属于管理员。
chown命令参数详解
3.命令参数:
必要参数:
- -c 仅显示发生实际更改的部分信息
- -f 忽略错误提示信息
- -h 针对符号链接本身进行修改而非其指向目标
- -R 递归处理指定目录及其子目录下的所有文件
- -v 输出详细的处理过程信息
- -deference 作用于符号链接所指向的目标文件,而非链接文件本身
选择参数:
- --reference=<目录或文件> 参照指定目录或文件的属性,将操作对象设置为相同的拥有者和群组
- --from=<当前用户:当前群组> 仅当文件当前的用户和群组与指定值一致时才执行更改
- --help 显示帮助信息
- --version 显示版本信息
chown命令使用实例
4.使用实例:
实例1:改变拥有者和群组
命令:
chown mail:mail log2012.log
输出:
[root@localhost test6]# ll
---xr--r-- 1 root users 302108 11-30 08:39 linklog.log
---xr--r-- 1 root users 302108 11-30 08:39 log2012.log
-rw-r--r-- 1 root users 61 11-30 08:39 log2013.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2014.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2026.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2026.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2026.log
[root@localhost test6]# chown mail:mail log2012.log
[root@localhost test6]# ll
---xr--r-- 1 root users 302108 11-30 08:39 linklog.log
---xr--r-- 1 mail mail 302108 11-30 08:39 log2012.log
-rw-r--r-- 1 root users 61 11-30 08:39 log2013.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2014.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2026.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2026.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2026.log
[root@localhost test6]#
实例2:改变文件拥有者和群组
命令:
chown root: log2012.log
输出:
[root@localhost test6]# ll
总计 604
---xr--r-- 1 root users 302108 11-30 08:39 linklog.log
---xr--r-- 1 mail mail 302108 11-30 08:39 log2012.log
-rw-r--r-- 1 root users 61 11-30 08:39 log2013.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2014.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2026.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2026.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2026.log
[root@localhost test6]# chown root: log2012.log
[root@localhost test6]# ll
总计 604
---xr--r-- 1 root users 302108 11-30 08:39 linklog.log
---xr--r-- 1 root root 302108 11-30 08:39 log2012.log
-rw-r--r-- 1 root users 61 11-30 08:39 log2013.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2014.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2026.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2026.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2026.log
[root@localhost test6]#
实例3:改变文件群组
命令:
chown :mail log2012.log
输出:
[root@localhost test6]# ll
总计 604
---xr--r-- 1 root users 302108 11-30 08:39 linklog.log
---xr--r-- 1 root root 302108 11-30 08:39 log2012.log
-rw-r--r-- 1 root users 61 11-30 08:39 log2013.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2014.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2026.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2026.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2026.log
[root@localhost test6]# chown :mail log2012.log
[root@localhost test6]# ll
总计 604
---xr--r-- 1 root users 302108 11-30 08:39 linklog.log
---xr--r-- 1 root mail 302108 11-30 08:39 log2012.log
-rw-r--r-- 1 root users 61 11-30 08:39 log2013.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2014.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2026.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2026.log
-rw-r--r-- 1 root users 0 11-30 08:39 log2026.log
实例4:改变指定目录以及其子目录下的所有文件的拥有者和群组
命令:
chown -R -v root:mail test6
输出:
[root@localhost test]# ll
drwxr-xr-x 2 root users 4096 11-30 08:39 test6
[root@localhost test]# chown -R -v root:mail test6
“test6/log2014.log” 的所有者已更改为 root:mail
“test6/linklog.log” 的所有者已更改为 root:mail
“test6/log2026.log” 的所有者已更改为 root:mail
“test6/log2013.log” 的所有者已更改为 root:mail
“test6/log2012.log” 的所有者已保留为 root:mail
“test6/log2026.log” 的所有者已更改为 root:mail
“test6/log2026.log” 的所有者已更改为 root:mail
“test6” 的所有者已更改为 root:mail
[root@localhost test]# ll
drwxr-xr-x 2 root mail 4096 11-30 08:39 test6
[root@localhost test]# cd test6
[root@localhost test6]# ll
总计 604
---xr--r-- 1 root mail 302108 11-30 08:39 linklog.log
---xr--r-- 1 root mail 302108 11-30 08:39 log2012.log
-rw-r--r-- 1 root mail 61 11-30 08:39 log2013.log
-rw-r--r-- 1 root mail 0 11-30 08:39 log2014.log
-rw-r--r-- 1 root mail 0 11-30 08:39 log2026.log
-rw-r--r-- 1 root mail 0 11-30 08:39 log2026.log
-rw-r--r-- 1 root mail 0 11-30 08:39 log2026.log
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。