Linux权限管理之基本权限

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux权限管理之基本权限相关的知识,希望对你有一定的参考价值。

参考技术A (Linux权限管理之基本权限-慕课网 http://www.imooc.com/learn/481 )

一、文件基本权限
1、基本权限的修改
-rw-r--r--
- 文件类型(- 文件 d 目录 l 软链接文件)
rw- u所有者
r-- g所属组
r-- o其他人
r 读
w 写
x 执行
chmod命令
chmod [选项] 模式 文件名
- 选项
-R 递归
- 模式
[ugoa] [+-=] [rwx]
[mode=421]

chmod u+x cangls.av
chmod g+w o+w furong.av
chmod a=rwx fengjie.av
#创建新文件
[root@localhost ~]# touch cangls.av
[root@localhost ~]# ll
#所有者添加执行权限
[root@localhost ~]# chmod u+x cangls.av
[root@localhost ~]# ll
#所属组和其他人添加写权限
[root@localhost ~]# chmod g+w,o+w cangls.av
[root@localhost ~]# ll
#所有者减去执行权限,所属组和其他人减去写权限
[root@localhost ~]# chmod u-x,g-w,o-w cangls.av
[root@localhost ~]# ll
#所有者添加读写执行权限,所属组添加读写权限
[root@localhost ~]# chmod u=rwx,g=rw cangls.av
[root@localhost ~]# ll
#所有人赋予读写权限
[root@localhost ~]# chmod a=rw cangls.av
[root@localhost ~]# ll

r ----4
w ----2
x ----1
rwxr-xr-x
7 5 5
#所有者赋予读写执行权限,所属组和其他人赋予读执行权限
[root@localhost ~]# chmod 755 cangls.av
[root@localhost ~]# ll
#改为普通文件权限
[root@localhost ~]# chmod 644 cangls.av
[root@localhost ~]# ll
777(文件最高权限)
644(普通文件权限)
755(所有者赋予读写执行权限,所属组和其他人赋予读执行权限)

r:读取文件的内容(cat more head tail)
w:编辑、新增、修改文件内容(vi echo),但是不包含删除文件;
x:可执行

r:可以查询目录下文件名(ls)
w:具有修改目录结构的权限。如新建文件和目录,删除此目录下文件和目录,重命名此目录下文件和目录,剪切(touch rm mv cp)
x:可以进入目录(cd)
对文件来讲:最高权限是x(执行)
对目录来讲:最高权限是w(写),能赋予的权限只有0,5,7
#创建普通用户user1
[root@localhost ~]# useradd user1
#创建普通用户密码user1
[root@localhost ~]# passwd user1
更改用户 user1 的密码 。
新的 密码:123
无效的密码: WAY 过短
无效的密码: 过于简单
重新输入新的 密码:123
passwd: 所有的身份验证令牌已经成功更新。

chown 用户名 文件名
例如:chown ds fengj.av

chgrp 组名 文件名
例如:chgrp group1 fengj.av

要求:
拥有一个av目录
让加藤老师拥有所有的权限
让本课程学员有查看的权限
其他所有人不许查看这个目录
#创建目录
[root@localhost ~]# mkdir av
#查看
[root@localhost ~]# ll -d av
[root@localhost ~]# useradd jt
[root@localhost ~]# passwd jt
更改用户 jt 的密码 。
新的 密码:
无效的密码: WAY 过短
无效的密码: 过于简单
重新输入新的 密码:
passwd: 所有的身份验证令牌已经成功更新。
#添加组
[root@localhost ~]# groupadd user
#把user1用户加到user组里面
[root@localhost ~]# gpasswd -a user1 user
Adding user user1 to group user
[root@localhost ~]# chown jt:user av
[root@localhost ~]# ll -d av
#其他人改为0权限
[root@localhost ~]# chmod 750 av
[root@localhost ~]# ll -d av

#查看默认权限
[root@localhost ~]# umask
0022
第一位0:文件特殊权限
022:文件默认权限

文件默认不能建立为执行文件,必须手工赋予执行权限;
所以文件默认权限最大为666;
默认权限需要换算成字母再相减;
建立文件之后的默认权限,为666减去umask值;
例如:
文件默认最大权限666,umask值022;
-rw-rw-rw-减去-----w--w-等于-rw-r--r--
例如:
文件默认最大权限666,umask值033
-rw-rw-rw-减去-----wx-wx等于-rw-r--r--

目录的默认权限最大为777;
默认权限需要换算成字母再相减;
建立文件之后的默认权限,为777减去umask值;
例如:
目录默认最大权限为777,umask值022;
-rwxrwxrwx减去-----w--w-等于-rwxr-xr-x

临时修改
umask 0002
永久修改
vi /etc/profile

linux权限管理之基本权限

 

基本权限 UGO


========================================================

文件权限设置: 可以赋于某个用户或组 能够以何种方式 访问某个文件

文件权限管理之: UGO设置基本权限(r、w、x)
rw-r--r-- alice hr install.log

权限对象:
属主: u
属组: g
其他人: o

权限类型:
读:r 4
写:w 2
执行: x 1

===设置权限
1. 更改文件的属主、属组
=chown:
[[email protected] ~]# chown alice.hr file1 //改属主、属组
[[email protected] ~]# chown alice file1 //只改属主
[[email protected] ~]# chown .hr file1 //只改属组
=chgrp:
[[email protected] ~]# chgrp it file1 //改文件属组
[[email protected] ~]# chgrp -R it dir1 //改文件属组

2. 更改权限
=a. 使用符号
对象 赋值符 权限类型
u + r
chmod g - w file1
o = x
a
[[email protected] ~]# chmod u+x file1 //属主增加执行
[[email protected] ~]# chmod a=rwx file1 //所有人等于读写执行
[[email protected] ~]# chmod a=- file1 //所有人没有权限
[[email protected] ~]# chmod ug=rw,o=r file1 //属主属组等于读写,其他人只读
[[email protected] ~]# ll file1 //以长模式方式查看文件权限
-rw-rw-r-- 1 alice it 17 10-25 16:45 file1 //显示的结果

=b. 使用数字
[[email protected] ~]# chmod 644 file1
[[email protected] ~]# ll file1
-rw-r--r-- 1 alice it 17 10-25 16:45 file1


===设置权限示例
针对hr部门的访问目录设置权限,要求如下:
1. root用户和hr组的员工可以读、写、执行
2. 其他用户没有任何权限
[[email protected] ~]# groupadd hr
[[email protected] ~]# useradd hr01 -G hr
[[email protected] ~]# useradd hr02 -G hr
[[email protected] ~]# mkdir /home/hr

[[email protected] ~]# chgrp hr /home/hr
[[email protected] ~]# chmod 770 /home/hr
[[email protected] ~]# ll -d /home/hr/
drwxrwx---. 2 root hr 4096 3月 13 14:26 /home/hr/


重要: r、w、x权限对文件和目录的意义


技术图片

示例1: 对文件的影响
[[email protected] ~]# mkdir /dir10
[[email protected] ~]# touch /dir10/file1
[[email protected] ~]# chmod 777 /dir10/file1

[[email protected] ~]# ll -d /dir10/
drwxr-xr-x. 2 root root 4096 3月 11 18:37 /dir10/
[[email protected] ~]# ll /dir10/file1
-rwxrwxrwx. 1 root root 0 3月 11 18:37 /dir10/file1

[[email protected] ~]$ cat /dir10/file1
[[email protected] ~]$ rm -rf /dir10/file1
rm: 无法删除"/dir10/file1": 权限不够

示例2: 对目录有w权限
[[email protected] ~]# chmod 777 /dir10/
[[email protected] ~]# chmod 000 /dir10/file1
[[email protected] ~]# ll -d /dir10/
drwxrwxrwx. 2 root root 4096 3月 11 18:37 /dir10/
[[email protected] ~]# ll /dir10/file1
----------. 1 root root 0 3月 11 18:37 /dir10/file1

[[email protected] ~]$ cat /dir10/file1
cat: /dir10/file1: 权限不够
[[email protected] ~]$ rm -rf /dir10/file1
[[email protected] ~]$ touch /dir10/file2

问题1:
[[email protected] ~]# ll /root/install.log
-rw-r--r--. 1 root root 46571 6月 1 23:37 /root/install.log
[[email protected] ~]$ cat /root/install.log
cat: /root/install.log: 权限不够

问题2: alice能删除/下的任何文件吗?
[[email protected] ~]# chmod 777 /
[[email protected] ~]# ll -d /
drwxrwxrwx. 27 root root 4096 6月 4 11:32 /
[[email protected] ~]$ rm -rf /etc

 

 

基本权限 ACL


========================================================

文件权限管理之: ACL设置基本权限(r、w、x)
UGO设置基本权限: 只能一个用户,一个组和其他人
ACL 设置基本权限: r,w,x

=ACL基本用法=
设置:
[[email protected] ~]# touch /home/test.txt
[[email protected] ~]# ll /home/test.txt
-rw-r--r-- 1 root root 0 10-26 13:59 /home/test.txt

[[email protected] ~]# getfacl /home/test.txt
[[email protected] ~]# setfacl -m u:alice:rw /home/test.txt //增加用户alice权限
[[email protected] ~]# setfacl -m u:jack:- /home/test.txt //增加用户jack权限
[[email protected] ~]# setfacl -m o::rw /home/test.txt

查看/删除:
[[email protected] ~]# ll /home/test.txt
-rw-rw-r--+ 1 root root 0 10-26 13:59 /home/test.txt
[[email protected] ~]# getfacl /home/test.txt

[[email protected] ~]# setfacl -m g:hr:r /home/test.txt
[[email protected] ~]# setfacl -x g:hr /home/test.txt //删除组hr的acl权限
[[email protected] ~]# setfacl -b /home/test.txt //删除所有acl权限

=查看帮助=
[[email protected] ~]# man setfacl
/EXAMPLES
[[email protected] ~]# getfacl file1 |setfacl --set-file=- file2 //复制file1的ACL权限给file2


=ACL高级用法=
mask:
用于临时降低用户或组(除属主和其他人)的权限
建议:为了方便管理文件权限,其他人的权限置为空
[[email protected] ~]# setfacl -m m::--- /home/file100.txt


default: 继承(默认)
要求: 希望alice能够对/home以及以后在/home下新建的文件有读、写、执行权限

思路:
步骤一: 赋予alice对/home读、写、执行权限
[[email protected] ~]# setfacl -m u:alice:rwx /home

步骤二: 赋予alice对以后在/home下新建的文件有读、写、执行权限 (使alice的权限继承)
[[email protected] ~]# setfacl -m d:u:alice:rwx /home



以上是关于Linux权限管理之基本权限的主要内容,如果未能解决你的问题,请参考以下文章

2022-03-24 学好大数据先攻克 Linux 之权限管理

Linux之权限管理

Python基础之Linux基础:用户权限相关命令

Linux权限管理基本知识

[Linux之权限管理⽤户组管理]

Linux-文件权限