当前位置: 首页 > article >正文

linux:chown用法详解

文章目录

  • 1. 描述
  • 2. 语法
  • 3. 参数
  • 4. 例子


1. 描述

chown 是 Linux 中用于更改文件或目录的所有者和所有者组的命令。

2. 语法

chown [选项] 所有者[:组] 文件名

详细用法:

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.

3. 参数

以下是chown命令常用的参数选项及其详细解释:

  1. -R, --recursive
    递归地改变文件及其目录内所有子文件和子目录的所有者和组。
    例如:chown -R user:group directory,将会改变directory及其内部所有文件和子目录的所有者和组。

  2. -h, --no-dereference
    只改变符号链接本身的所有者和组,而不是它指向的文件或目录。
    例如:chown -h user:group symlink,只改变符号链接syrmlink的所有者和组。

  3. -f, --silent, --quiet
    抑制错误信息,当chown尝试更改不存在的文件或目录时不会显示错误消息。
    例如:chown -f user:group file,如果file不存在,不会显示错误信息。

  4. -v, --verbose
    显示详细信息,显示更改了哪些文件的所有者和组。
    例如:chown -v user:group file,会输出更改file所有者和组的详细信息。

  5. –from=OWNER[:GROUP]
    只更改当前所有者和组与指定的所有者和组匹配的文件。
    例如:chown --from=user:group newowner:newgroup file,只会更改当前所有者为user且组为groupfile

  6. –reference=REF
    使用引用文件的所有者和组来更改文件。
    例如:chown --reference=ref_file file,将会把file的所有者和组改成与ref_file相同的。

  7. –no-preserve-root
    默认情况下,如果用户不是超级用户(root),则不允许指定/作为文件名。此选项取消此限制。
    例如:chown --no-preserve-root user:group /,允许非root用户尝试更改根目录的所有者和组。

  8. –preserve-root
    强制chown检查是否为根用户(root),如果不是,则拒绝任何尝试更改根目录(/)的行为。这是默认行为。

  9. –help
    显示帮助信息。

  10. –version
    显示版本信息。

使用chown命令时,可以将所有者和组分开指定,也可以同时指定:

  • 只指定所有者:chown newowner file
  • 只指定组:chown :newgroup file
  • 同时指定所有者和组:chown newowner:newgroup file

请注意,更改文件或目录的所有者和组通常需要超级用户权限(root权限),除非更改的对象是你自己拥有的文件或目录。

4. 例子

  1. 更改文件所有者
chown user1 file.txt

file.txt 的所有者更改为 user1

  1. 更改所有者和组
chown user1:group1 file.txt

同时更改文件的所有者为 user1,组为 group1

  1. 递归更改目录及其内容
chown -R user1:group1 /path/to/directory

递归地更改目录和子文件的所有者及组。


http://www.kler.cn/news/321655.html

相关文章:

  • 微调大模型(Finetuning Large Language Models)—Where finetuning fits in(二)
  • Oracle 相关的工具使用 SQL Developer , sqlplus
  • Kotlin:变量声明,null安全,条件语句,函数,类与对象
  • SpringBoot-全局处理异常,时间格式,跨域,拦截器,监听器
  • Brave编译指南2024 MacOS篇-获取源码(三)
  • 如何解决: Java商城系统开发过程中 开发难度大和时间紧的问题
  • python-rpc-windows服务器C#项目远程调用Linux服务器上的python脚本
  • 数据库常见概念
  • React学习笔记(2.0)
  • 【rust】 基于rust编写wasm,实现markdown转换为html文本
  • Lab1 Xv6 and Unix utilities
  • 推荐、nlp、算法题等相关复习(0922-0929)
  • 计算机毕业设计宠物领养网站我的发布领养领养用户信息/springboot/javaWEB/J2EE/MYSQL数据库/vue前后分离小程序
  • HalconDotNet实现OCR详解
  • 比较 Python Web 框架:Django、FastAPI 和 Flask
  • 如何使用 ChatGPT 生成万字长文?
  • verilog中非阻塞多个if 优先级判断。
  • 介绍与部署 Zabbix 监控系统
  • C#知识|基础知识点巩固拾遗
  • MySQL基础知识(二)
  • FBX福币连续2天破万亿,沪指重回3000点,后续怎么走?
  • 学习Java(三)
  • js发送邮件至指定邮箱功能实现方式和技巧?
  • 【系统架构设计师】专题:软件工程基础
  • 2024年9月27日历史上的今天大事件早读
  • 面向对象的三大特性:封装、继承、多态
  • AI/LLM 大模型入门指南
  • 探索EasyCVR视频融合平台:在视频编解码与转码领域的灵活性优势
  • 2024!再见前端!
  • TypeScript 设计模式之【备忘录模式】