基本权限 UGO

1标题文件权限管理: UGO 设置基本权限(r、w、x)

权限对象 权限类型
属主: u 读:r 4
属组: g 写:W 2
其他人: o 执行: x 1

1.1设置权限

1.1.1更改文件的属主属组
chown

[root@localhost ~]# touch file
[root@localhost ~]#ll -d file
-rw-r--r--. 1 root root 261 Oct 14 14:06 file
在root用户下创建一个文件他的属主和属组都是root
[root@localhost ~]#chown alice.hr file //改属主属组
-rw-r--r--. 1 alice hr 261 Oct 14 14:06 file //注意修改的主和组要存在
[root@localhost ~]#chown root file//只改属主
[root@localhost ~]# chown .root file//只改属组注意组前有个点
将file文件的属主属组又分别改回root
查看主组:ll -d 文件名

chgrp 改变文件的属组
chgrp 组名 文件名

1.2 更改权限 chmod
1.2.1 符号更改

符号 意义
+ 增加权限
- 去除权限
= 赋予权限(权限等于赋予值)
a 所有者(主组其他)

[root@localhost ~]# chmod u+x file //属主增加执行
[root@localhost ~]# chmod u-x file //属主去除执行
[root@localhost ~]# chmod u=x file //属主只有执行
[root@localhost ~]# chmod a+x file //属主/组其他都有执行
[root@localhost ~]# chmod a=- file1 //所有人没有权限

1.2.2 数字更改
[root@localhost ~]# chmod 644 file
[root@localhost ~]# ll file
-rw-r–r-- 1 alice it 17 10-25 16:45 file1
例题:创建文/home/file root 用户和 hr 组的员工可以读、写、执行;

先创造条件
[root@localhost ~]# touch /home/file
[root@localhost ~]# groupadd hr
[root@localhost ~]# useradd hr1 -G hr //创建用户hr1指定组hr
[root@localhost ~]# useradd hr2 -G hr1 //创建用户同时附加创造了组(用户组同名)
[root@localhost ~]# chgrp hr /home/file
[root@localhost ~]# chmod 770 /home/file //去除其他用户对文件的所有权限
[root@localhost ~]# ll /home/file
-rwxrwx—. 1 root root 0 Oct 16 21:47 /home/file
[root@localhost ~]# cat >>/home/file << eof //给文件里写入内容方便查看
123
456
eof
[root@localhost ~]# cat /home/file
123
456
[root@localhost ~]# su - hr1
[hr1@localhost ~]$ cat /home/file
123
456
[hr2@localhost ~]$ cat /home/file
cat: /home/file: Permission denied //hr2用户没有权限查看文件
解析:本题主要考察文件的权限设置对文件查看的影响;首先根据题意知道文件的属主与属组对文件具有最高权限其他没有权限;所以文件的查看只有属主与属组可以;这里要注意文件的增删改查与文件上级目录权限有关
[root@localhost home]# ll -d /home/ //home的权限是757属主其他对其有最高权限属组对其有读和执行权;
drwxr-xrwx. 19 root root 248 Oct 16 22:05 /home/
[root@localhost home]# chmod 717 /home/ //去除组的可读权限
[root@localhost home]# ll -d /home/
drwx–xrwx. 19 root root 248 Oct 16 22:05 /home/
[hr1@localhost ~]$ cat /home/file
123
456
去除了组的可读权限为什么hr1还可以查看file呢?试试去除/home的其他人权限看看hr1还能否读file
[root@localhost ~]# chmod 710 /home/
[root@localhost ~]# ll -d /home/
drwx–x—. 19 root root 248 Oct 16 22:05 /home/
[hr1@localhost ~]$ cat /home/file
cat: /home/file: Permission denied
这里要看清每一级目录的属主属组,/home的属主属组都是root,所以更改的组的可读权限只是对其root组下的成员具有作用,而hr1不是其组下的成员是其他人;
这里你还可以将hr1的属组改为root增加组的可读权限再看看hr1是否可读/home/file;//和上面情况一样去除其他人所有权限属组下的成员具有组的权限;

1.3 r, w, x 权限对文件和目录的意义

字母 文件 目录
r 可获取文件的数据 可使用 ls 命令获取其下的所有文件列表
w 可修改文件的数据 可修改此目录下的文件列表;即创建或删除文件
x 可将此文件运行为进程 可 cd 至此目录中,且可使用 ls -l 来获取所有文件的详细属性信息;

1.3.1 文件的r,w,x对文件的影响
(1) 文件的 r 对于文件的影响
[hr1@localhost ~]$ cat >> file << eof
123
456
789
eof
[hr1@localhost ~]$ ll -d file
-rw-rw-r–. 1 hr1 hr1 12 Oct 18 11:29 file
[hr1@localhost ~]$ cat file
123
456
789
[hr1@localhost ~]$ chmod 333 file
[hr1@localhost ~]$ ll -d file
–wx-wx-wx. 1 hr1 hr1 12 Oct 18 11:29 file
[hr1@localhost ~]$ cat file
cat: file: Permission denied

(2)文件的 w 对文件的影响
[hr1@localhost ~]$ chmod 555 file
[hr1@localhost ~]$ ll -d file
-r-xr-xr-x. 1 hr1 hr1 12 Oct 18 11:29 file
[hr1@localhost ~]$ rm -rf file
[hr1@localhost ~]$ ls
[hr1@localhost ~]$

1.3.2 目录的r ,w,x
(1)目录的 r 权限影响
[hr1@localhost ~]$ mkdir dir
[hr1@localhost ~]$ ls
dir file
[hr1@localhost ~]$ touch /dir/file
[hr1@localhost ~]$ ll -d dir/
drwxrwxr-x. 2 hr1 hr1 6 Oct 18 12:13 dir/
[hr1@localhost ~]$ chmod a-r dir/
[hr1@localhost ~]$ ll -d dir/
d-wx-wx–x. 2 hr1 hr1 6 Oct 18 12:13 dir/
[hr1@localhost ~]$ cd dir/
[hr1@localhost dir]$ ls
ls: cannot open directory .: Permission denied

(2)上级目录没有w权限对下级文件的影响
[root@localhost ~]# mkdir /dir
[root@localhost ~]# touch /dir/file
[root@localhost ~]# chmod 777 /dir/file
[root@localhost ~]# ll -d /dir
drwxr-xr-x. 2 root root 18 Oct 18 11:18 /dir
[root@localhost ~]# ll -d /dir/file
-rwxrwxrwx. 1 root root 0 Oct 18 11:18 /dir/file
[root@localhost ~]# cat >> /dir/file <<eof
123
eof
[hr1@localhost ~]$ cat /dir/file
123
[hr1@localhost ~]$ rm -rf /dir/file
rm: cannot remove ‘/dir/file’: Permission denied

1.4 基本权限 ACL
setfacl 命令可以针对单一用户或用户组、单一文件或目录来进行读/写/执行权限的控制
[root@localhost ~]# touch /home/file
[root@localhost ~]# ll -d /home/file
-rw-r–r--. 1 root root 0 Oct 18 17:19 /home/file
[root@localhost ~]# getfacl /home/file
getfacl: Removing leading ‘/’ from absolute path names
#file: home/file
#owner: root
#group: root
user::rw-
group::r–
other::r–

[root@localhost ~]# cat >> /home/file <<eof
hellowrod
eof
[root@localhost ~]# chmod 770 /home/file
[root@localhost ~]# ll -d /home/file
-rwxrwx—. 1 root root 10 Oct 18 17:22 /home/file
[hr1@localhost ~]$ cat /home/file
cat: /home/file: Permission denied
[root@localhost ~]# setfacl -m u:hr1:r /home/file
[root@localhost ~]# getfacl /home/file
getfacl: Removing leading ‘/’ from absolute path names
#file: home/file
#owner: root
#group: root
user::rwx
user:hr1:r–
group::rwx
mask::rwx
other::—
[hr1@localhost ~]$ cat /home/file
hellowrod
创建文件/home/file并在里面写入内容,修改文件权限为770其他人对其没有任何权限在运用setfacl对文件加入单个用户hr1的可读权限,这时用户hr1对文件具有了单一可读权;
[root@localhost ~]# setfacl -m g:hr:r /home/file //增加hr组的acl权限
[root@localhost ~]# setfacl -x g:hr /home/file //删除组 hr的 acl 权限
[root@localhost ~]# setfacl -b /home/file //删除所有 acl 权限
要求: 希望 alice 能够对/home 以及以后在/home 下新建的文件有读、写、执行权限
思路:
步骤一: 赋予 alice 对/home 读、写、执行权限
[root@localhost ~]# setfacl -m u:alice:rwx /home
步骤二: 赋予 alice 对以后在/home 下新建的文件有读、写、执行
权限 (使 alice 的权限继承)
[root@localhost ~]# setfacl -m d:u:alice:rwx /home

猜你喜欢

转载自blog.csdn.net/weixin_43168314/article/details/83098602
今日推荐