首页 技术 正文
技术 2022年11月19日
0 收藏 806 点赞 4,422 浏览 3416 个字

    chmod命令用来变更文件或目录的权限。

在UNIX系统家族里,文件或目录权限的控制分别以读取、写入、执行3种一般权限来区分,另有3种特殊权限可供运用。用户可以使用chmod指令去变更文件与目录的权限,设置方式采用文字或数字代号皆可。符号连接的权限无法变更,如果用户对符号连接修改权限,其改变会作用在被连接的原始文件。

    (1)用法:

  用法:  chmod  [选项]     [mode] 模式  文件

或 chmod  [-cfvR] [–help] [–version] mode file

命令有两种用法:一种是包含字母和操作符表达式的文字设定法;另一种是包含数字的数字设定法。

    (2)功能:

    功能:用于改变文件或目录的访问权限,用它控制文件或目录的访问权限。

    (3)参数详解:

1) -R    ——recursive:              递归处理,将指令目录下的所有文件及子目录一并处理

2) -v    ——verbose :               显示指令执行过程

3) –reference=<参考文件或目录>:          把指定文件或目录的所属群组全部设成和参考文件或目录的所属群组相同

4) <权限范围>+<权限设置>:           开启权限范围的文件或目录的该选项权限设置

<权限范围> -<权限设置>:                               关闭权限范围的文件或目录的该选项权限设置

<权限范围>=<权限设置>:                               指定权限范围的文件或目录的该选项权限设置

    (4)权限范围的表示法:
      1) u         User    即文件或目录的拥有者

2) g         Group     即文件或目录的所属群组

3) o         Other         除了文件或目录拥有者或所属群组之外,其他用户皆属于这个范围

4) a         All      即全部的用户,包含拥有者,所属群组以及其他用户

5) r                          读取权限,数字代号为“4”

6) w           写入权限,数字代号为“2”

7) x                          执行或切换权限,数字代号为“1”

8) –                          不具任何权限,数字代号为“0”

9) s                          特殊功能说明:变更文件或目录的权限

    (5)实例:

1)[root@localhost Documents]# chmod a+x core.log          为所有用户组添加可执行权限

[root@localhost Documents]# ll -al core.log         //ll -al 文件名与 ls -l 文件名貌似没啥区别
-rw-r--r--. root root 5月 : core.log
[root@localhost Documents]# ls -l core.log
-rw-r--r--. root root 5月 : core.log
[root@localhost Documents]# chmod a+x core.log
[root@localhost Documents]# ll -al core.log
-rwxr-xr-x. root root 5月 : core.log

2)[root@localhost Documents]# chmod ug+w,o-x core.log       同时为不同用户添加或删除权限

[root@localhost Documents]# ll -al core.log
-rwxr-xr-x. root root 5月 : core.log
[root@localhost Documents]# chmod ug+w,o-x core.log
[root@localhost Documents]# ll -l core.log
-rwxrwxr--. root root 5月 : core.log

3)[root@localhost Documents]# chmod g=x,o=rwx core.log        设置文件的u,g,o的三个权限。这里是设置文件所有者权限为可执行,其他用户可以读写执行,组用户权限不变

[root@localhost Documents]# ll -l core.log
-rwxrwxr--. root root 5月 : core.log
[root@localhost Documents]# chmod g=x,o=rwx core.log
[root@localhost Documents]# ls -l core.log
-rwx--xrwx. root root 5月 : core.log

4)[root@localhost Documents]# chmod -R o=— findDir      递归的设置findDir文件夹下的文件及文件夹的其他用户权限为不具备任何权限

[root@localhost Documents]# chmod -R o=--- findDir
[root@localhost Documents]# find . -name "Dir" -print
./Dir
[root@localhost Documents]# find . -name "Dir" -exec ls -l {} \;
总用量
--w-------. root root 5月 : head_text
--w-------. root root 5月 : less1
--w-------. root root 5月 : less2
[root@localhost Documents]# ls -l Dir
总用量
--w-------. root root 5月 : head_text
--w-------. root root 5月 : less1
--w-------. root root 5月 : less2

5)[root@localhost Documents]# chmod -R u=r,g=r,o=r findDir 与  [root@localhost Documents]# chmod -R =rx Dir    等价地给ugo的用户设置权限

[root@localhost Documents]# chmod -R u=r,g=r,o=r findDir
[root@localhost Documents]# ls -l findDir
总用量
-r--r--r--. root root 5月 : p1.pdf
-r--r--r--. root root 5月 : p2.pdf
-r--r--r--. root root 5月 : t1.txt
-r--r--r--. root root 5月 : T1.txt
-r--r--r--. root root 5月 : t2.txt
-r--r--r--. root root 5月 : T2.txt
[root@localhost Documents]# ls -l Dir
总用量
--w-------. root root 5月 : head_text
--w-------. root root 5月 : less1
--w-------. root root 5月 : less2
[root@localhost Documents]# chmod -R =rx Dir //设置所有组用户权限为rw之后,以前的所有者的w权限就没了
[root@localhost Documents]# ls -l Dir
总用量
-r-xr-xr-x. root root 5月 : head_text
-r-xr-xr-x. root root 5月 : less1
-r-xr-xr-x. root root 5月 : less2

6)[root@localhost Documents]# chmod 444 find        用数字给文件设置权限

[root@localhost Documents]# chmod  find
[root@localhost Documents]# ls -l find
-r--r--r--. root root 5月 : find
[root@localhost Documents]# chmod =r newlocate                //两种方法等价,但同时都会将原有的权限清除
[root@localhost Documents]# ls -l newlocate
-r--r--r--. root root 5月 : newlocate

    (6)其他:

Linux用户分为:拥有者、组群(Group)、其他(other)。

Linux系统中,预设的情況下,系统中所有的帐号与一般身份使用者,以及root的相关信息, 都是记录在/etc/passwd文件中。每个人的密码则是记录在/etc/shadow文件下。 此外,所有的组群名称记录在/etc/group內!

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,556
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898