<>一、 文件权限

<>1.文件属性的查看

ls -l file,会弹出以下的输出
- rw- r-- r-- . 1 root root 0 Dec 31 09:55 file 1 2 3 4 5 6 7 8
1:代表文件的类型
- :普通文件
d : 目录
c:字符设备
s : 嵌套文件
p : 管道
b : 块设备

2:代表文件读写的权限
rw- r-- r-- 拥有者 所属组 其他人 u g o r:读操作 w:写操作 x:执行操作 4 2 1
例:
chmod u+r file #对file的拥有者增加读权限
例:
假定以前是 拥有者 所属组 其他人 r-- r-- r-- 4 4 4 执行后chmod 745 file ,对用户增加了写和执行,对其他人增加了执行权限
拥有者 所属组 其他人 rwx r-- r-x 7 4 5
3:
对文件:表示文件内容被系统记录的次数(只有引用那块内存时会增加次数)
对目录:表示子目录个数(包括. … 隐藏目录)

4:文件的拥有者
5:文件的所属组
6:问价你的内容大小
7:文件最后一次被修改的时间
8:文件的名字

<>2.文件拥有者和所属组的改变
chown username file|dir 改文件/目录的拥有者 chgrp groupname file|dir 改文件/目录的所属组 chown
username.groupname file|dir 两个一起改 chown -R username file|dir (递归修改)改文件/
目录的拥有者,全部都改 chgrp-R groupname file|dir (递归修改)改文件/目录的所属组,全部都改
<>3.文件的普通权限

r=4
对文件来说:可以查看文件的字符(内容)(最高权限,若不能读更不能写)
对目录开说:可以查看目录中文件或目录的信息
w=2
对文件:可以改变文件内的字符(增删改)
对目录:在目录中增删改查
x=1
对文件:可以运行文件中记录的程序和动作
对目录:可以进入目录(最高权限)

<>4.系统默认权限的设定

系统设定新建文件或目录会自动去掉一些权限,以保证安全
设定方式
从系统存在角度来说,开放权力越大,系统存在意义越高
从系统安全角度来说,开放权力越少,系统安全性越高
所以系统设定新建文件或目录会去掉一些权限
新建文件的满权限是666,而目录是777
umask(普通用户和超级用户下都可以使用) #查看系统减掉的权限 umask xxx #修改该系统,使其创建文件或目录时减掉xxx,
此设定是临时设定,只在当前shell生效
<>永久设定umask值
修改: vim /etc/bashrc #第70行 代码 上为普通用户umask 下为超级用户umask vim /etc/profile #第60行 vim
/etc/bashrc 70 if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then 71 umask
002 #普通用户的umask 72 else 73 umask 022 #超级用户的umask 74 fi vim /etc/profile 59 if [
$UID-gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then 60 umask 022 61 else 62 umask
022 63 fi 例:普通用户新建 666/777-002 文件 664 目录 775 超级用户新建 666/777-022 文件 644 目录 755
以上两个文件的umask取值必须保持一致,而且umask取值不是随心所欲,要根据rwx的等级 修改完成后:root下执行[root@desktop0 pub]
# source/etc/bashrc [root@desktop0 pub]# source /etc/profile 让系统重新加载配置文件,让设定立即生效
<>二、文件的访问控制(acl列表)

<>1.acl定义

acl = access control
指定特殊的用户对特殊的文件有特殊的权限,除了当前的用户,还可以给某些用户一些权限
注意:若文件/目录有权限列表时(为+号时),
不可以用ls -l 查看权限,只能有getfacl 查看权限
[kisok@foundation0 ~]$ ls -dl /home/kisok drwx--- + 44 kisok kisok 4096·······
加号就说明开启了acl访问控制列表 注意:若文件/目录有权限列表时(为+号时), 不可以用ls -l 查看权限,只能有getfacl 查看权限 [
kiosk@foundation0~]$ getfacl /home/kiosk getfacl: Removing leading '/' from
absolute path names# file: home/kiosk ##目录/文件的名称 # owner: kiosk ##d/f 的拥有者 #
group: kiosk ##d/f 的所属组 user::rwx ##拥有者的权限 user:qemu:--x ##acl列表中(特殊用户)的权限 group
::--- ##特殊组的权限 mask::--x ##权限掩码 other::--- ##其他人的权限
<>2.设定acl列表

当acl列表开关打开时,该文件有特殊用户为绿色,无特殊用户为黑色
设定acl列表 setfacl -m u:student:rwx file #打开acl开关,并添加特殊用户student , 特殊权限为rwx -m:设定
-u:用户 -g:组 getfacl file #查看具体权限 [root@desktop0 mnt]# ll total 4 -rw-rwxr--+ 1
root root0 Dec 31 14:40 file [root@desktop0 mnt]# getfacl file # file: file #
owner: root # group: root user::rw- user:student:rwx group::r-- mask::rwx other:
:r--
<>3.删除acl列表中的用户或组
setfacl -x u:student file 删除特殊用户student,但是acl列表开关仍然是打开的,此时file文件无特殊用户 [
root@desktop0 mnt]# setfacl -x u:student file [root@desktop0 mnt]# ll total 4 -
rw-r--r--+ 1 root root 0 Dec 31 14:40 file
<>4.关闭列表
setfacl -b file #acl列表+会变成. [root@desktop0 mnt]# setfacl -b file [
root@desktop0 mnt]# ll total 0 -rw-r--r--. 1 root root 0 Dec 31 14:40 file
<>5.设置acl的mask

acl mask,mask用来标示实际能赋予用户最大权限
setfacl -m m:rw- file #将file的mask设置为rw- 代码:[root@desktop0 mnt]# setfacl -m m:rw
- file [root@desktop0 mnt]# getfacl file # file: file # owner: root # group:
root user::r-- user:student:rwx #effective:rw- group::r-- mask::rw- other::---
#会出现effective:rw- ,意味着特殊用户获得的最大权限不能超过rw-
注意:当你用chmod改变文件普通权限时,可能会破坏acl mask

<>6.acl的默认权限

当我们需要普通用户对属于root的某个目录拥有写的权限时,并且目录中新建的子目录对普通用户也生效,那么就要设定acl默认权限
注意:默认权限值对于目录中新建的目录或文件生效,对目录本身无效,对已经建立的文件和目录页无效

<>三、特殊权限位

<>1.suid=4 冒险位

只针对二进制可执行文件
文件内记录的程序产生的进程的拥有者为文件的拥有者
和进程的发起人没关系
[root@desktop0 mnt]# chmod u+s /usr/bin/touch == [root@desktop0 mnt]# chmod
4755 /usr/bin/touch [root@desktop0 mnt]# ll /usr/bin/touch -rwsr-xr-x. 1 root
root62432 Jan 25 2014 /usr/bin/touch [root@desktop0 mnt]# su - student Last
login: Mon Dec 31 16:23:23 CST 2018 on pts/0 [student@desktop0 ~]$ ll total 0 -
rw-------. 1 student student 0 Dec 31 16:23 file [student@desktop0 ~]$ touch
file2[student@desktop0 ~]$ ll total 0 -rw-------. 1 student student 0 Dec 31 16:
23 file -rw-------. 1 root student 0 Dec 31 16:25 file 设定方式: chmod u+s file suid
=4 chmod 4xxx file
<>2.sgid=2 强制位

对文件:只针对二进制可执行文件,
任何人运行二进制文件程序时程序产生的进程的所属组都是文件的所有组
和程序发起人组的身份无关
[root@desktop0 mnt]# chmod g+s /usr/bin/touch ^C [root@desktop0 mnt]# chmod
2755 /usr/bin/touch [root@desktop0 mnt]# ll /usr/bin/touch -rwxr-sr-x. 1 root
root62432 Jan 25 2014 /usr/bin/touch [root@desktop0 mnt]# su - student Last
login: Mon Dec 31 16:25:36 CST 2018 on pts/0 [student@desktop0 ~]$ ls file file2
[student@desktop0 ~]$ touch file3 [student@desktop0 ~]$ ll total 0 -rw-------. 1
student student0 Dec 31 16:23 file -rw-------. 1 root student 0 Dec 31 16:25
file2-rw-------. 1 student root 0 Dec 31 16:43 file3
对目录:当目录有sgid权限后,目录中新建的所有文件的所有组
都自动归属到目录的所有组之中,和文件建立者所在的组无关
[root@desktop0 mnt]# chmod g+s /westos/ [root@desktop0 mnt]# ll -d /westos/
drwxrwsrwx. 3 root root 19 Dec 31 16:46 /westos/ [root@desktop0 mnt]# su -
studnet su: user studnet does not exist [root@desktop0 mnt]# su - student Last
login: Mon Dec 31 16:45:59 CST 2018 on pts/0 [student@desktop0 ~]$ cd /westos/ [
student@desktop0 westos]$ ls stuent [student@desktop0 westos]$ ll total 0 drwx--
----. 2 student student 6 Dec 31 16:46 stuent [student@desktop0 westos]$ touch
ww[student@desktop0 westos]$ ll total 0 drwx------. 2 student student 6 Dec 31
16:46 stuent -rw-------. 1 student root 0 Dec 31 16:47 ww [student@desktop0
westos]$ mkdir ee [student@desktop0 westos]$ ll total 0 drwx--S---. 2 student
root6 Dec 31 16:47 ee drwx------. 2 student student 6 Dec 31 16:46 stuent -rw--
-----. 1 student root 0 Dec 31 16:47 ww
设定方式:
chmod g+s file|dir sgid=2 chmod 2xxx file|dir
<>3.sticky=1 ##粘制位

t=1权限:
只针对于目录,当一个目录上有t权限,那么目录中的文件只能被文件的拥有者删除
drwxrwxrwt. 2 root root 6 Dec 31 17:04 /pub/ [root@desktop0 mnt]# su - westos
Last login: Mon Dec 31 17:03:45 CST 2018 on pts/0 [westos@desktop0 ~]$ cd /pub/
[westos@desktop0 pub]$ touch westos [westos@desktop0 pub]$ ll total 0 -rw-------
. 1 westos root 0 Dec 31 17:06 westos [westos@desktop0 pub]$ su - student
Password: Last login: Mon Dec 31 17:03:26 CST 2018 on pts/0 [student@desktop0 ~]
$ cd/pub/ [student@desktop0 pub]$ ls westos [student@desktop0 pub]$ rm -rf
westos rm: cannot remove ‘westos’: Operation not permitted [student@desktop0 pub
]$ 设定方式: chmod o+t direcotry t=1 chmod 1777 direcotry
练习2:
这次练习的主要命令是groupadd \ useradd \ chgrp \ 权限下放 \umask的设定
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用户能用touch工具在/sc目录和/cw目录中任意建立文件,但不能删除文件
5)admin用户对于/sc和/cw目录可以读写执行
4.设定普通用户新建文件权限为“r–r-----”
5.设定admin用户可以通过sudo自由建立新用户
1. 组的建立 组的建立 groupadd -g ##建立组 groupdel 组名字 ##删除组 [root@localhost Desktop]#
groupadd shengchan[root@localhost Desktop]#groupadd caiwu [root@localhost
Desktop]# groupadd jishu 2. 用户建立 useradd 参数 用户名字 -u ##指定用户uid -g
##指定用户初始组信息,这个组必须已经存在-G ##指定附加组,这个组必须存在 -c ##用户说明 -d ##用户家目录 -s ##用户所使用的shell,/
etc/shells记录了用户能使用shell的名字 [root@localhost Desktop]#useradd -G shengchan tom [
root@localhost Desktop]#useradd -G caiwu harry [root@localhost Desktop]#useradd
-G jishu leo [root@localhost Desktop]#useradd admin 3. 1)粘滞特殊权限: t=1权限: 只针对与目录,
当一个目录上有t权限,那么目录中的文件只能被所有人删除[root@localhost Desktop]# mkdir /pub [root@localhost
Desktop]#chmod 1777 /pub/ 2)强制位=2 [root@localhost Desktop]#mkdir /sc [
root@localhost Desktop]# chgrp shengchan /sc [root@localhost Desktop]# chmod
2770 /sc 3)强制位=2 [root@localhost Desktop]# mkdir /cw [root@localhost Desktop]
#chgrp caiwu/cw [root@localhost Desktop]# chmod 2770 /cw 4)“给admin设定在/
下的acl列表,这样admin就可以在根下的子目录中拥有rwx权限,如何不让admin删除创建的文件,给touch命令设置强制位,然后在设置粘制位,这样只有root用户可以删除touch创建的文件,其他用户不可以删除”
setfacl-m u:admin:rwx /sc #让admin可以进入/sc,并可touch文件 setfacl -m u:admin:rwx /cw
#让admin可以进入/cw,并可touch文件 chmod 4755 /usr/bin/touch #给touch命令增加冒险位,
使得生成文件的所有者都是root chmod1770 /sc #/sc增加强制位,只有所有者可以删除 chmod 1770 /cw #/
cw增加强制位,只有所有者可以删除4.修改Umask vim /etc/bashrc ##shell 70 if [ $UID -gt 199 ] && [
"`id -gn`" = "`id -un`" ]; then 71 umask 440 ##普通用户umask 72 else 73 umask 077
##超级用户umask74 fi vim /etc/profile ##系统 59 if [ $UID -gt 199 ] && [ "`id -gn`" =
"`id -un`" ]; then 60 umask 440 ##普通用户umask 61 else 62 umask 077 ##超级用户umask 63
fi 修改完成后:root下执行[root@desktop0 pub]# source /etc/bashrc [root@desktop0 pub]#
source/etc/profile 5.权力下放 [root@foundation Desktop]#visudo --->admin localhost=(
root) NOPASSWD:/usr/sbin/useradd--->"Esc"--->:wq [root@foundation Desktop]#su -
admin[admin@foundation Desktop]$sudo useradd rr

友情链接
ioDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信