Linux基本功系列之chown命令实战
创始人
2024-05-15 10:20:35
0

在这里插入图片描述

文章目录

  • 一. 前言🚀🚀🚀
  • 二. chown命令介绍
  • 三. 语法格式及常用选项
  • 四. 参考案例
    • 3.1 改变指定文件的属组和属主
    • 3.2 改变指定文件的所属主与所属组,并显示过程
    • 3.3 改变指定目录及其内所有子文件的所属主与所属组
    • 3.4 只修改文件所属组为mufeng组
    • 3.5 只修改属主
  • 五. 总结

一. 前言🚀🚀🚀

想要学好Linux,命令是基本功,企业中常用的命令大约200多个,不管是写shell脚本还是管理操作系统,最常用的命令必须要牢牢掌握,像我们以前学乘法口诀一样,烂熟于心,唯有如此,才能打牢基础。
💓 知识最重要的是记忆
💓 入门须知: 想要人生从容,必须全力以赴,努力才是你最终的入场券🚀🚀🚀
💕 最后: 努力成长自己,愿我们都能在看不到的地方闪闪发光 ,一起加油进步🍺🍺🍺

二. chown命令介绍

Linux/Unix 属于多用户多任务操作系统,所有的文件皆有拥有者。
使用chown 命令可以将指定的文件的拥有着改为指定的用户和组。

chown命令来自于英文词组”Change owner“的缩写,其功能是用于改变文件或目录的用户和用户组信息。

管理员可以改变一切文件的所属信息,而普通用户只能改变自己文件的所属信息

三. 语法格式及常用选项

使用–help来参看具体的参数和格式:

[root@mufenggrow ~]# chown --help
用法:chown [选项]... [所有者][:[组]] 文件...或:chown [选项]... --reference=参考文件 文件...
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=当前所有者:当前所属组只当每个文件的所有者和组符合选项所指定时才更改所有者和组。其中一个可以省略,这时已省略的属性就不需要符合原有的属性。--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		显示此帮助信息并退出--version		显示版本信息并退出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.示例:chown root /u		将 /u 的属主更改为"root"。chown root:staff /u	和上面类似,但同时也将其属组更改为"staff"。chown -hR root /u	将 /u 及其子目录下所有文件的属主更改为"root"。

从上面的代码实例中可以看到,常用的几个参数如下:

在这里插入图片描述
其中最常用的是 -R参数,对目录及目录下所有文件进行变更。

四. 参考案例

3.1 改变指定文件的属组和属主

创建一个文件a.txt,同时把属组和属主改为 lp用户

[root@mufenggrow ~]# mkdir test
[root@mufenggrow ~]# cd test
[root@mufenggrow test]# touch a.txt
[root@mufenggrow test]# ll a.txt
-rw-r--r--. 1 root root 0 1月  22 14:33 a.txt
[root@mufenggrow test]# chown lp:lp a.txt
[root@mufenggrow test]# ll a.txt
-rw-r--r--. 1 lp lp 0 1月  22 14:33 a.txt

使用ll查看发现a.txt的所有者和所有组变成了lp

3.2 改变指定文件的所属主与所属组,并显示过程

-c 参数 like verbose but report only when a change is made
在更改时显示详细的报告

在这里插入图片描述
set verbos 设置打印信息

[root@mufenggrow test]# chown -c root:root a.txt
changed ownership of "a.txt" from lp:lp to root:root
[root@mufenggrow test]# ll a.txt
-rw-r--r--. 1 root root 0 1月  22 14:33 a.txt
[root@mufenggrow test]# 

这里的 ownership 指的是所有权

在这里插入图片描述

3.3 改变指定目录及其内所有子文件的所属主与所属组

当一个目录中有多个文件的时候,我们想要改变文件及文件内部的所有文件的权限,就会用到 -R参数。

如下: 我们先创建一个目录,在目录里创建几个文件,然后对目录修改属组和属主,并加上-R参数。

[root@mufenggrow test]# mkdir a
[root@mufenggrow test]# cd a
[root@mufenggrow a]# touch {1..5}.txt
[root@mufenggrow a]# ll
总用量 0
-rw-r--r--. 1 root root 0 1月  22 14:40 1.txt
-rw-r--r--. 1 root root 0 1月  22 14:40 2.txt
-rw-r--r--. 1 root root 0 1月  22 14:40 3.txt
-rw-r--r--. 1 root root 0 1月  22 14:40 4.txt
-rw-r--r--. 1 root root 0 1月  22 14:40 5.txt
[root@mufenggrow a]# cd ..
[root@mufenggrow test]# chown -R lp:lp a 
[root@mufenggrow test]# cd a
[root@mufenggrow a]# ll
总用量 0
-rw-r--r--. 1 lp lp 0 1月  22 14:40 1.txt
-rw-r--r--. 1 lp lp 0 1月  22 14:40 2.txt
-rw-r--r--. 1 lp lp 0 1月  22 14:40 3.txt
-rw-r--r--. 1 lp lp 0 1月  22 14:40 4.txt
-rw-r--r--. 1 lp lp 0 1月  22 14:40 5.txt

可以看到,测试结果与我们预想的一样。

3.4 只修改文件所属组为mufeng组

我们已经知道,想要修改属组和属主,可以使用 lp:lp的形式
如果只想修改属组怎么办呢?
这时候会用到:用户名的方式, 比如本题中我们只设置沐风组,就可以用:mufeng
以下是测试:

[root@mufenggrow a]# ls
1.txt  2.txt  3.txt  4.txt  5.txt
[root@mufenggrow a]# groupadd mufeng
[root@mufenggrow a]# chown :mufeng 1.txt
[root@mufenggrow a]# ll 1.txt 
-rw-r--r--. 1 lp mufeng 0 1月  22 14:40 1.txt
[root@mufenggrow a]#

除了:mufeng这种用法外,我们也可以使用. 的形式来修改属组。

[root@mufenggrow a]# ll 2.txt 
-rw-r--r--. 1 lp lp 0 1月  22 14:40 2.txt
[root@mufenggrow a]# chown .mufeng 2.txt
[root@mufenggrow a]# ll 2.txt 
-rw-r--r--. 1 lp mufeng 0 1月  22 14:40 2.txt
[root@mufenggrow a]# 

3.5 只修改属主

只修改属主,可以直接写属主的名字,比如:

我们给3.txt的属主改为 mufeng,但不改变属组:

[root@mufenggrow a]# useradd mufeng -g mufeng[root@mufenggrow a]# ll 3.txt
-rw-r--r--. 1 lp lp 0 1月  22 14:40 3.txt
[root@mufenggrow a]# chown mufeng 3.txt
[root@mufenggrow a]# ll 3.txt
-rw-r--r--. 1 mufeng lp 0 1月  22 14:40 3.txt
[root@mufenggrow a]# 

这里需要注意,因为我们一开始先创建的mufeng组,当你创建mufeng用户的时候需要用-g参数指定数组,否则会报错。

五. 总结

以上就是关于chown命令的详细使用,需要多多练习。

💕💕💕 好啦,这就是今天要分享给大家的全部内容了,我们下期再见!✨ ✨ ✨
🍻🍻🍻如果你喜欢的话,就不要吝惜你的一键三连了~

在这里插入图片描述

相关内容

热门资讯

【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
AsusVivobook无法开... 首先,我们可以尝试重置BIOS(Basic Input/Output System)来解决这个问题。...
ASM贪吃蛇游戏-解决错误的问... 要解决ASM贪吃蛇游戏中的错误问题,你可以按照以下步骤进行:首先,确定错误的具体表现和问题所在。在贪...
月入8000+的steam搬砖... 大家好,我是阿阳 今天要给大家介绍的是 steam 游戏搬砖项目,目前...