一、权限查看及读取
1、权限的查看:
输入ls -l file ------------------------查看文件的权限
输入ls -ld dir ------------------------查看目录的权限
2、权限的读取:
文件权限读取:
- | rw -r--r-- | . | 1 | root | root | 0 | Apr 12 10:57 | westos
1 2 3 4 5 6 7 8 9
目录权限读取:
d | rw -r--r-- | . | 2 | root | root | 0 | Apr 12 10:57 | westosdir
1 2 3 4 5 6 7 8 9
注释:
1:文件类型
2:用户权限
rw-|r--|r--
u g o
3、系统的selinux开启
4、对于文件:文件内容被系统记录次数
对于目录:目录中子目录的个数
5、文件/目录拥有者
6、文件/目录拥有组
7、对于文件:文件内容大小
对于目录:目录中子文件的元数据大小
8、文件内容被修改时间
9、文件名称
二、普通权限的类型及作用
1、在上述中有用户权限这一项其中:rw-|r--|r--
u g o
u:user文件的拥有者,ls -l看到第五列信息
g:文件拥有组 ls -l第六列信息
o:既不是拥有者也不是拥有组成员的其他用户的通称(other)
这里面权限位里优先级:u>g>o
权限类型:
r: 可读,对于文件来说可读取文件内容,对于目录可以ls列出目录中文件
w:可写,对于文件,可以更改文件内容,对于目录,可以在目录中新建或者删除文件
x:可执行,对于文件,可以用文件名称调用文件记录的程序,对于目录,可以进入目录中
三、设定普通权限的方法
我们用chmod命令可以设定文件权限,其中复制权限需要用到reference,
例如
chmod --reference=/tmp /mnt/westosdir -------------------复制tmp目录的权限到/mnt/westosdir上
chmod的字符方式设定
我们在/mnt 下建立一个westos1,然后查看westos1的权限 ,我们想给westos1的u加上读写权限,只需要:
chmod u+rw /mnt/westos1即可
详细的权限示例如下:
chmod u-rw /mnt/westos1
chmod u-rw /mnt/westosfile1
chmod u-rw,g+x,o+wx /mnt/westosfile2
chmod a-rwx /mnt/westosfile3
chmod u=rwx,g=rx,o=--- /mnt/westosfile4
chmod -R u=rwx,g=rx,o=--- /mnt/westosdir/
我们都知道计算机里的计算方式都是2进制,linux也不例外,在表示rwx的方式就是典型的2进制,
"---"表示"000", "rwx"表示"111"=7,由此又得到单独的"r"=4,"w"=2,"x"=1.
四、系统默认权限设定
umask---------------------------表示系统保留的权利,会输出0022,后三位就是对应权位rwx
我们想永久更改就需要进入shell系统配置文件
在70行左右的代码中需要把 umask 022 修改成 umask027,我们需要把/etc/bashrc 和/etc/profile都更改,再输入source /etc/bashrc和source /etc/profile,这是让系统立即识别
会发现umask改成了0027
五、特殊权位
1、
在删除文件时会有非本用户就删除了本用户的文件,对其进行修改
实验
前提建立两个用户lee和westos
在超级用户中建立一个目录给它所有权限(比如建立目录/pub)
chmod /pub
chmod 777 /pub
切换到用户westos
su - westos
touch /pub/westosfile
exit
再切换到用户lee
su - lee
touch /pub/leefile
这时我们在lee里可以使用rm命令
rm -rf /pub/westosfile
rm -rf /pub/leefile
会发现两个文件都在用户lee里删除了
解决问题:
在root里
chmod 1777 /pub
或chmod o+t /pub
再次进入westos
su - westos
touch /pub/westosfile1
exit
进入lee
su - lee
touch /pub/leefile1
rm -rf /pub/leefile1
rm -rf /pub/westosfile1
这就解决了lee用户只能删除了leefile1而不能删除westosfile1
2、
sgid -----强制位
设定:
chmod g+s dir
实验:
建立一个组westoste
再创建一个目录,mkdir /mnt/pub
赋予pub全部权限 chmod 777 /mnt/pub
切换到westos
touch /mnt/pub/file
chmod g+s /mnt/westosdir
就会看到组就变成其他所属组中
3、
一般看到westos用户的/bin/cat
出现
westos westos cat
进入root
chmod g+s /bin/cat 第二个westos变为root
chmod u+s /bin/cat 第一个westos变为root
权限列表
-rw-rw---- 表示acl列表未开启
-rw-rw----+ 表示acl列表开启
getfacl westfile 显示读取列表
setfacl -m u:lee:rw westosfile ------设定
setfacl -m g:westos:rw westosfile
setfacl -m u::rwx westosfile
setfacl -m g::0 westosfile
setfacl -x u:lee westosfile -----------删除列表中的lee
setfacl -m m: ---------------------恢复
权限优先级:拥有者>特殊指定用户>权限多的组>权限少的组>其他