Linux基础命令[29]-chown

2024-06-16 00:12
文章标签 基础 linux 命令 29 chown

本文主要是介绍Linux基础命令[29]-chown,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

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

1. chown 命令说明

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

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 isthe 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 theownership of a symlink)--from=CURRENT_OWNER:CURRENT_GROUPchange the owner and/or group of each file only ifits current owner and/or group match those specifiedhere.  Either may be omitted, in which case a matchis 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 thanspecifying OWNER:GROUP values-R, --recursive        operate on files and directories recursivelyThe 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 linkto a directory, traverse it-L                     traverse every symbolic link to a directoryencountered-P                     do not traverse any symbolic links (default)--help     display this help and exit--version  output version information and exitOwner 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 命令语法

chown [OPTION]... [OWNER][:[GROUP]] FILE

3. chown 命令示例

3.1 修改属主

[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 修改属组

修改属组需要加冒号

[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 修改属主和属组

[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 会将该文件夹下所有内容一起修改。

[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:更改文件的用户或用户组,常用命令

chown 属主:属组 文件

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

这篇关于Linux基础命令[29]-chown的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1064977

相关文章

Linux虚拟机不显示IP地址的解决方法(亲测有效)

《Linux虚拟机不显示IP地址的解决方法(亲测有效)》本文主要介绍了通过VMware新装的Linux系统没有IP地址的解决方法,主要步骤包括:关闭虚拟机、打开VM虚拟网络编辑器、还原VMnet8或修... 目录前言步骤0.问题情况1.关闭虚拟机2.China编程打开VM虚拟网络编辑器3.1 方法一:点击还原VM

Windows命令之tasklist命令用法详解(Windows查看进程)

《Windows命令之tasklist命令用法详解(Windows查看进程)》tasklist命令显示本地计算机或远程计算机上当前正在运行的进程列表,命令结合筛选器一起使用,可以按照我们的需求进行过滤... 目录命令帮助1、基本使用2、执行原理2.1、tasklist命令无法使用3、筛选器3.1、根据PID

Linux搭建Mysql主从同步的教程

《Linux搭建Mysql主从同步的教程》:本文主要介绍Linux搭建Mysql主从同步的教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux搭建mysql主从同步1.启动mysql服务2.修改Mysql主库配置文件/etc/my.cnf3.重启主库my

Linux系统之authconfig命令的使用解读

《Linux系统之authconfig命令的使用解读》authconfig是一个用于配置Linux系统身份验证和账户管理设置的命令行工具,主要用于RedHat系列的Linux发行版,它提供了一系列选项... 目录linux authconfig命令的使用基本语法常用选项示例总结Linux authconfi

jdk21下载、安装详细教程(Windows、Linux、macOS)

《jdk21下载、安装详细教程(Windows、Linux、macOS)》本文介绍了OpenJDK21的下载地址和安装步骤,包括Windows、Linux和macOS平台,下载后解压并设置环境变量,最... 目录1、官网2、下载openjdk3、安装4、验证1、官网官网地址:OpenJDK下载地址:Ar

linux本机进程间通信之UDS详解

《linux本机进程间通信之UDS详解》文章介绍了Unix域套接字(UDS)的使用方法,这是一种在同一台主机上不同进程间通信的方式,UDS支持三种套接字类型:SOCK_STREAM、SOCK_DGRA... 目录基础概念本机进程间通信socket实现AF_INET数据收发示意图AF_Unix数据收发流程图A

linux环境openssl、openssh升级流程

《linux环境openssl、openssh升级流程》该文章详细介绍了在Ubuntu22.04系统上升级OpenSSL和OpenSSH的方法,首先,升级OpenSSL的步骤包括下载最新版本、安装编译... 目录一.升级openssl1.官网下载最新版openssl2.安装编译环境3.下载后解压安装4.备份

linux打包解压命令方式

《linux打包解压命令方式》文章介绍了Linux系统中常用的打包和解压命令,包括tar和zip,使用tar命令可以创建和解压tar格式的归档文件,使用zip命令可以创建和解压zip格式的压缩文件,每... 目录Lijavascriptnux 打包和解压命令打包命令解压命令总结linux 打包和解压命令打

linux如何复制文件夹并重命名

《linux如何复制文件夹并重命名》在Linux系统中,复制文件夹并重命名可以通过使用“cp”和“mv”命令来实现,使用“cp-r”命令可以递归复制整个文件夹及其子文件夹和文件,而使用“mv”命令可以... 目录linux复制文件夹并重命名我们需要使用“cp”命令来复制文件夹我们还可以结合使用“mv”命令总

Linux使用cut进行文本提取的操作方法

《Linux使用cut进行文本提取的操作方法》Linux中的cut命令是一个命令行实用程序,用于从文件或标准输入中提取文本行的部分,本文给大家介绍了Linux使用cut进行文本提取的操作方法,文中有详... 目录简介基础语法常用选项范围选择示例用法-f:字段选择-d:分隔符-c:字符选择-b:字节选择--c