Linux是一个多用户,多任务的操作系统。为保证文件安全,文件权限的设置是重要的,那么文件权限的设置简单来说即为赋于某个用户或组 能够以何种方式 访问某个文件
在Linux中一切都为文件,那么就权限而言,对于一般文件来说,
对于目录文件来说,
[root@tong ~]# touch file1.txt
[root@tong ~]# ll
-rw-r--r-- 1 root root 0 Nov 20 21:22 file1.txt
[root@tong ~]# chmod u+x file1.txt //增加属主拥有执行权限,使用符号
[root@tong ~]# ll
-rwxr--r-- 1 root root 0 Nov 20 21:22 file1.txt
[root@tong ~]# chmod ug=rwx,o=rx file1.txt //增加属主,属组拥有读写执行权限,其他人拥有读写权限
[root@tong ~]# ll
-rwxrwxr-x 1 root root 0 Nov 20 21:22 file1.txt
[root@tong ~]# chmod 621 file2.txt //使用数字修改
[root@tong ~]# ll
total 0
-rw--w---x 1 root root 0 Nov 20 21:45 file2.txt
②chown(语法: chwon 用户名.组名 文件)
[root@tong ~]# useradd tong
[root@tong ~]# groupadd student
[root@tong ~]# chown tong.student file1.txt //更改文件属主和属组
[root@tong ~]# ll
-rwxrwxr-x 1 tong student 0 Nov 20 21:22 file1.txt
[root@tong ~]# chown root file1.txt //只更改文件属主
[root@tong ~]# ll
-rwxrwxr-x 1 root student 0 Nov 20 21:22 file1.txt
[root@tong ~]# chown .root file1.txt //只更改文件属组
[root@tong ~]# ll
-rwxrwxr-x 1 root root 0 Nov 20 21:22 file1.txt
③chgrp: 设置一个文件属于哪个组(属组)
语法: chgrp 组名 文件
[root@tong ~]# chgrp student file1.txt //更改文件属组
[root@tong ~]# ll
-rwxrwxr-x 1 root student 0 Nov 20 21:22 file1.txt
[root@tong ~]# mkdir dir1
[root@tong ~]# chgrp -R student dir1/ //-R递归 更改目录属组,并且子文件继承下去
[root@tong ~]# ll
drwxr-xr-x 2 root student 4096 Nov 20 21:31 dir1
-rwxrwxr-x 1 root student 0 Nov 20 21:22 file1.txt
[root@tong ~]# touch file2.txt
[root@tong ~]# ll
total 0
-rw-r--r-- 1 root root 0 Nov 20 21:45 file2.txt
[root@tong ~]# getfacl file2.txt //查看文件ACL权限
# file: file2.txt
# owner: root
# group: root
user::rw-
group::r--
other::r--
[root@tong ~]# setfacl -m u:tong:rwx file2.txt //给单个用户tong增加读写执行权限
[root@tong ~]# setfacl -m o::rw file2.txt //给其他人设置读写权限
[root@tong ~]# getfacl file2.txt //查看文件ACL权限
# file: file2.txt
# owner: root
# group: root
user::rw-
user:tong:rwx
group::r--
mask::rwx
other::rw-
[root@tong ~]# ll //查看文件,有+号显示
-rw-rwxrw-+ 1 root root 0 Nov 20 21:45 file2.txt
[root@tong ~]# setfacl -x u:tong file2.txt //删除单个用户ACL权限
[root@tong ~]# getfacl file2.txt //查看
# file: file2.txt
# owner: root
# group: root
user::rw-
group::r--
mask::r--
other::rw-
[root@tong ~]# setfacl -b file2.txt //删除全部ACL权限
[root@tong ~]# getfacl file2.txt
# file: file2.txt
# owner: root
# group: root
user::rw-
group::r--
other::rw-
[root@tong ~]# su - tong
[tong@tong ~]$ whoami //显示当前登陆用户
tong
[tong@tong ~]$ cat /root/file2.txt
cat: /root/file2.txt: Permission denied //权限不够,不能访问
[tong@tong ~]$ ll /usr/bin/bash
-rwxr-xr-x 1 root root 960608 Sep 7 2017 /usr/bin/bash
[tong@tong ~]$ exit //登出
logout
[root@tong ~]# chmod u+s /usr/bin/cat //给/usr/bin/cat增加suid位
[root@tong ~]# ll /usr/bin/cat
-rwsr-xr-x. 1 root root 54080 Nov 6 2016 /usr/bin/cat
[root@tong ~]# su - tong //切换用户
Last login: Tue Nov 20 22:14:02 CST 2018 on pts/0
[tong@tong ~]$ cat /root/file2.txt //可以顺利访问了
hello
[root@tong ~]# groupadd team1 //设置所需环境
[root@tong ~]# useradd man01 -G team1
[root@tong ~]# useradd man02 -G team1
[root@tong ~]# mkdir /home/team1
[root@tong ~]# chgrp team1 /home/team1/
[root@tong ~]# chmod g+w /home/team1/
[root@tong ~]# ll -d /home/team1/
drwxrwxr-x 2 root team1 4096 Nov 21 00:14 /home/team1/
[root@tong ~]# chmod g+s /home/team1/ //设置SGID
[root@tong ~]# ll -d /home/team1/
drwxrwsr-x 2 root team1 4096 Nov 21 00:14 /home/team1/
[root@tong ~]# touch /home/team1/test01.txt //在设置SGID文件下创建文件证明效果
[root@tong ~]# ll /home/team1/
-rw-r--r-- 1 root team1 0 Nov 21 00:17 test01.txt //新文件的属组是继承下来了,本来未设置SGID时默认属组为root
sbit=1
作用:sticky针对目录设置,该目录中的内容只有root和属主能够删除
格式:chmod o+x 目录
例子:当有一个共享文件夹,所有的用户可以在其中上传文件,但是你不能让非文件拥有者去删除变换该文件,那么就要用SBIT啦。
(1)未设置SBIT
[root@tong ~]# mkdir /home/dir_test1
[root@tong ~]# chmod 777 /home/dir_test1/
[root@tong ~]# su - alice
Last login: Sat Nov 17 19:31:21 CST 2018 on pts/1
[alice@tong ~]$ touch /home/dir_test1/alice_file1.txt
[alice@tong ~]$ ll /home/dir_test1/alice_file1.txt
-rw-rw-r-- 1 alice alice 0 Nov 21 00:02 /home/dir_test1/alice_file1.txt
[alice@tong ~]$ exit
logout
[root@tong ~]# su - jack
[jack@tong ~]$ rm -rf /home/dir_test1/alice_file1.txt //可以随意删除其他人创建的文件
[jack@tong ~]$ ll /home/dir_test1/
(2)设置SBIT
[root@tong ~]# su - alice
Last login: Wed Nov 21 00:00:46 CST 2018 on pts/0
[alice@tong ~]$ touch /home/dir_test1/alice_file2.txt //一个用户创建文件
[alice@tong ~]$ exit
logout
[root@tong ~]# chmod o+t /home/dir_test1/ //设置SBIT
[root@tong ~]# ll -d /home/dir_test1/
drwxrwxrwt 2 root root 4096 Nov 21 00:04 /home/dir_test1/
[root@tong ~]# su - jack
Last login: Wed Nov 21 00:05:05 CST 2018 on pts/0
[jack@tong ~]$ rm -rf /home/dir_test1/alice_file2.txt //其他除root和所有者用户不可以删除其创立的文件
rm: cannot remove ‘/home/dir_test1/alice_file2.txt’: Operation not permitted
[jack@tong ~]$ exit
logout
[root@tong ~]# rm -rf /home/dir_test1/alice_file2.txt
[root@tong ~]# ll /home/dir_test1/
小结:
不同设置方式:
注:针对所有用户,包括root
[root@tong ~]# touch file3.txt file4.txt
[root@tong ~]# lsattr file3.txt file4.txt //查看文件隐藏属性
-------------e-- file3.txt
-------------e-- file4.txt
[root@tong ~]# chattr +a file3.txt //允许在文件中进行追加操作
[root@tong ~]# chattr +i file4.txt //启动这个属性时,不能更改、重命名或删除这个文件
[root@tong ~]# lsattr file3.txt file4.txt //查看隐藏属性
-----a-------e-- file3.txt
----i--------e-- file4.txt
[root@tong ~]# echo hello >file3.txt //覆盖方式写hello进文件
-bash: file3.txt: Operation not permitted
[root@tong ~]# rm -rf file3.txt //删除此文件
rm: cannot remove ‘file3.txt’: Operation not permitted //不允许删除
[root@tong ~]# echo world >file4.txt //写入失败
-bash: file4.txt: Permission denied
[root@tong ~]# echo world >>file4.txt //追加写入失败
-bash: file4.txt: Permission denied
[root@tong ~]# rm -rf file4.txt //不能删除
rm: cannot remove ‘file4.txt’: Operation not permitted
[root@tong ~]# chattr -a file3.txt //将属性还原
[root@tong ~]# chattr -i file4.txt //i属性删除
[root@tong ~]# lsattr file3.txt file4.txt
-------------e-- file3.txt
-------------e-- file4.txt
[root@tong ~]# lsattr file3.txt file4.txt
-------------e-- file3.txt
-------------e-- file4.txt