一、文件或目录权限

1. 权限分为所属者的权限标红字段,所属组的权限标绿字段,其他用户的权限标蓝字段。

[root@localhost ~]# ls -l
总用量 626128
-rw-r--r--. 1 root root 0 12月 18 23:01 11.txt
-rw-r--r--. 1 root root 33 12月 20 00:51 1.txt
drwxr-xr-x. 4 root root 50 12月 20 00:40 dir

2.权限的表现形式。r   可读  数字表现为 4  w 可写  数字表现  2  x 可执行  数字表现为 1   rwx=4+2+1=7  rw_=4+2=6  r_x=4+1=5

[root@localhost ~]# ls -l
总用量 626128
-rw-r--r--. 1 root root 0 12月 18 23:01 11.txt  

该权限则可以表现为644

二、chmod 命令

1..chmod 修改用户对文件或者目录的权限

[root@localhost ~]# ls -l 11.txt
-rw-r--r--. 1 root root 0 12月 18 23:01 11.txt

我们看到11.txt 所有者的权限为6,可读可写,所属组为4,可读,其他用户4 ,可读,即644

[root@localhost ~]# ls -l 11.txt
-rwxrw---x. 1 root root 0 12月 18 23:01 11.txt

修改后,三者的权限为761,所属者可读可写可执行,所属组可读可写,其他用户可执行

2. -R 批量修改目录或文件的权限,可以看到,没加 -R前只修改了目录的权限,加- R后连同子目录和文件权限一起修改。

[root@localhost ~]# ls -l dir
总用量 4
-rw-r--r--. 1 root root 3 12月 20 00:38 3.txt
-rw-r--r--. 1 root root 0 12月 20 00:38 4.txt
drwxr-xr-x. 3 root root 15 12月 20 00:37 c
drwxr-xr-x. 2 root root 6 12月 20 00:34 d
[root@localhost ~]# ls -ld dir
drwxr-xr-x. 4 root root 50 12月 20 00:40 dir

[root@localhost ~]# chmod 661 dir
[root@localhost ~]# ls -ld dir
drw-rw---x. 4 root root 50 12月 20 00:40 dir
[root@localhost ~]# chmod -R 661 dir
[root@localhost ~]# ls -l dir
总用量 4
-rw-rw---x. 1 root root 3 12月 20 00:38 3.txt
-rw-rw---x. 1 root root 0 12月 20 00:38 4.txt
drw-rw---x. 3 root root 15 12月 20 00:37 c
drw-rw---x. 2 root root 6 12月 20 00:34 d

3.也可用这种方式来修改权限,但不常用,也不方便。

[root@localhost ~]# ls -l 1.txt
-rw-r--r--. 1 root root 33 12月 20 00:51 1.txt
[root@localhost ~]# chmod u=rwx,g=rw,o=x 1.txt
[root@localhost ~]# ls -l 1.txt
-rwxrw---x. 1 root root 33 12月 20 00:51 1.txt

 

[root@localhost ~]# chmod u-w,g-w,o+r 1.txt
[root@localhost ~]# ls -l 1.txt
-r-xr--r-x. 1 root root 33 12月 20 00:51 1.txt

[root@localhost ~]# chmod a+w 1.txt
[root@localhost ~]# ls -l 1.txt
-rwxrw-rwx. 1 root root 33 12月 20 00:51 1.txt

三、chown 命令

1.chown 更改文件的所有者

[root@localhost dir]# ls -ld dir1
drwxr-xr-x. 2 root root 6 12月 21 00:47 dir1
[root@localhost dir]# chown test dir1
[root@localhost dir]# ls -ld dir1
drwxr-xr-x. 2 test root 6 12月 21 00:47 dir1

我们可以看到dir1 有root用户变为test用户,如红色所示。

2.更改所属组,groupadd 增加组的命令

[root@localhost dir]# ls -ld dir1/
drwxr-xr-x. 2 test root 6 12月 21 00:47 dir1/
[root@localhost dir]# groupadd testgroup
[root@localhost dir]# chown test:testgroup dir1
[root@localhost dir]# ls -ld dir1
drwxr-xr-x. 2 test testgroup 6 12月 21 00:47 dir1

我们可以看到 所属组以经由 root变为testgroup组。

3. -R 批量修改所有者和所属组,可以多级目录修改。

[root@localhost dir]# ls -l dir1
总用量 0
-rw-r--r--. 1 root root 0 12月 21 00:58 1.txt
drwxr-xr-x. 2 root root 6 12月 21 00:59 dir2
[root@localhost dir]# ls -ld dir1
drwxr-xr-x. 3 test testgroup 31 12月 21 00:59 dir1
[root@localhost dir]# chown -R test:testgroup dir1
[root@localhost dir]# ls -l dir1
总用量 0
-rw-r--r--. 1 test testgroup 0 12月 21 00:58 1.txt
drwxr-xr-x. 2 test testgroup 6 12月 21 00:59 dir2

四、umask 命令

1.默认为 0022,实际运算时为022,最前面的0没有影响。

[root@localhost dir]# umask
0022

2.文件预设权限为666,目录权限预设为777,但还需减去umask ,所以文件权限表现为644,即 rw-rw-rw-  ----w--w-=rw-r--r--=644,,目录权限表现为755.即 rwxrwxrwx  ----w--w-=rwxr-xr-x=755.

3.umask 值可以自己设定

[root@localhost dir]# umask 002
[root@localhost dir]# umask
0002

4.umask 是不能做加减法运算,只能按字符去算。如下,我们看到umask 为022和033时的结果是一样。所以不能单纯的以数字计算。

[root@localhost ~]# umask 022
[root@localhost ~]# umask
0022
[root@localhost ~]# touch 2.txt
[root@localhost ~]# ls -ld 2.txt
-rw-r--r--. 1 root root 0 12月 21 01:21 2.txt
[root@localhost ~]# umask 033
[root@localhost ~]# umask
0033
[root@localhost ~]# touch 3.txt
[root@localhost ~]# ls -ld 3.txt
-rw-r--r--. 1 root root 0 12月 21 01:21 3.txt

五、chattr 和 lsattr

1.chattr  常用的 i 选项, 增加该属性后不能创建和删除,重命名,新增数据。

[root@localhost ~]# chattr +i dir
[root@localhost ~]# mkdir -p dir/dir1
mkdir: 无法创建目录"dir/dir1": 权限不够
[root@localhost ~]# touch dir/2.txt
touch: 无法创建"dir/2.txt": 权限不够
[root@localhost ~]# echo "11" > dir/1.txt

root@localhost ~]# rm -rf dir
rm: 无法删除"dir/1.txt": 权限不够
[root@localhost ~]# mv dir dir5
mv: 无法将"dir" 移动至"dir5": 不允许的操作
[root@localhost ~]# chattr +i 1.txt
[root@localhost ~]# echo "111" > 1.txt
-bash: 1.txt: 权限不够

[root@localhost ~]# echo "111" >> 1.txt
-bash: 1.txt: 权限不够

2.a  选项,只能追加,不能删除

[root@localhost ~]# chattr -R +a data
[root@localhost ~]# lsattr -l data/
data/11 Append_Only
data/2.txt Append_Only
[root@localhost ~]# lsattr -ld data/
data/ Append_Only
[root@localhost ~]# lsattr -R data/
-----a---------- data/11

data/11:

-----a---------- data/2.txt

[root@localhost ~]# rm -rf data/11
rm: 无法删除"data/11": 不允许的操作
[root@localhost ~]# rm -rf data/2.txt
rm: 无法删除"data/2.txt": 不允许的操作
[root@localhost ~]# echo "111" > data/2.txt
-bash: data/2.txt: 不允许的操作
[root@localhost ~]# echo "111" >> data/2.txt

 

3.lsattr 属性的查看方式,-a 隐藏的文件显示出来, -R 连同子级目录显示出来。

root@localhost ~]# lsattr -aR dir
----i----------- dir/.
---------------- dir/..
---------------- dir/1.txt
[root@localhost ~]# lsattr -aR data
-----a---------- data/.
---------------- data/..
-----a---------- data/11

data/11:
-----a---------- data/11/.
-----a---------- data/11/..

-----a---------- data/2.txt

4.去除属性, -i  -a

[root@localhost ~]# chattr -i dir
[root@localhost ~]# lsattr dir
---------------- dir/1.txt

[root@localhost ~]# chattr -aR data
[root@localhost ~]# lsattr -R data
---------------- data/11

data/11:

---------------- data/2.txt

 

posted on 2017-12-21 01:58  天梭  阅读(131)  评论(0编辑  收藏  举报