一、文件权限10个字符对应类型和权限
二、Linux普通文件和Linux目录读、写、执行权限说明
标注:Linux 中的文件名是存在于父目录的block里面,并指向这个文件的inode节点
1、linux 普通文件读、写、执行权限说明
2、linux 目录的读、写、执行权限说明
三、环境模拟实践
chmod (change mode) 改变文件或目录权限的命令
chmod命令可以通过两种方法设置权限,第一种是权限字母和操作符表达式,第二种是数字方法(八进制),推荐使用数字方法。
第一种方法:文件权限对应的数字方法(八进制)表示
chmod [数字组合] 文件名
第二种方法:权限字母和操作符表达式
chmod [用户类型] [+|-|=] [字符权限] 文件名
模拟测试准备:
1、创建一个用户组 incahome(主人的家庭,即用户组)
2、创建三个用户
oldboy (主人,即用户)
oldgirl (主人的家人,即用户)
3、test (其他人,即用户)
创建一个目录oldboy 并且目录里创建一个文件test.sh
模拟操作:
[root@oldboy ~]# groupadd incahome ##创建一个用户组
[root@oldboy ~]# useradd oldboy -g incahome ##创建一个用户并分配到指定的incahome用户组,如果oldboy用户存在需使用usermod修改用户组命令
[root@oldboy ~]# useradd oldgirl
[root@oldboy ~]# usermod -g incahome oldgirl ##修改用户oldgirl所属用户组用户必须已创建
[root@oldboy ~]# id oldboy ##查看oldoyb UID和GID
uid=500(oldboy) gid=500(oldboy)groups=500(oldboy)
[root@oldboy ~]# id oldgirl
uid=501(oldgirl) gid=502(incahome)groups=502(incahome)
[root@oldboy ~]# useradd test
[root@oldboy ~]# id test
uid=502(test) gid=503(test)groups=503(test)
##创建oldboy目录,并在oldboy目录下创建一个文件test.sh
[root@oldboy ~]# mkdir /oldboy
[root@oldboy ~]# ls -ld /oldboy/
drwxr-xr-x 2 root root 4096 Dec 1 16:30 /oldboy/
[root@oldboy ~]# echo "echo oldboylinux" > /oldboy/test.sh
[root@oldboy ~]# ls -l /oldboy/test.sh
-rw-r--r-- 1 root root 17 Dec 1 16:33 /oldboy/test.sh
[root@oldboy ~]# chmod +x /oldboy/test.sh
[root@oldboy ~]# ls -l /oldboy/test.sh
-rwxr-xr-x 1 root root 17 Dec 1 16:33 /oldboy/test.sh
[root@oldboy ~]# chown oldboy.incahome /oldboy/test.sh ##修改文件所属主和属组
[root@oldboy ~]# chown oldboy.incahome /oldboy/
[root@oldboy ~]# ls -l /oldboy/test.sh
-rwxr-xr-x 1 oldboy incahome 32 Dec 3 10:36 /oldboy/test.sh
[root@oldboy ~]# ls -ld /oldboy/
drwxr-xr-- 2 oldboy incahome 4096 Dec 4 17:01 /oldboy/
范例1:
[root@oldboy ~]# ls -l /oldboy/test.sh
-rwxr-xr-x 1 oldboy incahome 32 Dec 3 10:36 /oldboy/test.sh
[root@oldboy ~]# chmod 740 /oldboy/test.sh
[root@oldboy ~]# ls -l /oldboy/test.sh
-rwxr----- 1 oldboy incahome 17 Dec 4 17:01 /oldboy/test.sh
[root@oldboy ~]# su - oldgirl
[oldgirl@oldboy ~]$ cat /oldboy/test.sh
echo oldboylinux
[oldgirl@oldboy ~]$ /oldboy/test.sh
-bash: /oldboy/test.sh: Permission denied
[oldgirl@oldboy ~]$ rm -f /oldboy/test.sh
rm: cannot remove `/oldboy/test.sh':Permission denied
[oldgirl@oldboy ~]$ su - test
Password:
[test@oldboy ~]$ cat /oldboy/test.sh
cat: /oldboy/test.sh: Permission denied
[test@oldboy ~]$ /oldboy/test.sh
-bash: /oldboy/test.sh: Permission denied
[test@oldboy ~]$ rm /oldboy/test.sh
rm: cannot remove `/oldboy/test.sh':Permission denied
范例2:
[root@oldboy ~]# chmod 762 /oldboy/test.sh
[root@oldboy ~]# ls -l /oldboy/test.sh
-rwxrw--w- 1 oldboy incahome 17 Dec 4 17:01 /oldboy/test.sh
[oldgirl@oldboy ~]$ ls -ld /oldboy/
drwxr-xr-x 2 root root 4096 Dec 4 17:01 /oldboy/
[oldgirl@oldboy ~]$ rm /oldboy/test.sh ##文件名存在于oldboy目录的block里
rm: cannot remove `/oldboy/test.sh':Permission denied
[oldgirl@oldboy ~]$ echo "testdata" >> /oldboy/test.sh
[oldgirl@oldboy ~]$ cat /oldboy/test.sh
echo oldboylinux
testdata
[oldgirl@oldboy ~]$ su - test
Password:
[test@oldboy ~]$ rm /oldboy/test.sh
rm: cannot remove `/oldboy/test.sh':Permission denied
[test@oldboy ~]$ echo "othersdata" >> /oldboy/test.sh
[test@oldboy ~]$ cat /oldboy/test.sh
cat: /oldboy/test.sh: Permission denied
范例3:
[root@oldboy ~]# ls -l /oldboy/test.sh
-rwxr-x--x 1 oldboy incahome 17 Dec 4 17:47 /oldboy/test.sh
[root@oldboy ~]# su - oldgirl
[oldgirl@oldboy ~]$ cat /oldboy/test.sh
echo oldboylinux
[oldgirl@oldboy ~]$ /oldboy/test.sh
oldboylinux
[oldgirl@oldboy ~]$ su - test
Password:
[test@oldboy ~]$ cat /oldboy/test.sh
cat: /oldboy/test.sh: Permission denied
[test@oldboy ~]$ /oldboy/test.sh
bash: /oldboy/test.sh: Permission denied
范例4:
[root@oldboy ~]# chmod 754 /oldboy/
[root@oldboy ~]# ls -ld /oldboy/
drwxr-xr-- 2 oldboy incahome 4096 Dec 4 17:01 /oldboy/
[root@oldboy ~]# su - oldgirl
[oldgirl@oldboy ~]$ cd /oldboy/
[oldgirl@oldboy oldboy]$ ls /oldboy/
abc.txt test.sh
[oldgirl@oldboy oldboy]$ ls -l /oldboy/
total 4
-rw-r--r-- 1 oldgirl incahome 0 Dec 3 10:41 abc.txt
-rwxr-x--x 1 oldboy incahome 17 Dec 4 17:47 test.sh
[oldgirl@oldboy oldboy]$ su - test
Password:
[test@oldboy ~]$ cd /oldboy/
-bash: cd: /oldboy/: Permission denied
[test@oldboy ~]$ ls /oldboy/
ls: cannot access /oldboy/test.sh:Permission denied
ls: cannot access /oldboy/abc.txt:Permission denied
abc.txt test.sh
[test@oldboy ~]$ ls -l /oldboy/
ls: cannot access /oldboy/test.sh:Permission denied
ls: cannot access /oldboy/abc.txt: Permissiondenied
total 0
-????????? ? ? ? ? ? abc.txt
-????????? ? ? ? ? ? test.sh
范例5:
[root@oldboy ~]# chmod 777 /oldboy/test.sh /oldboy/file.txt
[root@oldboy ~]# chmod 731 /oldboy/
[root@oldboy ~]# ls -ld /oldboy/
drwx-wx--x 2 oldboy incahome 4096 Dec 4 18:05 /oldboy/
[root@oldboy ~]# ls -l /oldboy/
total 8
-rwxrwxrwx 1 root root 9 Dec 4 18:05 file.txt
-rwxrwxrwx 1 oldboy incahome 17 Dec 4 17:47 test.sh
[root@oldboy ~]# su - oldgirl
[oldgirl@oldboy ~]$ rm /oldboy/file.txt
[oldgirl@oldboy ~]$ ls -l /oldboy/
ls: cannot open directory /oldboy/:Permission denied
[oldgirl@oldboy ~]$ exit
logout
[root@oldboy ~]# ls -l /oldboy/
total 4
-rwxrwxrwx 1 oldboy incahome 17 Dec 4 17:47 test.sh
[root@oldboy ~]# su - test
[test@oldboy ~]$ rm /oldboy/test.sh
rm: cannot remove `/oldboy/test.sh':Permission denied
范例6:
[root@oldboy ~]# chmod 716 /oldboy/
[root@oldboy ~]# ls -ld /oldboy/
drwx--xrw- 2 oldboy incahome 4096 Dec 4 18:11 /oldboy/
[root@oldboy ~]# su - oldgirl
[oldgirl@oldboy ~]$ cd /oldboy/
[oldgirl@oldboy oldboy]$ pwd
/oldboy
[oldgirl@oldboy oldboy]$ su - test
Password:
[test@oldboy ~]$ cd /oldboy/
-bash: cd: /oldboy/: Permission denied
[test@oldboy ~]$ ls -l /oldboy/
ls: cannot access /oldboy/test.sh: Permissiondenied
total 0
-????????? ? ? ? ? ? test.sh
网友评论