Linux下文件/目录权限及归属访问权限读(r):允许查看文件内容,显示目录列表写(w):允许修改文件内容,allowsCreate,delete,andmovefilesorsubdirectoryinadirectoryExecutable(x):允许运行程序,切换目录Nopermission(-):无权限权限介绍-rw-r--r--。1rootroot1258Jun32019initial-setup-ks.cfgdrwxr-xr-x。2rootroot6Jun32019Music-代表普通文件d代表目录c代表角色文件l代表链接文件|rwx|r-x|r-x|根|根|-|-|-|-|-||所有者权限|属于组权限|他人许可|业主|Owner)文件所有者(Owner)文件所有者(Owner)文件所属组(Group)文件所属组(Group)文件所属组(Group)其他用户其他用户其他用户|r|瓦|-|r|-|-|r|-|-|-|-|-|-|-|-|-|-|-|4|2|0|4|0|0|4|0|0这个文件权限为644权限修改格式1:chmodugoa[rwx]文件/目录-u,g,o,a分别代表属主,数组,其他用户,所有用户-+,-,=代表增加,减法,设置一个权限ex:[root@localhost~]#touch/root/a.txt[root@localhost~]#lsa.txt-l-rw-r--r--.1rootroot0May2105:56a.txt[root@localhost~]#chmodg+w,o+w/root/a.txt[root@localhost~]#lsa.txt-l-rw-rw-rw-。1rootroot0May2105:56a.txt格式2chmodnnn(三位八进制数)文件/目录例如:[root@localhost~]#chmod644/root/a.txt[root@localhost~]#lsa.txt-l-rw-r--r--。1rootroot0May2105:56a.txt常用选项-R递归修改指定目录下所有文件或子目录的权限Ownership(所有权)Owner:拥有该文件或目录的用户帐户所属组:拥有文件或目录权限修改的组帐号格式:chownownerfile/directorychown:groupfile/directorychownowner:groupfile/directory常用选项-R:递归修改指定目录下所有文件或子目录的所有权权限ex:[root@localhost~]#chowntoma.txt[root@localhost~]#chown:managera.txt[root@localhost~]#lsa.txt-l-rw-r--r--.1汤姆经理05月21日05:56a.txt[root@localhost~]#chownroot:roota.txt[root@localhost~]#lsa.txt-l-rw-r--r--。1rootroot0May2105:56a.txt[root@localhost~]#Exercise-01新建文件/root/bb.txt,所有者为harry,组为manager,所有者可读写,该组可以读写,其他人只能读[root@localhost~]#touch/root/bb.txt[root@localhost~]#useraddharry[root@localhost~]#groupaddmanager[root@localhost~]#chownharry:manager/root/bb.txt[root@localhost~]#chmod774/root/bb.txt[root@localhost~]#lsbb.txt-l-rwxrwxr--.1harrymanager0May2110:40bb.txt新需求设置susa用户对该文件具有读取和执行权限。设置群组group中的成员对该文件具有读取权限。根据以上知识无法解决这个需求。需要设置ACL权限[root@localhost~]#useraddsusa[root@localhost~]#setfacl-mu:susa:rwbb.txt[root@localhost~]#getfaclbb.txt#file:bb.txt#owner:harry#group:manageruser::rwxuser:susa:rw-group::rwxmask::rwxother::r--[root@localhost~]#groupadd组[root@localhost~]#setfacl-mg:group:rbb.txt[root@localhost~]#getfaclbb.txt#文件:bb.txt#owner:harry#group:manageruser::rwxuser:susa:rw-group::rwxgroup:group:r--mask::rwxother::r--ACL权限ACL(AccessControlList),主要目录是提供传统的所有者、组、其他人的读、写和执行权限。ACL上面可以设置详细的权限。可以为单个用户或单个文件/目录设置r、w和x权限。ACLsetfacl-mu:用户名:rwx文件名u:所有者用户名:用户名rwx:权限文件名:文件setfacl-mg:组名:rwx文件名g:所属组groupname:组名查看getfaclfilename删除setfacl-xu:usernamefilename删除用户权限[root@localhost~]#setfacl-mu:susa:rwa.txt[root@localhost~]#getfacla.txt#file:a.txt#owner:root#group:rootuser::rw-user:susa:rw-group::r--mask::rw-other::r--[root@localhost~]#setfacl-xu:susaa.txt[root@localhost~]#getfacla.txt#file:a.txt#owner:root#group:rootuser::rw-group::r--mask::r--other::r--删除组权限[root@localhost~]#setfacl-mg:group:ra.txt[root@localhost~]#getfacla.txt#file:a.txt#owner:root#group:rootuser::rw-group::r--group:group:r--mask::r--other::r--[root@localhost~]#setfacl-xg:groupa.txt[root@localhost~]#getfacla.txt#文件:a.txt#owner:root#group:rootuser::rw-group::r--mask::r--other::r--Exercise-02复制文件/etc/fstab到/var/tmp/fstab,配置文件/var/tmp/fstab的权限文件/var/tmp/fstab的所有者是root用户文件/var/tmp/fstab属于root组文件/var/tmp/fstab不可执行anyone用户natasha可以访问文件/var/tmp/fstab执行读写操作用户harry既不能读取也不能写入文件/var/tmp/fstab所有其他用户(当前和未来)都可以读取文件/var/tmp/fstab[root@localhost~]#useraddharry[root@localhost~]#useraddnatasha[root@localhost~]#cp/etc/fstab/var/tmp/fstab[root@localhost~]#cd/var/tmp/[root@localhosttmp]#lsfstab-l-rw-r--r--.1rootroot465May2111:09fstab[root@localhosttmp]#setfacl-mu:natasha:rwfstab[root@localhosttmp]#setfacl-mu:harry:--fstab[root@localhosttmp]#getfaclfstab#文件:fstab#所有者:root#组:rootuser::rw-user:harry:---user:natasha:rw-group::r--mask::rw-other::r--特殊权限umaskanti-mask0022权限的补充777-022=755#root身份默认权限[root@localhost~]#mkdirroot_dir[root@localhost~]#touchroot_filedrwxr-xr-x.2rootroot6May2208:46root_dir755-rw-r--r--。1rootroot0May2208:46root_file644#普通用户身份默认权限[ruochen@localhost~]$touchruochen_file[ruochen@localhost~]$mkdirruochen_dirdrwxrwxr-x.2ruochenruochen6May2208:49ruochen_dir775-rw-rw-r--.1ruochenruochen0May2208:49ruochen_file664[root@localhost~]#umask0[root@localhost~]#umask0000[root@localhost~]#touchnew_file[root@localhost~]#mkdirnew_dirdrwxrwxrwx.2rootroot6May2208:55new_dir-rw-rw-rw-。1rootroot0May2208:55new_fileadditionalpermissionSETbitpermissionSUID:setforexecutablefile(filehasxbitpermission),权限标识字符:'s'u+s44***执行这个可执行文件时,callExecuteastheownerofthefile[root@localhost~]#ll/etc/shadow----------.1rootroot1585May2207:33/etc/shadow$\color{red}为什么shadow文件没有权限,普通用户还是可以登录/修改密码?$以一个ping命令为例,先将/bin/ping复制给用户/home/ruochen,然后执行/home/ruochen/ping127.0.0.1看看会发生什么?[ruochen@localhost~]$/home/ruochen/ping127.0.0.1ping:icmpopensocket:Operationnotpermitted显然,如果没有权限,我们给他设置SUID权限会怎样?[root@localhost~]#chmodu+s/home/ruochen/ping现在再次执行[ruochen@localhost~]$/home/ruochen/ping127.0.0.1PING127.0.0.1(127.0.0.1)56(84)字节ofdata.64bytesfrom127.0.0.1:icmp_seq=1ttl=64time=0.019ms64bytesfrom127.0.0.1:icmp_seq=2ttl=64time=0.069ms显然,现在可以执行了。$\color{red}由此可见,SUID权限的作用也可以解释上面提到的普通用户登录/修改密码的问题。该目录下新建的文件/目录自动继承父目录的所属组。我们先在ruochen用户下创建一个文件夹和一个文件[ruochen@localhost~]$mkdirsgid_dir[ruochen@localhost~]$cdsgid_dir/[ruochen@localhostsgid_dir]$touchruochen.txt然后在root用户下,切换到/home/ruochen/sgid_dir目录下,新建一个root.txt文件[root@localhost~]#cd/home/ruochen/sgid_dir/[root@localhostsgid_dir]#touchroot.txt然后,我们添加一个sgid权限到新建的目录下[root@localhostruochen]#chmodg+ssgid_dir/root用户下,继续在sgid_dir文件夹下新建目录[root@localhostsgid_dir]#mkdirroot_dir[root@localhostsgid_dir]#lltotal0drwxr-sr-x。2根若琛6May2209:37root_dir-rw-r--r--.1rootroot05月22日09:32root.txt-rw-rw-r--。1ruochenruochen0May2209:30ruochen.txt这时候我们发现$\color{red}root\_dir这组不是root,而是ruochen$,我们看一下root_dir的父目录[root@localhostsgid_dir]#cd..[root@localhostruochen]#lltotal0drwxrwxr-x.2ruochenruochen6May2208:49ruochen_dir-rw-rw-r--.1若尘若陈05月22日08:49ruochen_filedrwxrwsr-x。3ruochenruochen54May2209:37sgid_dir从这里可以看出sgid权限的作用,即$\color{red}“老鼠的儿子会打洞”$粘性位权限(Sticky)设置为公共目录(777),标识为't'1***用户无法删除其目录/tmp、/var/tmpdrwxrwxrwt中其他用户的文件。17rootroot4096May2209:42tmp在roor用户下创建/test目录[root@localhost~]#mkdir/testdrwxr-xr-x。2rootroot6May2209:50test修改权限为777,在.txt文件目录下新建root[root@localhost~]#chmod777/test/drwxrwxrwx.2rootroot6May2209:50test[root@localhosttest]#touchroot.txt然后我们切换到ruochen用户,发现可以切换到/test目录,还可以创建和查看文件[ruochen@localhostshare]$cd/test/[ruochen@localhosttest]$touchruochen.txt[ruochen@localhosttest]$lltotal0-rw-r--r--.1rootroot05月22日09:53root.txt-rw-rw-r--。1ruochenruochen0May2209:54ruochen.txt我们尝试删除ruochen用户下/test目录下的文件[ruochen@localhosttest]$rm-rfroot.txt[ruochen@localhosttest]$lltotal0-写-写-r--。1ruochenruochen0May2209:54ruochen.txt**[ruochen@localhosttest]$rm-rfruochen.txt[ruochen@localhosttest]$lltotal0我们发现若臣用户可以删除里面的所有文件,所以$\color{red}共享文件可以随意删除$,这个是不可能。这时候$\color{red}stickybitpermission$就派上用场了,可以防止用户删除其他用户的文件。在root用户下,给/test/目录权限添加一个stickybit,然后在这个目录下新建一个root.txt文件[root@localhost~]#chmodo+t/test/[root@localhosttest]#touchroot.txt我们再试试删除ruochen用户下的root.txt文件[ruochen@localhosttest]$rm-rfroot.txtrm:cannotremove'root.txt':Operationnotpermitted发现删除被拒绝,这样我们就可以理解stickybit权限Exercise-03创建一个共享目录/home/cnrts,特性如下和目录的执行权限,其他所有用户没有权限(root用户可以访问所有文件和目录)在/home/cnrts目录下创建文件,其所属组会自动设置为属于manager组[root@localhost~]#mkdir/home/cnrts[root@localhost~]#groupaddmanager[root@localhost~]#chown:manager/home/cnrts/[root@localhost~]#chmod2770/home/cnrts/
