Linux基础命令[29]-chown

文章目录

    • [1. chown 命令说明](#1. chown 命令说明)
    • [2. chown 命令语法](#2. chown 命令语法)
    • [3. chown 命令示例](#3. chown 命令示例)
      • [3.1 修改属主](#3.1 修改属主)
      • [3.2 修改属组](#3.2 修改属组)
      • [3.3 修改属主和属组](#3.3 修改属主和属组)
      • [3.4 修改文件夹所属](#3.4 修改文件夹所属)
    • [4. 总结](#4. 总结)

1. chown 命令说明

chown:更改文件的用户或用户组,需要 root 用户或 sudo 权限的用户执行该命令。基本信息如下:

shell 复制代码
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: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'chown invocation'

参数如下:

选项 作用
-c 只有真正更改时才会显示信息
-f 忽略错误信息
-h 修复符号链接
-v 显示详细的处理信息
-R 处理文件夹及文件夹中所有文件、子文件夹
-H 配合 -R ,如果命令行参数是指向目录的符号链接,则遍历它
-L 配合 -R ,遍历遇到的每个指向目录的符号链接
-P 配合 -R ,不遍历任何符号链接(默认)

2. chown 命令语法

shell 复制代码
chown [OPTION]... [OWNER][:[GROUP]] FILE

3. chown 命令示例

3.1 修改属主

shell 复制代码
[root@localhost aaa]# ll
total 0
-r--rw-r--. 1 root root  0 May 17 08:52 a.txt
drwxrwxrwx. 3 root root 30 Jun  3 12:44 bbb
[root@localhost aaa]# chown test1 a.txt 
[root@localhost aaa]# ll
total 0
-r--rw-r--. 1 test1 root  0 May 17 08:52 a.txt
drwxrwxrwx. 3 root  root 30 Jun  3 12:44 bbb
[root@localhost aaa]# 

3.2 修改属组

修改属组需要加冒号

shell 复制代码
[root@localhost aaa]# chown :test1 a.txt 
[root@localhost aaa]# ll
total 0
-r--rw-r--. 1 test1 test1  0 May 17 08:52 a.txt
drwxrwxrwx. 3 root  root  30 Jun  3 12:44 bbb
[root@localhost aaa]# id test2
uid=1015(test2) gid=1015(test2) groups=1015(test2)
[root@localhost aaa]# chown 1015 a.txt 
[root@localhost aaa]# ll
total 0
-r--rw-r--. 1 test2 test1  0 May 17 08:52 a.txt
drwxrwxrwx. 3 root  root  30 Jun  3 12:44 bbb
[root@localhost aaa]# 

3.3 修改属主和属组

shell 复制代码
[root@localhost aaa]# ll
total 0
-r--rw-r--. 1 test2 test1  0 May 17 08:52 a.txt
drwxrwxrwx. 3 root  root  30 Jun  3 12:44 bbb
[root@localhost aaa]# chown test1:test2 a.txt 
[root@localhost aaa]# ll
total 0
-r--rw-r--. 1 test1 test2  0 May 17 08:52 a.txt
drwxrwxrwx. 3 root  root  30 Jun  3 12:44 bbb
[root@localhost aaa]# 

3.4 修改文件夹所属

修改文件夹时,若不加 -R 选项,会只修改该文件夹的所属,加 -R 会将该文件夹下所有内容一起修改。

shell 复制代码
[root@localhost aaa]# ll
total 0
-r--rw-r--. 1 test1 test2  0 May 17 08:52 a.txt
drwxrwxrwx. 3 root  root  30 Jun  3 12:44 bbb
[root@localhost aaa]# chown test1:test1 bbb/
[root@localhost aaa]# ll
total 0
-r--rw-r--. 1 test1 test2  0 May 17 08:52 a.txt
drwxrwxrwx. 3 test1 test1 30 Jun  3 12:44 bbb
[root@localhost aaa]# cd bbb/
[root@localhost bbb]# ll
total 0
-rwxrwxrwx. 1 root root 0 Jun  3 12:44 1.txt
drwxrwxrwx. 2 root root 6 Jun  3 12:38 ccc
[root@localhost bbb]# cd ..
[root@localhost aaa]# chown -R test1:test1 bbb/
[root@localhost aaa]# cd bbb/
[root@localhost bbb]# ll
total 0
-rwxrwxrwx. 1 test1 test1 0 Jun  3 12:44 1.txt
drwxrwxrwx. 2 test1 test1 6 Jun  3 12:38 ccc
[root@localhost bbb]# 

4. 总结

chown:更改文件的用户或用户组,常用命令

shell 复制代码
chown 属主:属组 文件

若是想同步改变文件夹的内容所属,则加上 -R。

相关推荐
LCG元7 分钟前
性能排查必看!当Linux服务器CPU/内存飙高,如何快速定位并"干掉"罪魁祸首进程?
linux·后端
路由侠内网穿透9 分钟前
本地部署开源数据分析平台 Elastic Stack 并实现外部访问( Windows 版本)
运维·服务器·网络·windows·开源·jenkins
回忆是昨天里的海15 分钟前
k8s安装-kubeadm join,将工作节点加入k8s集群
java·服务器·kubernetes
TG_yunshuguoji1 小时前
亚马逊云代理:AWS的EC2, S3, RDS,Lambda具体简介
服务器·云计算·aws
王道长服务器 | 亚马逊云1 小时前
AWS CloudFormation —— 自动化部署的“云中脚本大师”
运维·服务器·网络·自动化·云计算·aws
christine-rr1 小时前
MySQL数据库管理、DDL、DQL、DML、DCL等总结
linux·数据库·mysql
奥尔特星云大使1 小时前
CentOS 7 上通过 RPM 包安装 Zabbix 4.x
linux·centos·zabbix
Bruce_Liuxiaowei1 小时前
解决Kali虚拟机中VMnet1(仅主机模式)网卡无法获取IP地址的问题
运维·网络·网络协议·tcp/ip
key_Go1 小时前
12.docker swarm
运维·docker·容器·docker swarm
程序员勾践1 小时前
安装nginx
linux·nginx·centos