想要学好Linux,命令是基本功,企业中常用的命令大约200多个,不管是写shell脚本还是管理操作系统,最常用的命令必须要牢牢掌握,像我们以前学乘法口诀一样,烂熟于心,唯有如此,才能打牢基础。
💓 知识最重要的是记忆
💓 入门须知: 想要人生从容,必须全力以赴,努力才是你最终的入场券🚀🚀🚀
💕 最后: 努力成长自己,愿我们都能在看不到的地方闪闪发光 ,一起加油进步🍺🍺🍺
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参数,对目录及目录下所有文件进行变更。
创建一个文件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
-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 指的是所有权
当一个目录中有多个文件的时候,我们想要改变文件及文件内部的所有文件的权限,就会用到 -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
可以看到,测试结果与我们预想的一样。
我们已经知道,想要修改属组和属主,可以使用 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.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命令的详细使用,需要多多练习。
💕💕💕 好啦,这就是今天要分享给大家的全部内容了,我们下期再见!✨ ✨ ✨
🍻🍻🍻如果你喜欢的话,就不要吝惜你的一键三连了~