005. Linux基础五 P2 (文件权限管理)

1 文件全部者和属组属性操做

chown

设置文件的全部者
能够修改文件的属主,属组
选项:

git

  • OWNER #只修改全部者
  • OWNER:GROUP #同时修改全部者和属组
  • :GROUP #只修改属组,冒号也可用 . 替换 --reference=RFILE #参考指定的的属性,来修改
  • -R #递归,此选项慎用,很是危险!
    [root@centos7 ~]# chown test f1.txt 
    [root@centos7 ~]# ll f1.txt 
    -rw-r--r--. 1 test root 595 11月  9 22:40 f1.txt

[root@centos7 ~]# chown :admins f1.txt
[root@centos7 ~]# ll f1.txt
-rw-r--r--. 1 test admins 595 11月 9 22:40 f1.txt

redis

[root@centos7 ~]# chown root.bin f1.txt
[root@centos7 ~]# ll f1.txt
-rw-r--r--. 1 root bin 595 11月 9 22:40 f1.txt

mongodb

[root@centos7 ~]# cp /etc/issue f2.txt
[root@centos7 ~]# ll f2.txt
-rw-r--r--. 1 root root 23 11月 9 22:43 f2.txt
[root@centos7 ~]# chown --reference=f1.txt f2.txt
[root@centos7 ~]# ll f1.txt f2.txt
-rw-r--r--. 1 root bin 595 11月 9 22:40 f1.txt
-rw-r--r--. 1 root bin 23 11月 9 22:43 f2.txt





docker

### chgrp
设置文件属组信心
能够只修改文件的属组

[root@centos7 ~]# ll f1.txt
-rw-r--r--. 1 root bin 595 11月 9 22:40 f1.txt
[root@centos7 ~]# chgrp admins f1.txt
[root@centos7 ~]# ll f1.txt
-rw-r--r--. 1 root admins 595 11月 9 22:40 f1.txt



centos

# 2 文件权限

* owner 属主, u
* group 属组, g 
* other 其余, o

**注意:从左向右进行顺序匹配,一旦匹配权限当即生效,再也不向右查看其权限**

###### 对文件的权限
* r 可以使用文件查看类工具,好比:cat,能够获取其内容
* w 可修改其内容
* x 能够把此文件提请内核启动为一个进程,便可以执行(运行)此文件
###### 对文件夹的权限

* r 能够使用ls查看此目录中文件列表
* w 可在此目录中建立文件,也可删除此目录中的文件,而和此被删除的文件的权限无关
* x 能够cd进入此目录,能够使用ls -l查看此目录中文件元数据(须配合r权限),属于目录的可访问的最 小权限
* X 只给目录x权限,不给无执行权限的文件x权限

### chmod
who:u,g,o,a
opt:+,-,=
permission:r,w,x

# 3 新建文件和目录的默认权限
umask 的值能够用来保留在建立文件权限 
实现方式:

* 新建文件的默认权限: 666-umask,若是所得结果某位存在执行(奇数)权限,则将其权限+1,偶数不变
* 新建目录的默认权限: 777-umask

非特权用户umask默认是 002 
root的umask 默认是 022

[root@centos7 ~]# umask
0022
[root@centos7 ~]# umask -S
u=rwx,g=rx,o=rx
[root@centos7 ~]# umask -pS
umask -S u=rwx,g=rx,o=rx
[root@centos7 ~]# umask -p
umask 0022






bash

[root@centos7 ~]# umask 002
[root@centos7 ~]# umask u=rw,g=r,o=
ide

# 练习
1. 当用户docker对/testdir 目录无执行权限时,意味着没法作哪些操做?
* **意味着docker没法进入文件夹,意味着ls该文件夹下文件,不能得到详细信息**

docker1@centos7 ~]$ chmod a-x testdir/
[docker1@centos7 ~]$ ll testdir/ -d
drw-rw-r--. 2 docker1 docker1 6 Nov 9 23:03 testdir/
[docker1@centos7 ~]$ cd testdir/
-bash: cd: testdir/: Permission denied



工具

[docker1@centos7 ~]$ ll testdir/
ls: cannot access testdir/a: Permission denied
total 0
-????????? ? ? ? ? ? a


centos7

2. 当用户mongodb对/testdir 目录无读权限时,意味着没法作哪些操做?
* **能够cd,不能ls**

[docker1@centos7 ~]$ chmod a-r testdir/
[docker1@centos7 ~]$ ll testdir/
ls: cannot open directory testdir/: Permission denied
[docker1@centos7 ~]$ cd testdir/
[docker1@centos7 testdir]$ ll
ls: cannot open directory .: Permission denied




spa

3. 当用户redis 对/testdir 目录无写权限时,该目录下的只读文件file1是否可修改和删除?
*** 能够修改,没法建立,没法删除**

[docker1@centos7 testdir]$ touch file2
touch: cannot touch ‘file2’: Permission denied
[docker1@centos7 testdir]$ rm file1
rm: cannot remove ‘file1’: Permission denied


4. 当用户zabbix对/testdir 目录有写和执行权限时,该目录下的只读文件file1是否可修改和删除?
*** 能够**

[docker1@centos7 testdir]$ ll
total 0
-rw-rw-r--. 1 docker1 docker1 0 Nov 9 23:08 a
-r--r--r--. 1 docker1 docker1 0 Nov 9 23:13 file1
[docker1@centos7 testdir]$ cd
[docker1@centos7 ~]$ chmod 333 testdir/
[docker1@centos7 ~]$ rm testdir/file1
rm: remove write-protected regular empty file ‘testdir/file1’? y






5. 复制/etc/fstab文件到/var/tmp下,设置文件全部者为docker1读写权限,所属组为admins组有读写 权限,其余人无权限

[root@centos7 /]# chown docker1.admins /var/tmp/fstab
[root@centos7 /]# ll /var/tmp/fstab
-rw-r--r--. 1 docker1 admins 595 11月 9 23:18 /var/tmp/fstab
[root@centos7 /]# chmod 660 /var/tmp/fstab
[root@centos7 /]# ll /var/tmp/fstab
-rw-rw----. 1 docker1 admins 595 11月 9 23:18 /var/tmp/fstab




6. 误删除了用户git的家目录,请重建并恢复该用户家目录及相应的权限属性

[root@centos7 /]# cp -r /etc/skel/. /home/docker1[root@centos7 /]# chmod 700 /home/docker1/[root@centos7 /]# chown -R docker1.docker1 /home/docker1/[root@centos7 /]# ll /home/docker1/总用量 0[root@centos7 /]# ll /home/docker1/ -ddrwx------. 2 docker1 docker1 62 11月 9 23:26 /home/docker1/

相关文章
相关标签/搜索