码迷,mamicode.com
首页 > 系统相关 > 详细

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

时间:2020-11-10 11:27:34      阅读:11      评论:0      收藏:0      [点我收藏+]

标签:奇数   other   启动   rmi   权限   bin   选项   工具   内容   

1 文件所有者和属组属性操作

chown

设置文件的所有者
可以修改文件的属主,属组
选项:

  • 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

[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

[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


### 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

# 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

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

# 练习
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

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

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

[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 目录有写和执行权限时,该目录下的只读文件?le1是否可修改和删除?
*** 可以**

[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/ -d
drwx------. 2 docker1 docker1 62 11月 9 23:26 /home/docker1/

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

标签:奇数   other   启动   rmi   权限   bin   选项   工具   内容   

原文地址:https://blog.51cto.com/abyssce/2548590

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!