设为首页 收藏本站
查看: 1177|回复: 0

[经验分享] Linux系统权限

[复制链接]

尚未签到

发表于 2019-2-17 12:00:14 | 显示全部楼层 |阅读模式
第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即可




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-673549-1-1.html 上篇帖子: linux之sudo、visudo 下篇帖子: linux精华网站
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表