赞
踩
我们可以通过ls -l 命令来查看当前目录的文件权限:
[root@hjh ~]# ls -l
total 48
-rw-r--r-- 1 root root 35159 Aug 29 14:09 man.txt
忽略第一位 (第一位代表文件格式类型 - 代表普通文件) r w - 前三位代表 属主(User),中三位 r - - 代表属组(Group), 后三位 r - - 代表其他用户(Other) ,第一个 root 代表文件的属主 , 第二个 root 代表文件的属组, r是 read的意思也就是 可读 ,w 代表write的意思就是可写
,x
是Executable的意思也就是可执行
我们可以通过id
命令来查看当前用户的的信息来判断自己当前的基本信息
[root@hjh ~]# id
uid=0(root) gid=0(root) groups=0(root)
r = 4
,w = 2
, x = 1
, 只要记住这几个数字所有的方式就是轻松快速的更改。if Permission == User:
print("你是属主")
elif Permission in the Group:
print("你是属组的成员")
else:
print("你是其他人")
你可以让用户加入到那个某个拥有此文件你想要权限的组中(当然前提是存在这样的组)
你可以修改文件本身所属主或者所属组(用chown命令),修改文件对外的权限(用chmod命令)
[hjh@hjh ~]$ id
uid=1000(hjh) gid=1000(hjh) groups=1000(hjh)
` 使用usermod命令的 -G选项将 `hjh` 加入到root组中:
[root@hjh ~]# usermod -G root hjh
[root@hjh ~]# id hjh
uid=1000(hjh) gid=1000(hjh) groups=1000(hjh),0(root)
如上图可以看到hjh的额外组(groups)中增加了root ,也就是代表现在`hjh`获得了man.txt的属组的权限。
[root@hjh test]# mkdir dir
[root@hjh test]# ls
dir
hjh test]# touch dir/file
[root@hjh test]# ll -d dir
drwxr-xr-x 2 root root 18 Sep 15 16:58 dir
[root@hjh test]# ll -d dir/file
-rw-r--r-- 1 root root 0 Sep 15 16:58 dir/file
从上面的结果可以看出在新建一个文件时,文件夹所有一般都具有执行权限,而文件一般都没有执行权限,将上面的环境作为下面实验的环境。
修改权限:
chmod
~~~~
[ugoa]
~~~~
[+ - =]
~~~~
[rwx]
~~~~
filename
我们上面的环境做个实验,这里我们选择用户权限来做实验,其他同理。(特别说明=
表示覆盖。
[root@hjh test]# ll -d dir
drwxr-xr-x 2 root root 18 Sep 15 16:58 dir
[root@hjh test]# chmod u=rx dir
[root@hjh test]# ll -d dir
dr-xr-xr-x 2 root root 18 Sep 15 16:58 dir
[root@hjh test]# chmod u+w dir
[root@hjh test]# ll -d dir
drwxr-xr-x 2 root root 18 Sep 15 16:58 dir
[root@hjh test]# chmod u-w dir
[root@hjh test]# ll -d dir
dr-xr-xr-x 2 root root 18 Sep 15 16:58 dir
这里要特殊说明的是修改权限是可以同时指定多用户的,下面我做出示例。
[root@hjh test]# ll -d dir
dr-xr-xr-x 2 root root 18 Sep 15 16:58 dir
[root@hjh test]# chmod ug=rwx dir
[root@hjh test]# ll -d dir
drwxrwxr-x 2 root root 18 Sep 15 16:58 dir
[root@hjh test]# chmod u=rwx,g=rx dir
[root@hjh test]# ll -d dir
drwxr-xr-x 2 root root 18 Sep 15 16:58 dir
[root@hjh test]# chmod u=rwx,g=rwx,o= dir
[root@hjh test]# ll -d dir
drwxrwx--- 2 root root 18 Sep 15 16:58 dir
[root@hjh test]# chmod a=rwx dir
[root@hjh test]# ll -d dir
drwxrwxrwx 2 root root 18 Sep 15 16:58 dir
注:但是这些权限只针对普通用户,对root
用户无效,下面示例:
[root@hjh test]# chmod a= dir
[root@hjh test]# ll -d dir
d--------- 2 root root 18 Sep 15 16:58 dir
[root@hjh test]# ls dir/
file
[root@hjh test]# touch dir/test
对所有用户进行操作权限时你甚至不需声明,示例:
[root@hjh test]# ll -d dir
d--------- 2 root root 30 Sep 15 17:52 dir
[root@hjh test]# chmod a=rx dir
[root@hjh test]# ll -d dir
dr-xr-xr-x 2 root root 30 Sep 15 17:52 dir
[root@hjh test]# chmod = dir
[root@hjh test]# ll -d dir
d--------- 2 root root 30 Sep 15 17:52 dir
chmod
nnn
filename
第一个n: U
第二个n: G
第三个n: O
接下来我们做一个实验:
所属用户权限为 r--
所属组为rx-
其他用户没权限 ---
[root@hjh test]# ll -d dir/file
-rw-r--r-- 1 root root 0 Sep 15 16:58 dir/file
[root@hjh test]# chmod 450 dir/file
[root@hjh test]# ll -d dir/file
-r--r-x--- 1 root root 0 Sep 15 16:58 dir/file
[root@hjh test]#
常见的组合
默认目录是755
文件是644
[root@hjh test]# mkdir test
[root@hjh test]# ll -d test
drwxr-xr-x 2 root root 6 Sep 15 19:48 test
[root@hjh test]# touch test/test.txt
[root@hjh test]# ll -d test/test.txt
-rw-r--r-- 1 root root 0 Sep 15 19:49 test/test.txt
[root@hjh test]#
目录:755,750,700
文件 :644,640,600
[root@hjh test]# ll -d dir
drwxrwxrwx 2 root root 30 Sep 15 17:52 dir
[root@hjh test]# ll -d dir/file
-rwxrwxrwx 1 root root 0 Sep 15 16:58 dir/file
[root@hjh test]# chmod -R = dir/
[root@hjh test]# ll -d dir
d--------- 2 root root 30 Sep 15 17:52 dir
[root@hjh test]# ll -d dir/file
---------- 1 root root 0 Sep 15 16:58 dir/file
[root@hjh test]# ll -d dir d--------- 2 root root 30 Sep 15 17:52 dir [root@hjh test]# chown hjh dir [root@hjh test]# ll -d dir d--------- 2 hjh root 30 Sep 15 17:52 dir [root@hjh test]# chown .dba dir [root@hjh test]# ll -d dir d--------- 2 hjh dba 30 Sep 15 17:52 dir [root@hjh test]# ll -d dir/ file test [root@hjh test]# ll -d dir/file ---------- 1 root root 0 Sep 15 16:58 dir/file [root@hjh test]# chown -R hjh.hjh dir/ [root@hjh test]# ll -d dir dir/file d--------- 2 hjh hjh 30 Sep 15 17:52 dir ---------- 1 hjh hjh 0 Sep 15 16:58 dir/file [root@hjh test]# chown -R root:root dir/ [root@hjh test]# ll -d dir/ dir/file d--------- 2 root root 30 Sep 15 17:52 dir/ ---------- 1 root root 0 Sep 15 16:58 dir/file [root@hjh test]# [root@hjh test]#
//默认文件其他用户仅有读权限 [root@hjh test]# echo "date" > date.txt [root@hjh test]# ll -d date.txt -rw-r--r-- 1 root root 5 Sep 16 21:10 date.txt //测试下权限 [hjh@hjh test]$ cat date.txt date [hjh@hjh test]$ echo "test" > date.txt -bash: date.txt: Permission denied [hjh@hjh test]$ /tmp/test/test/date.txt -bash: /tmp/test/test/date.txt: Permission denied //增加执行权限并测试 [root@hjh test]# chmod o+x date.txt [root@hjh test]# ll -d date.txt -rw-r--r-x 1 root root 5 Sep 16 21:11 date.txt [hjh@hjh test]$ /tmp/test/test/date.txt Mon Sep 16 21:22:33 CST 2019 //增加写权限并测试 [root@hjh test]# chmod o+w date.txt [root@hjh test]# ll -d date.txt -rw-r--rwx 1 root root 5 Sep 16 21:11 date.txt [hjh@hjh test]$ echo "test" > date.txt [hjh@hjh test]$
实战案例一:对目录没有 w 权限 ,对文件有 r w x
//搭建环境 [root@hjh test]# pwd /tmp/test [root@hjh test]# ll -d /tmp/test test.txt -rw-r--r-- 1 root root 0 Sep 16 22:05 test.txt drwxr-xr-x 2 root root 22 Sep 16 22:05 /tmp/test [root@hjh test]# chmod 777 test.txt [root@hjh test]# ll -d test.txt -rwxrwxrwx 1 root root 0 Sep 16 22:05 test.txt [root@hjh test]# echo "test" > test.txt 换普通用户实验 [hjh@hjh ~]$ cat /tmp/test/test.txt test [hjh@hjh ~]$ rm -r /tmp/test/ rm: descend into write-protected directory ‘/tmp/test/’? y rm: cannot remove ‘/tmp/test/test.txt’: Permission denied [hjh@hjh ~]$ echo "test" >> /tmp/test/test.txt [hjh@hjh ~]$ /tmp/test/test.txt [hjh@hjh ~]$
实战案例二:对目录有 w, 对文件没有任何权限
//搭建实验环境 [root@hjh test]# chmod o=w /tmp/test [root@hjh test]# chmod 000 test.txt [root@hjh test]# ll -d /tmp/test/ test.txt ---------- 1 root root 10 Sep 16 22:10 test.txt drwxr-x-w- 2 root root 22 Sep 16 22:05 /tmp/test/ //用普通用户做实验 [hjh@hjh ~]$ cat /tmp/test/test.txt cat: /tmp/test/test.txt: Permission denied [hjh@hjh ~]$ cat /tmp/test/ cat: /tmp/test/: Permission denied [hjh@hjh ~]$ cd /tmp/test/ -bash: cd: /tmp/test/: Permission denied [hjh@hjh ~]$ touch /tmp/test/hjh.txt touch: cannot touch ‘/tmp/test/hjh.txt’: Permission denied // 注:这里你会发现你的写权限都不好使了 //我们切换回root加上x权限 [root@hjh test]# chmod o=wx /tmp/test //我们再切回普通用户 [hjh@hjh ~]$ touch /tmp/test/hjh.txt [hjh@hjh ~]$ //用root用户查看文件是否新建成功 [root@hjh test]# ls hjh.txt test.txt [hjh@hjh ~]$ rm -rf /tmp/test/hjh.txt [hjh@hjh ~]$ [root@hjh test]# chmod o=rw /tmp/test [hjh@hjh test]$ ls /tmp/test/ ls: cannot access /tmp/test/test.txt: Permission denied test.txt 结论:从这里可以看出想要在一个目录中查看或者操作文件必须首先要进入这个目录,也即是要有X权限。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。