1.文件权限存在的意义:
系统最底层安全设定方法之一,保证文件可以被可用的用户做相应操作
2.文件和目录属性查看
(1)查看文件属性
格式: ls -l filename(文件名)
参数解释:
- rw-rw-r-- 1 kiosk kiosk 0 Oct 2 17:05 file
[1] [2] [3] [4] [5] [6] [7] [8]
[1] ##文件类型
- #普通文件
d #目录
s #socket套接子(程序对外开放的接口)
l #软链接(快捷方式)
p #管道
c #字符设备
b #块设备
套接字:网络上两个程序双向通讯连接的端点/是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口
软链接:可以看作是Windows中的快捷方式,可以让你快速链接到目标档案或目录。
硬链接:透过文件系统的inode来产生新档名,而不是产生新档案。
软链接(符号链接) ln -s source target
硬链接(实体链接) ln source target
[2] ##读写权限
rw-|rw-|r--
@ $ *
@ #文件拥有者对文件能做的动作
$ #文件所在组的组成员能对文件做的动作
* #其他人对文件能做的动作
[3] #文件内容被系统记录的次数
[4] #文件拥有者
[5] #文件所在组
[6] #文件大小
[7] #文件最后一次被更改的时间
[8] #文件名称
(2)查看目录属性
格式: ls -ld directoy(目录名)
参数解释:
d rwxr-xr-x 3 kiosk kiosk 33 Aug 27 09:47 directory
[1] [2] [3] [4] [5] [6] [7] [8]
[1] ##类型(同文件)
[2] ##权限
[3] ##子目录个数
[4] ##目录拥有者
[5] ##目录所在组
[6] ##目录中内容的属性(元数据)的大小
[7] ##目录中文件增加或减少或被更名的时间
[8] ##目录名称
实验:
[root@localhost ~]# touch file
[root@localhost ~]# mkdir test
## - 表示 普通文件
[root@localhost ~]# ls -l file
-rw------- 1 root root 0 Oct 5 05:10 file
## d 表示 目录
[root@localhost ~]# ls -ld test
drwx------ 2 root root 6 Oct 5 05:10 test
[root@localhost ~]# ll file
-rw------- 1 root root 0 Oct 5 05:10 file
[root@localhost ~]# ll -d test
drwx------ 2 root root 6 Oct 5 05:10 test
## ll = ls -l
[root@localhost ~]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
## c 表示 字符设备
[root@localhost ~]# ll /dev/pts/0
crw--w---- 1 root tty 136, 0 Oct 5 05:14 /dev/pts/0
## b 表示 块设备
[root@localhost ~]# ll /dev/vdb
brw-rw---- 1 root disk 253, 16 Oct 5 05:05 /dev/vdb
##制作软链接
[root@localhost ~]# ln -s file file1
## l 表示软链接
[root@localhost ~]# ll file1
lrwxrwxrwx 1 root root 4 Oct 1 04:31 file1 -> file
[root@localhost ~]# vim file
[root@localhost ~]# cat file
westos
## 1表示文件内容被系统记录的次数
[root@localhost ~]# ls -l file
-rw-r--r-- 1 root root 7 Oct 1 04:34 file
##制作硬链接
[root@localhost ~]# ln file file2
[root@localhost ~]# ls -l file
-rw-r--r-- 2 root root 7 Oct 1 04:34 file
[root@localhost ~]# rm -fr file2
[root@localhost ~]# ls -l file
-rw-r--r-- 1 root root 7 Oct 1 04:34 file
[root@localhost ~]# mkdir test
## 2表示子目录的个数
[root@localhost ~]# ls -ld test
drwx------ 2 root root 6 Oct 5 05:51 test
[root@localhost ~]# ls test
[root@localhost ~]# ls -a test
. ..
[root@localhost ~]# mkdir test/hello
[root@localhost ~]# ls -a test/
. .. hello
[root@localhost ~]# ls -ld test
drwx------ 3 root root 18 Oct 5 05:52 test
[root@localhost ~]# rm -rf test
[root@localhost ~]# mkdir test
## 6表示元数据的大小
[root@localhost ~]# ls -ld test
drwx------ 2 root root 6 Oct 5 05:56 test
[root@localhost ~]# touch test/w
## 14=6+7+1 (1表示一个字符)
[root@localhost ~]# ls -ld test
drwx------ 2 root root 14 Oct 5 05:57 test
[root@localhost ~]# touch test/su
## 23=14+7+2(2表示2个字符)
[root@localhost ~]# ls -ld test
drwx------ 2 root root 23 Oct 5 05:57 test
3.文件和目录用户组的更改
(1)文件用户组的更改
chown + 用户名 + 文件名 ##更改文件所有人(拥有者)
chgrp + 组名 + 文件名 ##更改文件所有组(所在组)
(2)目录用户组的更改
chown -R + 用户 + 目录名 ##更改目录本身以及目录中的子文件的所有人
chgrp -R + 组名 + 目录名 ##更改目录本身以及目录中的子文件的所有组
实验:
[root@localhost ~]# mkdir westos
[root@localhost ~]# mkdir westos/linux
[root@localhost ~]# touch westos/file{1..3}
[root@localhost ~]# touch westos/linux/redhat{1..3}
[root@localhost ~]# ls -l westos
total 0
-rw------- 1 root root 0 Oct 5 07:50 file1
-rw------- 1 root root 0 Oct 5 07:50 file2
-rw------- 1 root root 0 Oct 5 07:50 file3
drwx------ 2 root root 48 Oct 5 07:51 linux
[root@localhost ~]# ls -lR westos
westos:
total 0
-rw------- 1 root root 0 Oct 5 07:50 file1
-rw------- 1 root root 0 Oct 5 07:50 file2
-rw------- 1 root root 0 Oct 5 07:50 file3
drwx------ 2 root root 48 Oct 5 07:51 linux
westos/linux:
total 0
-rw------- 1 root root 0 Oct 5 07:51 redhat1
-rw------- 1 root root 0 Oct 5 07:51 redhat2
-rw------- 1 root root 0 Oct 5 07:51 redhat3
##监控
[root@localhost Desktop]# watch -n 1 "ls -lR /mnt"
[root@localhost Desktop]# id tom
uid=2000(tom) gid=1005(tom) groups=1005(tom),1002(shengchan)
[root@localhost Desktop]# id leo
uid=3001(leo) gid=3001(leo) groups=3001(leo),1004(jishu)
[root@localhost Desktop]# id harry
uid=3000(harry) gid=2001(harry) groups=2001(harry),1003(caiwu)
[root@localhost Desktop]# chown leo westos/file1
[root@localhost ~]# chgrp harry westos/file2
##同时更改westos/file3文件的所有人和所有组
[root@localhost ~]# chown student.admin westos/file3
[root@localhost ~]# chown student westos/linux
[root@localhost ~]# chown root /mnt/linux
## -R 表示递归(目录和目录中的文件)
[root@localhost ~]# chown -R tom westos/linux
4.权限的识别
rw-rw-r--
rw- rw- r--
用户权限(u) 组成员权限(g) 其他用户权限(o)
(1)权限的种类
r
r权限针对文件,表示是否可以查看文件内容 ( cat file )
r权限针对目录,表示是否可以查看目录中有什么子文件或者子目录 ( ls dir )
w
w权限针对文件,表示可以更改文件的内容
w权限针对目录,表示是否可以删除目录中的子文件或者子目录/对目录中子目录或子文件的元数据进行更改
x
x权限对于文件,表示是否可以开启文件当中记录的程序/是否可以通过文件名称调用文件内记录的程序
x权限对于目录,表示是否可以进入目录中
(2)权限的更改
格式1: chmod <u|g|o><+|-|=><r|w|x> file|dir
例如:
chmod u+x /mnt/file1
chmod g-r /mnt/file2
chmod ug-r /mnt/file3
chmod u-r,g+x /mnt/file4
chmod -r /mnt/file5
chmod o=r-x /mnt/file6
格式2: chmod + 权限数值 + 文件名/目录名
各权限所对应的数值:
r=4 w=2 x=1 -=0
例如:
u=rwx=7 g=rwx=7 o=rwx=7 777
u=rw-=6 g=r--=4 o=r--=4 644
7=rwx,6=rw-,5=r-x,4=r--,3=-wx,2=-w-,1=--x,0=---
实验:
[root@localhost ~]# rm -rf westos/*
[root@localhost ~]# umask
0022
[root@localhost ~]# touch westos/file{1..3}
[root@localhost ~]# mkdir westos/linux
[root@localhost ~]# touch westos/linux/redhat{1..3}
[root@localhost ~]# ls -lR westos
westos:
total 0
-rw-r--r-- 1 root root 0 Oct 5 08:27 file1
-rw-r--r-- 1 root root 0 Oct 5 08:27 file2
-rw-r--r-- 1 root root 0 Oct 5 08:27 file3
drwxr-xr-x 2 root root 48 Oct 5 08:27 linux
westos/linux:
total 0
-rw-r--r-- 1 root root 0 Oct 5 08:27 redhat1
-rw-r--r-- 1 root root 0 Oct 5 08:27 redhat2
-rw-r--r-- 1 root root 0 Oct 5 08:27 redhat3
[root@localhost ~]# watch -n 1 ls -lR westos
[root@localhost ~]# chmod ugo+x westos/file1
[root@localhost ~]# chmod ug-x,o+w westos/file1
[root@localhost ~]# chmod ug=rwx,o=--- westos/file1
## -w 默认只会对文件所有人 起作用
[root@localhost ~]# chmod -r westos/file2
[root@localhost ~]# chmod +x westos/file2
[root@localhost ~]# chmod o+w westos/file1
[root@localhost ~]# chmod -w westos/file1
chmod: westos/file1: new permissions are r-xrwx-w-, not r-xr-x---
##撤销所有权限
[root@localhost ~]# chmod ugo-wrx westos/file2
[root@localhost ~]# chmod 000 westos/file3
[root@localhost ~]# chmod 0 westos/file1
5.文件的默认权限
安全性和功能的均衡(功能越小越安全,两者矛盾)
默认目录权限:755 默认文件权限:644
777-022=755-111=644
022 #系统预留权限值
111 #文件系统(去掉可执行权限)
(1)临时修改
umask ##显示系统预留权限值
umask 077 ##临时修改umask值
(2)永久修改
vim /etc/profile ##系统配置文件
################
59 if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then ## -gt 表示 >
60 umask 002 ##普通用户的umask(普通用户uid是从1000开始的)
61 else
62 umask 077 ##超级用户的umask
63 fi
vim /etc/bashrc ##shell配置文件
################
70 if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
71 umask 002
72 else
73 umask 077
74 fi
source /etc/profile ##刷新系统配置(让更改立即生效)
source /etc/profile
实验:
##监控
[root@localhost ~]# rm -rf westos/*
[root@localhost ~]# watch -n 1 ls -lR westos
(1)临时更改
[root@localhost ~]# umask
0022
[root@localhost ~]# umask 077
[root@localhost ~]# umask
0077
##700=777-077
[root@localhost ~]# mkdir westos/test1
##600=700-111
[root@localhost ~]# touch westos/file1
再打开一个shell
[root@localhost ~]# umask
0022
##644=777-022
[root@localhost ~]# mkdir westos/test2
[root@localhost ~]# touch westos/file2
(2)永久性更改
#更改系统配置文件
[root@localhost ~]# umask
0022
[root@localhost ~]# vim /etc/profile
###################
62 umask 077
#更改shell配置文件
[root@localhost ~]# vim /etc/bashrc
###################
73 umask 077
#刷新系统配置
[root@localhost ~]# source /etc/profile
[root@localhost ~]# source /etc/bashrc
[root@localhost ~]# umask
0077
再打开一个shell
[root@localhost ~]# umask
0077
6.特殊权限
(1)stickyid 粘制位
o+t #只针对目录,当一个目录上有t权限时,这个目录中的文件只能被文件拥有者删(t=1)
设定方式:
chmod o+t dir(目录)
chmod 1xxx dir
实验:
[root@localhost ~]# rm -rf /mnt/*
[root@localhost ~]# mkdir /mnt/public
[root@localhost ~]# touch /mnt/public/file
[root@localhost ~]# ll -d /mnt/public
drwxr-xr-x. 2 root root 17 Oct 5 09:39 /mnt/public
[root@localhost ~]# chmod 777 /mnt/public
[root@localhost ~]# ll -d /mnt/public
drwxrwxrwx. 2 root root 17 Oct 5 09:39 /mnt/public
[root@localhost ~]# su - student
Last login: Fri Oct 5 09:38:29 EDT 2018 on pts/1
[student@localhost ~]$ cd /mnt/public
[student@localhost ~]$ touch test
##用户可以删除不属于自己的文件是不合理的
[tom@localhost public]$ rm -rf file
[tom@localhost public]$ exit
logout
##解决方案
[root@localhost ~]# touch /mnt/public/file
## o+t 等同于 1777
[root@localhost ~]# chmod o+t /mnt/public/
[root@localhost ~]# chmod o-t /mnt/public/
[root@localhost ~]# chmod 1777 /mnt/public/
[root@localhost ~]# ll -d /mnt/public
drwxrwxrwt. 2 root root 28 Oct 5 09:40 /mnt/public
[root@localhost ~]# su - student
Last login: Fri Oct 5 09:39:38 EDT 2018 on pts/1
[student@localhost ~]$ cd /mnt/public/
[student@localhost public]$ touch test
[student@localhost public]$ rm -rf file
rm: cannot remove ‘file’: Operation not permitted
(2)sgid 强制位
g+s #针对目录,在目录中创建的文件都自动归属到目录所在组,
#针对二进制文件,文件内记录的程序在执行时和执行者的组身份没有关系,而是以二进制文件的所有组的身份执行的
设定方式:
chmod g+s file|directory
chmod 2xxx file|directory
实验1:
(1)对于目录
[root@localhost ~]# rm -rf /mnt/*
[root@localhost ~]# mkdir /mnt/public
[root@localhost ~]# ll -d /mnt/public
drwxr-xr-x. 2 root root 17 Oct 5 09:53 /mnt/public
[root@localhost ~]# chmod 777 /mnt/public
[root@localhost ~]# ll -d /mnt/public
drwxrwxrwx. 2 root root 17 Oct 5 09:53 /mnt/public
[root@localhost ~]# su - student
Last login: Fri Oct 5 09:52:40 EDT 2018 on pts/1
[student@localhost ~]$ cd /mnt/public/
##文件所有组属于自己(私有),很不合理(比如:厨师在饭店做的饭不属于自己,而属于饭店)
[student@localhost public]$ touch file1
[student@localhost public]$ ll file1
-rw-rw-r--. 1 student student 0 Oct 5 09:53 file1
[student@localhost public]$ exit
logout
##解决方案
[root@localhost ~]# chmod g+s /mnt/public/
[root@localhost ~]# chmod g-s /mnt/public/
[root@localhost ~]# chmod 2777 /mnt/public/
[root@localhost ~]# ll -d /mnt/public
drwxrwsrwx. 2 root root 29 Oct 5 09:53 /mnt/public
[root@localhost ~]# su - student
Last login: Fri Oct 5 09:53:29 EDT 2018 on pts/1
[student@localhost ~]$ cd /mnt/public/
[student@localhost public]$ touch file2
[student@localhost ~]$ ll /mnt/public/
total 0
-rw-r--r--. 1 root root 0 Oct 5 09:53 file
-rw-rw-r--. 1 student student 0 Oct 5 09:53 file1
#两个权限同时加
[root@localhost ~]# chmod o-t /mnt/public/
[root@localhost ~]# chmod g-s /mnt/public/
[root@localhost ~]# chmod 3777 /mnt/public/
(2)对于二进制文件
##watch命令属于root组,但在student用户中执行watch命令属于student组;这样很不合理
在shell 1中:
[root@localhost ~]# which watch
/usr/bin/watch
[root@localhost ~]# ls -l /usr/bin/watch
-rwxr-xr-x. 1 root root 24704 Feb 27 2014 /usr/bin/watch
[root@localhost ~]# watch -n 1 date
在shell 2中:
[root@localhost ~]# ps ax -o comm,user,group|grep watch
watchdog/0 root root
abrt-watch-log root root
abrt-watch-log root root
watch root root
在shell 1中:
[root@localhost ~]# su - student
Last login: Mon Oct 1 02:26:07 EDT 2018 on pts/1
[student@localhost ~]$ watch -n 1 date
在shell 2中:
[root@localhost ~]# ps ax -o comm,user,group|grep watch
watchdog/0 root root
abrt-watch-log root root
abrt-watch-log root root
watch student student
解决方案:
在shell 1中
[student@localhost ~]$ exit
logout
[root@localhost ~]# chmod g+s /usr/bin/watch
[root@localhost ~]# ls -l /usr/bin/watch
-rwxr-sr-x. 1 root root 24704 Feb 27 2014 /usr/bin/watch
[root@localhost ~]# su - student
Last login: Tue Oct 2 00:33:29 EDT 2018 on pts/2
[student@localhost ~]$ watch -n 1 date
在shell 2中
[root@localhost ~]# ps ax -o comm,user,group|grep watch
watchdog/0 root root
abrt-watch-log root root
abrt-watch-log root root
watch student root
实验2:
[root@localhost ~]# ls -l /usr/bin/touch
-rwxr-xr-x. 1 root root 62432 Jan 24 2014 /usr/bin/touch
[root@localhost ~]# su - student
Last login: Fri Oct 5 10:24:24 EDT 2018 on pts/1
[student@localhost ~]$ touch file
[student@localhost ~]$ ls -l file
-rw-rw-r--. 1 student student 0 Oct 5 10:25 file
[student@localhost ~]$ exit
logout
[root@localhost ~]# chmod g+s /usr/bin/touch
[root@localhost ~]# ls -l /usr/bin/touch
-rwxr-sr-x. 1 root root 62432 Jan 24 2014 /usr/bin/touch
[root@localhost ~]# su - student
Last login: Fri Oct 5 10:25:44 EDT 2018 on pts/1
[student@localhost ~]$ touch test
[student@localhost ~]$ ll
total 0
-rw-rw-r--. 1 student student 0 Oct 5 10:25 file
-rw-rw-r--. 1 student root 0 Oct 5 10:26 test
(3)suid 冒险位
u+s #针对文件,文件记录动作在执行时是一文件所有人身份执行的,与是谁发起的无关
设定方式:
chmod u+s file
chmod 4xxx file
实验:
##文件所有人归属于动作的执行者
在shell 1中:
[root@localhost ~]# ls -l /bin/watch
-rwxr-sr-x. 1 root root 24704 Feb 27 2014 /bin/watch
[root@localhost ~]# su - student
Last login: Tue Oct 2 00:43:01 EDT 2018 on pts/2
[student@localhost ~]$ watch -n 1 date
在shell 2 中:
[root@localhost ~]# ps ax -o comm,user,group|grep watch
watchdog/0 root root
abrt-watch-log root root
abrt-watch-log root root
watch student root
##u+s表示任何人执行这个文件中记的程序产生的进程都属于文件的所有人,而与执行者无关
在shell 1中:
[student@localhost ~]$ exit
logout
[root@localhost ~]# chmod u+s /bin/watch
[root@localhost ~]# ls -l /bin/watch
-rwsr-sr-x. 1 root root 24704 Feb 27 2014 /bin/watch
[root@localhost ~]# su - student
Last login: Tue Oct 2 01:25:54 EDT 2018 on pts/2
[student@localhost ~]$ watch -n 1 date
在shell 2 中:
[root@localhost ~]# ps ax -o comm,user,group|grep watch
watchdog/0 root root
abrt-watch-log root root
abrt-watch-log root root
watch root root
实际应用:用户的降级
[root@localhost ~]# rm -rf /mnt
[root@localhost ~]# mkdir /mnt
[root@localhost ~]# ll -d /mnt
drwxr-xr-x. 2 root root 6 Oct 5 10:44 /mnt
[root@localhost ~]# chmod 755 /mnt
[root@localhost ~]# touch /mnt/file
[root@localhost ~]# ll -d /mnt
drwxr-xr-x. 2 root root 17 Oct 5 10:44 /mnt
##group位和other位无w(可写)权限时 root用户仍然可以删除文件;这样很不安全
[root@localhost ~]# rm -rf /mnt/*
##解决方案
[root@localhost ~]# touch /mnt/file
[root@localhost ~]# useradd -s /sbin/nologin norm
[root@localhost ~]# ls -l /bin/rm
-rwxr-xr-x. 1 root root 62808 Jan 24 2014 /bin/rm
[root@localhost ~]# chown norm.norm /bin/rm
[root@localhost ~]# ls -l /bin/rm
-rwxr-xr-x. 1 norm norm 62808 Jan 24 2014 /bin/rm
## 6755 = u+s,g+s
[root@localhost ~]# chmod 6755 /bin/rm
[root@localhost ~]# ls -l /bin/rm
-rwsr-sr-x. 1 norm norm 62808 Jan 24 2014 /bin/rm
[root@localhost ~]# ll -d /mnt
drwxr-xr-x. 2 root root 17 Oct 5 10:46 /mnt
[root@localhost ~]# rm -rf /mnt
rm: cannot remove ‘/mnt/file’: Permission denied
[root@localhost ~]# rm -rf /mnt/*
rm: cannot remove ‘/mnt/file’: Permission denied
7.acl权限列表
(1) 作用
让特定的用户对特定的文件拥有特定权限
(2) acl列表查看
-rw-rwx---+ 1 root root 0 Mar 28 02:39 file
^
权限列表开启 (设定文件的特殊用户权限后权限列表便会自动开启)
那么这个文件的普通权限也在权限列表中识别,ls -l 看到的权限是不准确的
getfacl /mnt/file ##查看文件的具体权限
##具体内容解释:
file: mnt/file #文件名称
# owner: root #文件的拥有者
# group: root #文件的组
user::rw- #文件拥有者的权限
user:westos:rw- #特殊指定用户的权限
group::--- #文件组的权限
group:student:rwx #特殊指定组的权限
mask::rwx #权限最大值(设定文件的特殊用户权限后才会有mask值)
other::--- #其他人的权限
实验:
[root@localhost ~]# rm -rf /mnt/*
[root@localhost ~]# touch /mnt/file
[root@localhost ~]# ll /mnt/file
-rw-r--r--. 1 root root 0 Oct 8 05:47 /mnt/file
[root@localhost ~]# getfacl /mnt/file
getfacl: Removing leading '/' from absolute path names
# file: mnt/file
# owner: root
# group: root
user::rw-
group::r--
other::r--
[root@localhost ~]# setfacl -m u:student:rx /mnt/file
[root@localhost ~]# ll /mnt/file
-rw-r-xr--+ 1 root root 0 Oct 8 05:47 /mnt/file
[root@localhost ~]# getfacl /mnt/file
getfacl: Removing leading '/' from absolute path names
# file: mnt/file
# owner: root
# group: root
user::rw-
user:student:r-x
group::r--
mask::r-x
other::r--
(3) acl列表的管理
setfacl -m u:lee:rwx /mnt/file #设定lee用户对file文件可以读写执行
setfacl -m g:student:rwx /mnt/file #设定student组对文件可以读写执行
setfacl -x u:lee /mnt/file #在权限列表中删除用户lee的信息
setfacl -x g:student /mnt/file #在权限列表中删除student组的信息
setfacl -b /mnt/file #关闭权限列表那么,"+"消失
实验:
[root@localhost ~]# rm -rf /mnt/*
[root@localhost ~]# touch /mnt/westos
[root@localhost ~]# watch -n 1 'ls -lR /mnt; getfacl /mnt/westos'
[root@localhost ~]# setfacl -m u:tom:0 /mnt/westos
[root@localhost ~]# setfacl -x u:student /mnt/westos
[root@localhost ~]# setfacl -x u:student /mnt/westos
[root@localhost ~]# setfacl -m g:shengchan:rw /mnt/westos
[root@localhost ~]# setfacl -x g:shengchan /mnt/westos
[root@localhost ~]# setfacl -b /mnt/westos
实际应用:文件只对root用户和student用户有可写权限
##还原环境
[root@localhost ~]# ll -ld /bin/touch
-rwxr-sr-x. 1 root root 62432 Jan 24 2014 /bin/touch
[root@localhost ~]# ll -ld /bin/rm
-rwsr-sr-x. 1 norm norm 62808 Jan 24 2014 /bin/rm
[root@localhost ~]# chmod ug-s /bin/touch
[root@localhost ~]# chmod ug-s /bin/rm
[root@localhost ~]# chown root.root /bin/rm
[root@localhost ~]# ll -ld /bin/rm
-rwxr-xr-x. 1 root root 62808 Jan 24 2014 /bin/rm
[root@localhost ~]# ll -ld /bin/touch
-rwxr-xr-x. 1 root root 62432 Jan 24 2014 /bin/touch
##监控
[root@localhost ~]# watch -n 1 ls -lR /mnt
[root@localhost ~]# rm -rf /mnt/*
[root@localhost ~]# touch /mnt/westos
[root@localhost ~]# umask
0022
[root@localhost ~]# setfacl -m u:student:rw /mnt/westos
[root@localhost ~]# getfacl /mnt/westos
getfacl: Removing leading '/' from absolute path names
# file: mnt/westos
# owner: root
# group: root
user::rw-
user:student:rw-
group::r--
mask::rw-
other::r--
测试:
[root@localhost ~]# echo heel > /mnt/westos
[root@localhost ~]# cat /mnt/westos
heel
[root@localhost ~]# su - student
Last login: Sun Oct 7 01:22:13 EDT 2018 on pts/1
[student@localhost ~]$ echo hello word >> /mnt/westos
[student@localhost ~]$ cat /mnt/westos
heel
hello word
[student@localhost ~]$ exit
logout
[root@localhost ~]# useradd haha
[root@localhost ~]# su - haha
[haha@localhost ~]$ echo hello linux > /mnt/westos
-bash: /mnt/westos: Permission denied
[haha@localhost ~]$ exit
logout
(4)mask值
mask值是能够赋予用户权限的最大值
当设定acl列表后,如果用chmod命令缩小文件的权限,那么mask值会被更改
恢复mask值
setfacl -m m:rwx /mnt/file
[root@localhost Desktop]# touch /mnt/file
[root@localhost Desktop]# ls -l /mnt/file
-rw-r--r-- 1 root root 0 Sep 8 10:28 /mnt/file
##设定acl列表
[root@localhost Desktop]# setfacl -m u:harry:rw /mnt/file
[root@localhost Desktop]# ls -l /mnt/file
-rw-rw-r--+ 1 root root 0 Sep 8 10:28 /mnt/file
[root@localhost Desktop]# getfacl /mnt/file
getfacl: Removing leading '/' from absolute path names
# file: mnt/file
# owner: root
# group: root
user::rw-
user:harry:rw-
group::r--
mask::rw-
other::r--
##缩小文件的权限
[root@localhost Desktop]# chmod 600 /mnt/file
##mask值发生改变; #effective:--- 此位置表示有效的权力
[root@localhost Desktop]# getfacl /mnt/file
getfacl: Removing leading '/' from absolute path names
# file: mnt/file
# owner: root
# group: root
user::rw-
user:harry:rw- #effective:---
group::r-- #effective:---
mask::---
other::---
##恢复mask的值
[root@localhost Desktop]# setfacl -m m:rwx /mnt/file
[root@localhost Desktop]# getfacl /mnt/file
getfacl: Removing leading '/' from absolute path names
# file: mnt/file
# owner: root
# group: root
user::rw-
user:harry:rw-
group::r--
mask::rwx
other::---
(5). acl列表的默认权限
acl默认权限针对于目录生效
当对目录设定普通的权限列表后,是不能取定特定用户对在目录中新建的文件由指定权限的
如果需要权限自动添加到新建文件上那么要设定目录的默认权限;
“acl权限只针对设定完成之后"新建立"的文件或目录生效,而"已经存在的"文件是不会继承默认权限”
setfacl -m d:u:student:rwx /mnt/westos
实验:
[root@localhost ~]# rm -rf /mnt/*
[root@localhost ~]# mkdir /mnt/test
[root@localhost ~]# watch -n 1 'ls -lR /mnt/; getfacl /mnt/test'
[root@localhost ~]# setfacl -m u:student:rwx /mnt/test
[root@localhost ~]# su - student
Last login: Tue Oct 2 03:11:19 EDT 2018 on pts/0
##student用户对mnt/test目录 有可写权限
[student@localhost ~]$ touch /mnt/test/file
[student@localhost ~]$ exit
logout
##但sudent用户对/mnt/test目录的子目录linux不可写
[root@localhost ~]# mkdir /mnt/test/linux
[root@localhost ~]# su - student
Last login: Tue Oct 2 03:25:42 EDT 2018 on pts/0
[student@localhost ~]$ cd /mnt/test/linux/
[student@localhost linux]$ touch westos
touch: cannot touch ‘westos’: Permission denied
[student@localhost linux]$ exit
logout
[root@localhost ~]# ll -d /mnt/test/linux
drwxr-xr-x. 2 root root 6 Oct 7 02:25 /mnt/test/linux
[root@localhost ~]# umask
0022
解决方案:
##设定默认权限
[root@localhost ~]# setfacl -m d:u:student:rwx /mnt/test
##此时会出现default默认权限列表
[root@localhost ~]# getfacl /mnt/test
getfacl: Removing leading '/' from absolute path names
# file: mnt/test
# owner: root
# group: root
user::rwx
user:student:rwx
group::r-x
mask::rwx
other::r-x
default:user::rwx
default:user:student:rwx
default:group::r-x
default:mask::rwx
default:other::r-x
##acl默认权限只针对设定完成之后新建立的文件或目录生效,而已经存在的文件是不会继承默认权限
[root@localhost ~]# mkdir /mnt/test/hello
[root@localhost ~]# su - student
Last login: Sun Oct 7 02:40:08 EDT 2018 on pts/2
##linux目录原本就存在的
[student@localhost ~]$ cd /mnt/test/linux
[student@localhost linux]$ touch file1
touch: cannot touch ‘file1’: Permission denied
##hello目录是新建的
[student@localhost linux]$ cd /mnt/test/hello
[student@localhost hello]$ touch file2
##去掉默认权限
[root@localhost ~]# setfacl -k /mnt/test
[root@localhost ~]# getfacl /mnt/test
getfacl: Removing leading '/' from absolute path names
# file: mnt/test
# owner: root
# group: root
user::rwx
user:student:rwx
group::r-x
mask::rwx
other::r-x
习题1:
1.新建用户组,shengchan,caiwu,jishu
2.新建用户要求如下:
1)tom 是shengchan组的附加用户
2)harry 是caiwu组的附加用户
3)leo 是jishu组的附加用户
4)新建admin用户,此用户不属于以上提到的三个部门
3.新建目录要求如下:
1)/pub目录为公共存储目录对所有用户可以读,写,执行
2)/sc 目录为生产部存储目录只能对生产部人员可以写入
3)/cw 目录为财务部存储目录只能对财务部人员可以写入中
习题答案:
[root@localhost ~]# groupadd shengchan
[root@localhost ~]# useradd -G shengchan tom
[root@localhost ~]# groupadd caiwu
[root@localhost ~]# useradd -G caiwu harry
[root@localhost ~]# groupadd jishu
[root@localhost ~]# useradd -G jishu leo
[root@localhost ~]# useradd admin
[root@localhost ~]# id tom
uid=2000(tom) gid=2000(tom) groups=2000(tom),1002(shengchan)
[root@localhost ~]# id harry
uid=3000(harry) gid=3000(harry) groups=3000(harry),1003(caiwu)
[root@localhost ~]# id leo
uid=3001(leo) gid=3001(leo) groups=3001(leo),1004(jishu)
[root@localhost ~]# id admin
uid=3002(admin) gid=3002(admin) groups=3002(admin)
[root@localhost ~]# mkdir /pub
[root@localhost ~]# ll -d /pub
drwxr-xr-x 2 root root 6 Oct 5 08:41 /pub
[root@localhost ~]# chmod 777 /pub
[root@localhost ~]# mkdir /sc
[root@localhost ~]# ls -l /sc
total 0
[root@localhost ~]# chgrp shengchan /sc
[root@localhost ~]# chmod 007 /sc
[root@localhost ~]# mkdir /cw
[root@localhost ~]# chgrp caiwu /cw
[root@localhost ~]# chmod 070 /cw
[root@localhost ~]# ls -ld /pub
drwxrwxrwx 2 root root 6 Oct 5 08:41 /pub
[root@localhost ~]# ls -ld /sc
d------rwx 2 root shengchan 6 Oct 5 08:42 /sc
[root@localhost ~]# ls -ld /cw
d---rwx--- 2 root caiwu 6 Oct 5 08:43 /cw
习题2
1.新建用户组,shengchan,caiwu,jishu
2.新建用户要求如下:
1)tom 是shengchan组的附加用户
2)harry 是caiwu组的附加用户
3)leo 是jishu组的附加用户
4)新建admin用户,此用户不属于以上提到的三个部门
3.新建目录要求如下:
1)/pub目录为公共存储目录对所有用户可以读,写,执行
但用户只能删除属于自己的文件
2)/sc 目录为生产部存储目录只能对生产部人员可以写入
并且生产部人员所建立的文件都自动归属到shengchan组中
3)/cw 目录为财务部存储目录只能对财务部人员可以写入
并且财务部人员所建立的文件都自动归属到caiwu组中
4)admin用户对于/sc和/cw目录可以读写执行
习题答案:
[root@localhost ~]# id tom
uid=2000(tom) gid=2000(tom) groups=2000(tom),1002(shengchan)
[root@localhost ~]# id harry
uid=3000(harry) gid=3000(harry) groups=3000(harry),1003(caiwu)
[root@localhost ~]# id leo
uid=3001(leo) gid=3001(leo) groups=3001(leo),1004(jishu)
[root@localhost ~]# id admin
uid=3002(admin) gid=3002(admin) groups=3002(admin)
[root@localhost ~]# mkdir /pub
[root@localhost ~]# chmod 1777 /pub
[root@localhost ~]# mkdir /sc
[root@localhost ~]# chgrp -R shengchan /sc
[root@localhost ~]# chmod 2770 /sc
[root@localhost ~]# mkdir /cw
[root@localhost ~]# chgrp -R caiwu /cw
[root@localhost ~]# chmod 2770 /cw