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

Linux系统权限

时间:2018-09-11 12:23:29      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:cannot   成员   查看   name   复制   修改   echo   输出   递归   

第1章 Linux系统权限

1.1 简介
Linux中的文件或目录的权限和用户及用户组关联很大,Linux中每个文件或目录都有一组共9个基础权限位,每三个字符被分为一组,他们分别是属主权限位(占三个字符)、属组权限位(占三个字符)、其他用户权限位(占三个字符)。
比如rwxr-xr-x linux中正是这9个权限位来控制文件属主(User)、属组(Group)、其他用户(Other)基础权限。


1.2 三种角色
用户对资源来说, 有三种角色
User(u): 属主用户(文件所有者)
Group(g): 属组用户(包含组成员)
Other(o): 匿名用户(其他人)


1.3 环境

[root@oldboyedu ~]# ll -d dir
drwxr-xr-x. 2 root root 18 8月  16 17:11 dir
[root@oldboyedu ~]# ll -d dir/file 
-rw-r--r--. 1 root root 0 8月  16 17:11 dir/file

1.3.1 权限描述
/root/dir的权限是所属用户root读写执行,所属组root读执行,其他用户读执行
/root/dir/file的权限是所属用户root读写,所属组root读,其他用户读


1.4 修改文件所属命令 chown
修改文件所属
chown [user][.|:][group] [-R] filename

//修改属主
[root@oldboyedu ~]# chown oldboy /root/dir
[root@oldboyedu ~]# ll -d /root/dir


//修改属组
[```
root@oldboyedu ~]# chown .dba /root/dir
[root@oldboyedu ~]# ll -d /root/dir
d-wx--x--x. 2 oldboy dba 29 8月 16 17:24 /root/dir


1.5 修改权限命令chmod


第一种方式: chmod [ugoa] [+-=] [rwx] [-R] filename // a是全部
[root@oldboyedu ~]# chmod u=rx /root/dir
[root@oldboyedu ~]# ll -d dir
dr-xr-xr-x. 2 root root 18 8月 16 17:11 dir
[root@oldboyedu ~]# chmod u+w /root/dir
[root@oldboyedu ~]# ll -d dir
drwxr-xr-x. 2 root root 18 8月 16 17:11 dir
[root@oldboyedu ~]# chmod u-w /root/dir
[root@oldboyedu ~]# ll -d dir
dr-xr-xr-x. 2 root root 18 8月 16 17:11 dir
[root@oldboyedu ~]# chmod a=rwx /root/dir
[root@oldboyedu ~]# ll -d dir
drwxrwxrwx. 2 root root 18 8月 16 17:11 dir
[root@oldboyedu ~]# chmod a= /root/dir
[root@oldboyedu ~]# ll -d dir
d---------. 2 root root 18 8月 16 17:11 dir
[root@oldboyedu ~]# chmod =rx /root/dir
[root@oldboyedu ~]# ll -d dir
dr-xr-xr-x. 2 root root 29 8月 16 17:24 dir


第二种方式: chmod nnn [-R] filename //nnn 表示U G O
所属用户rw,属组用户只读,其他用户没权限
4+2=6 4 0
[root@oldboyedu ~]# chmod 640 /root/dir/file
[root@oldboyedu ~]# ll /root/dir/file
-rw-r-----. 1 root root 0 8月 16 17:11 /root/dir/file


1.6 递归修改目录权限(修改目录及子目录权限)

chmod -R 权限 filename
[root@oldboyedu ~]# ll -d /root/dir
drwxr-xr--. 2 root root 29 8月  16 17:24 /root/dir
[root@oldboyedu ~]# ll -d /root/dir/file 
-rw-r-----. 1 root root 0 8月  16 17:11 /root/dir/file
[root@oldboyedu ~]# chmod -R 755 /root/dir
[root@oldboyedu ~]# ll -d /root/dir/file 
-rwxr-xr-x. 1 root root 0 8月  16 17:11 /root/dir/file

第2章 文件权限实验案例


2.1 默认文件其他用户仅有读权限

[root@oldboy ~]# echo "date" >/tmp/date.txt
[root@oldboy ~]# ll /tmp/date.txt
-rw-r--r--. 1 root root 5 Aug 16 06:37 /tmp/date.txt

2.2 测试读权限(无法执行或删除)

[root@oldboy ~]# su - oldboy
[oldboy@oldboy ~]$ cat  /tmp/date.txt
date
[oldboy@oldboy ~]$ echo "test" >/tmp/date.txt 
-bash: /tmp/date.txt: Permission denied
[oldboy@oldboy ~]$ /tmp/date.txt
-bash: /tmp/date.txt: Permission denied

//增加x执行权限
root@oldboy ~]# chmod o+x /tmp/date.txt
[root@oldboy ~]# ll /tmp/date.txt
-rw-r--r-x. 1 root root 5 Aug 16 06:37 /tmp/date.txt


//测试执行权限**

[oldboy@oldboy ~]$ /tmp/date.txt
Thu Aug 16 06:40:56 CST 2018


//增加w写权限

[root@oldboy ~]# chmod o+w /tmp/date.txt
[root@oldboy ~]# ll /tmp/date.txt
-rw-r--rwx 1 root root 5 Aug 16 06:38 /tmp/date.txt


//测试写权限
[oldboy@oldboy ~]$ echo "test" >/tmp/date.txt
或者使用vim编辑文件来测试

第3章 rwx对文件的影响


3.1 读取权限(r)
文件只有r权限: 具有读取\阅读文件内容权限
1.能使用查看类命令 cat、head、tail、less、more
2.不能复制、不能移动、不能编辑,不能删除


3.2 写入权限(w)
如果文件只有w权限: 具有新增、修改文件内容的权限
1.使用vim编辑,会提示权限拒绝, 但可强制保存,会覆盖之前文件内容
2.使用echo命令重定向或追加重定向技术可以往文件内写入数据
3.使用cat命令读取文件, 将读取到的文件输出交给仅有w权限文件的输入
4.不能复制、不能移动、不能删除,(删除需要看上级目录w的权限)


3.3 执行权限(x)
文件只有x权限,具有执行文件的权限。
//注意: 普通用户需要有r权限,管理员不需要
1.不能执行、查看、编辑、复制、移动、删除


第4章 目录权限实验案例


4.1 实战案例1: 对目录没有 w,对文件有 rwx

[root@oldboy ~]# mkdir /test
[root@oldboy ~]# echo "test" > /test/test.txt
[root@oldboy ~]# chmod 777 /test/test.txt
[root@oldboy ~]# ll -d /test
drwxr-xr-x. 2 root root 22 Aug 16 06:52 /test
[root@oldboy ~]# ll /test/test.txt 
-rwxrwxrwx. 1 root root 5 Aug 16 06:52 /test/test.txt

普通用户验证权限

[oldboy@oldboy ~]$ cat /test/test.txt
test
[oldboy@oldboy ~]$ rm -f /test/test.txt
rm: cannot remove ‘/test/test.txt’: Permission denied

4.2 实战案例2: 对目录有 w,对文件没有任何权限

[root@oldboy ~]# chmod 777 /test/
[root@oldboy ~]# chmod 000 /test/test.txt
[root@oldboy ~]# ll -d /test
drwxrwxrwx. 2 root root 22 Aug 16 06:52 /test
[root@oldboy ~]# ll -d /test/test.txt 
----------. 1 root root 5 Aug 16 06:52 /test/test.txt

//普通用户验证权限
[oldboy@oldboy ~]$ cat /test/test.txt
cat: /test/test.txt: Permission denied
[oldboy@oldboy ~]$ rm -f /test/test.txt
[oldboy@oldboy ~]$ touch /test/test1.txt

4.3 实战案例3: 对目录没有 x,对文件有任何权限

[root@oldboy ~]# chmod 766 /test/
[root@oldboy ~]# chmod 777 /test/test.txt
[root@oldboy ~]# ll -d /test/
drwxrw-rw-. 2 root root 22 Aug 16 06:58 /test/
[root@oldboy ~]# ll /test/test.txt 
-rwxrwxrwx. 1 root root 5 Aug 16 06:58 /test/test.txt

//普通用户验证权限
[oldboy@oldboy ~]$ cd /test
-bash: cd: /test: Permission denied
[oldboy@oldboy ~]$ cat /test/test.txt
cat: /test/test.txt: Permission denied
[oldboy@oldboy ~]$ rm -f /test/test.txt
rm: cannot remove ‘/test/test.txt’: Permission denied


第5章 rwx对目录的影响


5.1 目录只有r权限: 具有浏览目录及子目录权限
1.能使用ls命令浏览目录及子目录, 同时会提示权限拒绝
2.能使用ls -l命令浏览目录及子目录, 会带问号,同时只能看到文件名
总结: 目录只有r权限,仅仅只能浏览内的文件名,无其他操作权限


5.2 写入权限(w)
如果目录只有w权限:具有增加、删除或修改目录内文件名权限(需要x配合)
//注意:如果目录有w权限, 可以在目录创建文件, 可以删除目录中的文件(跟文件权限无关)
不能进入目录、不能复制目录、不能删除目录、不能移动目录


5.3 执行权限(x)
目录只有x权限
1.只能进入目录
2.不能浏览、复制、移动、删除


5.4 权限小结:
文件rw权限, 可以查看和编辑文件内容
文件rx权限, 只能查看和执行文件、不能编辑、复制、移动、删除
目录rx权限, 允许浏览目录内文件以及子目录、并允许在目录内新建文件, 不允许创建、删除文件和目录


> 注意事项:

文件, x权限小心给予,建议赋予r或rw即可
目录, w权限小心给予,建议无特殊需求赋予rx即可

Linux系统权限

标签:cannot   成员   查看   name   复制   修改   echo   输出   递归   

原文地址:http://blog.51cto.com/13904281/2173705

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