Linux 第二周学习笔记 (8)
2.14 文件或目录权限 chmod
(1). 权限表示
[root@daizhihong01 ~]# ls -l
总用量 12
-rw-------. 1 root root 1418 1 月 22 08:19 anaconda-ks.cfg
-rw-r--r-- 1 root root 4358 1 月 29 23:24 anaconda-ks.cfg.1
文件的权限是和所有者, 所属组有关系的, 一个文件总共有三个权限位总共有 9 位分成三段, 加上第一位表示设备类型总共十位, 一下进行解析:
-rw-r--r--
第一位 "-": 表示设备类型
d: 表示目录
-: 表示普通文件 (包含文本文档及带色标记的二进制文档)
c: 表示字符串的设备 (如: 鼠标, 键盘)
l: 软件连接或者硬连接文件 (软连接相当于一个快捷方式)
s: 通讯文件 (如: 进程相互间的通讯)
rw-r--r--(从第二位开始表示权限)
w: 是否可写 (- 不可写);r: 是否可读 (- 不可读);x: 是否可执行;
-: 不可执行.
第一组 rw-: 表示所有者 (root), 文件是谁所有.
rw - 表示: r 可读, w 可写,- 不可执行.
第二组 - r--: 表示所属组 (root), 文件是属于那一个用户组.
r-- 表示: r 可读,- 不可写,- 不可执行.
第三组 r--: 表示其它用户, 除了所有者, 所有组以外的其它用户
r-- 表示: r 可读,- 不可写,- 不可执行.
权限使用数字表示:
r=4 w=2 x=1
rwx=7(三个数字相加之和) 表示可读可写可执行
rw-=6 可读可写不可执行
--x=1 只可执行
rw-r--r--=664
rw-r-xr-x=655
-rw-------.: 在这权限当中最后一位有一个 "." 是代表什么意思, 意味着这个文件受制于 seLinux, 如果 seLinux 开启的话创建的目录最后一位就会有一个 ".". 可使用 getenforce 命令暂时关闭, 如果要完全关闭的话需要更改配置文件 vi /etc/seLinux/config
-----------------------------------------------------------------------------------------
"/etc/selinux/config" 14L, 547C
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=enforcing(更改为 disabled 即可) 这节课这里只做了解, 不做任何更改
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
"/etc/selinux/config" 36L, 1133C
-----------------------------------------------------------------------------------------
(2.).chmod 命令: chmod 命令用来变更文件或目录的权限. 在 UNIX 系统家族里, 文件或目录权限的控制分别以读取, 写入, 执行 3 种一般权限来区分, 另有 3 种特殊权限可供运用. 用户可以使用 chmod 指令去变更文件与目录的权限, 设置方式采用文字或数字代号皆可. 符号连接的权限无法变更, 如果用户对符号连接修改权限, 其改变会作用在被连接的原始文件.
chmod-R 命令: chmod 命令只是对文件或者目录生效的仅仅只是指定的这个目录本身, 如果要把目录下面的子目录还有子文件全部一次性批量更改的权限, 就要使用 - R 参数
---------------------------------------------------------------------------------------
chmod 命令更改权限: 新建 123.txt 文件进行权限更改, 权限更改为: 所有者可读, 可写, 可执行, 用户组不可读, 不可写, 不可执行, 其它用户不可读, 不可写, 不可执行; 权限表示如下: rwx------=700
[root@daizhihong01 ~]# touch 123.txt
[root@daizhihong01 ~]# ls
123.txt anaconda-ks.cfg anaconda-ks.cfg.1
[root@daizhihong01 ~]# ls -l
总用量 12
-rw-r--r-- 1 root root 0 1 月 30 20:53 123.txt
-rw-------. 1 root root 1418 1 月 22 08:19 anaconda-ks.cfg
-rw-r--r-- 1 root root 4358 1 月 29 23:24 anaconda-ks.cfg.1
[root@daizhihong01 ~]# chmod 700 123.txt
[root@daizhihong01 ~]# ls -l
总用量 12
-rwx------ 1 root root 0 1 月 30 20:53 123.txt
-rw-------. 1 root root 1418 1 月 22 08:19 anaconda-ks.cfg
-rw-r--r-- 1 root root 4358 1 月 29 23:24 anaconda-ks.cfg.1
更改 daizhihong3 权限表示如下: drwxr-xr-x
u (User): 即文件或目录的拥有者;
g( Group): 即文件或目录的所属群组;
o (Other): 除了文件或目录拥有者或所属群组之外, 其他用户皆属于这个范围;
[root@daizhihong01 tmp]# ls -l
总用量 8
-rw-r--r-- 1 root root 2564 1 月 29 23:22 anaconda-ks.cfg.1
drwxr-xr-x 2 root root 20 1 月 29 13:36 daizhihong
drwxr-x--x 3 root root 24 1 月 28 21:58 daizhihong3
drwxr-xr-x 5 root root 53 1 月 29 11:43 daizhong
-rw-------. 1 root root 0 1 月 22 08:14 yum.log
[root@daizhihong01 tmp]# chmod u=wxr,g=xr,o=rx daizhihong3(在录入的时候是不能加 "-")
[root@daizhihong01 tmp]# ls -l
总用量 8
-rw-r--r-- 1 root root 2564 1 月 29 23:22 anaconda-ks.cfg.1
drwxr-xr-x 2 root root 20 1 月 29 13:36 daizhihong
drwxr-xr-x 3 root root 24 1 月 28 21:58 daizhihong3
drwxr-xr-x 5 root root 53 1 月 29 11:43 daizhong
使用 chmod 三组权限全部加权限:
[root@daizhihong01 tmp]# ls -l
总用量 8
-rw-r--r-- 1 root root 2564 1 月 29 23:22 anaconda-ks.cfg.1
drwxr-xr-x 2 root root 20 1 月 29 13:36 daizhihong
d-wx--x--- 3 root root 24 1 月 28 21:58 daizhihong3
drwxr-xr-x 5 root root 53 1 月 29 11:43 daizhong
[root@daizhihong01 tmp]# chmod a+r daizhihong3(三组权限全部加 r,a 代表全部)
[root@daizhihong01 tmp]# ls -l
总用量 8
-rw-r--r-- 1 root root 2564 1 月 29 23:22 anaconda-ks.cfg.1
drwxr-xr-x 2 root root 20 1 月 29 13:36 daizhihong
drwxr-xr-- 3 root root 24 1 月 28 21:58 daizhihong3(全部都加上了 r)
drwxr-xr-x 5 root root 53 1 月 29 11:43 daizhong
使用 chmod 三组权限全部减权限:
[root@daizhihong01 tmp]# ls -l
总用量 8
-rw-r--r-- 1 root root 2564 1 月 29 23:22 anaconda-ks.cfg.1
drwxr-xr-x 2 root root 20 1 月 29 13:36 daizhihong
drwxr-xr-- 3 root root 24 1 月 28 21:58 daizhihong3
drwxr-xr-x 5 root root 53 1 月 29 11:43 daizhong
[root@daizhihong01 tmp]# chmod a-r daizhihong3(三组权限全部减 r,a 代表全部)
[root@daizhihong01 tmp]# ls -l
总用量 8
-rw-r--r-- 1 root root 2564 1 月 29 23:22 anaconda-ks.cfg.1
drwxr-xr-x 2 root root 20 1 月 29 13:36 daizhihong
d-wx--x--- 3 root root 24 1 月 28 21:58 daizhihong3(三组权限全部减 r)
drwxr-xr-x 5 root root 53 1 月 29 11:43 daizhong
也可以这样使用:
chmod u+r daizhihong3
chmod u-r daizhihong3
chmod g+r daizhihong3
chmod g-r daizhihong3
chmod o+r daizhihong3
chmod o-r daizhihong3
--------------------------------------------------------------------------------
在前面讲密钥认证的时候, 在做密钥认证的时候就用到过如下:
[root@daizhihong01 ~]# chmod 700 /root/.ssh
[root@daizhihong01 ~]# ls -ld /root/.ssh/
drwx------. 2 root root 48 1 月 26 04:29 /root/.ssh/
--------------------------------------------------------------------------------------------
chmod-R 命令: chmod 命令只是对文件或者目录生效的仅仅只是指定的这个目录本身, 如果要把目录下面的子目录还有子文件全部一次性批量更改的权限, 就要使用 - R 参数
未加 R 参数:
[root@daizhihong01 tmp]# ls -l
总用量 8
-rw-r--r-- 1 root root 2564 1 月 29 23:22 anaconda-ks.cfg.1
drwxr-xr-x 2 root root 20 1 月 29 13:36 daizhihong
drwxr-xr-x 3 root root 24 1 月 28 21:58 daizhihong3
drwxr-xr-x 5 root root 53 1 月 29 11:43 daizhong
[root@daizhihong01 tmp]# ls -l daizhihong3
总用量 0
[root@daizhihong01 tmp]# chmod 770 daizhihong3(更改 daizhihong3 权限成 770rwxrwx---)
[root@daizhihong01 tmp]# ls -l
总用量 8
-rw-r--r-- 1 root root 2564 1 月 29 23:22 anaconda-ks.cfg.1
drwxr-xr-x 2 root root 20 1 月 29 13:36 daizhihong
drwxrwx--- 3 root root 24 1 月 28 21:58 daizhihong3(更改成功)
drwxr-xr-x 5 root root 53 1 月 29 11:43 daizhong
[root@daizhihong01 tmp]# ls -l daizhihong3
总用量 0
drwxr-xr-x 2 root root 20 1 月 28 21:58 daizhihong(但是在 daizhihong3 下面的子目录权限并未更改)
加 R 参数:
[root@daizhihong01 tmp]# chmod -R 770 daizhihong3
[root@daizhihong01 tmp]# ls -l
总用量 8
-rw-r--r-- 1 root root 2564 1 月 29 23:22 anaconda-ks.cfg.1
drwxr-xr-x 2 root root 20 1 月 29 13:36 daizhihong
drwxrwx--- 3 root root 24 1 月 28 21:58 daizhihong3
drwxr-xr-x 5 root root 53 1 月 29 11:43 daizhong
[root@daizhihong01 tmp]# ls -l daizhihong3
总用量 0
drwxrwx--- 2 root root 20 1 月 28 21:58 daizhihong(加入 - R 参数后连着子目录或者子文件的权限一起更改)
--------------------------------------------------------------------------------------
Linux 第二周学习笔记 (8)
来源: http://www.bubuko.com/infodetail-2481576.html