【linux】centos7学习日记-4

目录

权限管理

1.基本权限UGO

权限应用

rwx对于文件的意义:

rwx对于目录的意义

2. 更改权限

=a. 使用符号

=b. 使用数字

3.高级权限

一:高级权限的类型

二:设置特殊权限

4.权限掩码umask

5.文件系统属性(隐藏权限)


which  + 命令    //查看该命令所处的位置,判断该命令是普通用户可以执行的命令还是管理员可以执行的命令
[root@localhost ~]# which passwd
/usr/bin/passwd
[root@localhost ~]# which usermod
/usr/sbin/usermod

扩展:
1.在普通用户利用passwd设置密码时,注意 密码强度 管理员的密码
2.root账户可以直接切换到任意用户,普通用户之间切换需要输入密码
3.非交互设置用户密码 echo ‘123’ | passwd --stdin root
#!/bin/bash
for i in {1..100}
do
    useradd name$i
    echo 'qf' | passwd --stdin name$i
done
4.临时关闭selinux
setenforce 0 
查看selinux的状态
getenforce
永久修改(修改selinux配置文件,值改为disabled)
关闭防火墙
systemctl stop firewalld
systemctl disable firewalld

权限管理

1.基本权限UGO

-rw-r--r--. 1 root root    37 Nov 19 23:45 a.txt
   u  g   o

r 读 4
w 写 2
x 执行 1
-

rwx -w- r-- a.txt
724

r-x rw- rwx b.txt
567

777
621 c.txt
rw- -w- --x

权限应用

chmod

chmod 642 a.txt

-rw-r--r--. 1 root root    37 Nov 19 23:45 v.txt
案例一:
1.给v.txt的其他人增加执行的权限
chmod o+x v.txt
chmod 645 v.txt
2.取消所有者对v.txt的写的权限
chmod u-w v.txt
3.给所有人对于v.txt都设置写的权限
chmod a+w v.txt
4.取消所有者和其他人的对于v.txt的读权限
chmod u-r,o-r v.txt
5.设置所有者对于v.txt拥有读写执行的权限
chmod u=rwx v.txt
6.取消所有人对于v.txt的所有权限
chmod a=- v.txt

参数:-R 递归
1.现有一个目录a,目录a下所有的文件都设置权限为777
[root@localhost ~]# chmod 777 a -R

chmod        u    g    o    a
r        +
w        -
x        =


chown 设置文件/目录的所有者和所属组
1.设置文件的所有者和所属组为lcr
chown lcr.lcr k.txt
chown lcr. k.txt
2.设置文件的所有者为lcr
chown lcr c.txt
3.设置文件的所属组为hr
[root@localhost ~]# chown .hr c.txt
参数 -R 递归设置
3.将a目录和a目录下所有的文件所有者都设置为qf
[root@localhost ~]# chown qf a -R


练习题
1.创建一个目录为lcr,在lcr的目录下创建文件a,b,c;创建用户alice,jack,tom;创建组hr,xz,cw
jack属于xz;tom属于hr,alice属于cw
a文件的所有者为alice,隶属于hr,jack对于a文件没有任何权限
b文件的所有者为jack,其他人对于b文件只有读的权限
c文件的所有者为tom

mkdir lcr
touch lcr/{a,b,c}
useradd alice
useradd jack
useradd tom
groupadd hr
groupadd xz
groupadd cw

gpasswd -a jack xz
gpasswd -a  tom hr
gpasswd -a alice cw

chown alice.hr  lcr/a
chmod o=- lcr/a
chown jack lcr/b
chmod o=r lcr/b
chown tom lcr/c

rwx对于文件的意义:

x:
[root@localhost opt]# ll
-rw-r--r--  1 xingdian root   5 Nov 22 22:36 p.txt
[root@localhost opt]# su - lcr
[lcr@localhost ~]$ cd /opt/
[lcr@localhost opt]$ ll
-rw-r--r--  1 lcr root   5 Nov 22 22:36 p.txt
[lcr@localhost opt]$ ./p.txt
-bash: ./p.txt: Permission denied
[lcr@localhost opt]$ /opt/p.txt
-bash: /opt/p.txt: Permission denied
[lcr@localhost opt]$ exit
logout
[root@localhost opt]# chmod u+x p.txt
[root@localhost opt]# su - lcr
Last login: Mon Nov 22 22:36:30 CST 2021 on pts/0
[lcr@localhost ~]$ /opt/p.txt
Mon Nov 22 22:38:03 CST 2021
[lcr@localhost ~]$ cd /opt/
[lcr@localhost opt]$ ./p.txt
Mon Nov 22 22:38:09 CST 2021
w:
[root@localhost opt]# chmod u=- p.txt
[root@localhost opt]# ll
----r--r--. 1 lcr root 850 Nov 19 17:12 p.txt
[root@localhost opt]# su - lcr
Last login: Mon Nov 22 22:31:39 CST 2021 on pts/0
[lcr@localhost ~]$ echo "123" > /opt/p.txt
-bash: /opt/p.txt: Permission denied
[lcr@localhost ~]$ exit
logout
[root@localhost opt]# chmod u+w p.txt
[root@localhost opt]# su - lcr
Last login: Mon Nov 22 22:33:17 CST 2021 on pts/0
[lcr@localhost ~]$ echo "123" > /opt/p.txt
r:
[root@localhost opt]# chmod u=- p.txt
[root@localhost opt]# ll
----r--r--. 1 lcr root 850 Nov 19 17:12 p.txt
[root@localhost opt]# su - lcr
Last login: Mon Nov 22 18:53:00 CST 2021 on pts/0
[lcr@localhost ~]$ cat /opt/p.txt
cat: /opt/p.txt: Permission denied
[lcr@localhost ~]$ exit
logout
[root@localhost opt]# chmod u=r p.txt
[root@localhost opt]# su - lcr
Last login: Mon Nov 22 22:31:06 CST 2021 on pts/0
[lcr@localhost ~]$ cat /opt/p.txt

总结:
r:读文件  cat head tail ....
w:写文件 echo vim
x:执行文件  ./   /

rwx对于目录的意义

案例一:w
[root@localhost opt]# mkdir dir10
[root@localhost opt]# touch dir10/file1
[root@localhost opt]# ll -d dir10
drwxr-xr-x 2 root root 19 Nov 22 22:46 dir10
[root@localhost opt]# chmod 777 dir10/file1
[root@localhost opt]# ll dir10
total 0
-rwxrwxrwx 1 root root 0 Nov 22 22:46 file1
[root@localhost opt]# su - lcr
Last login: Mon Nov 22 22:37:58 CST 2021 on pts/0
[lcr@localhost ~]$ cat /opt/dir10/file1
[lcr@localhost ~]$ rm -rf /opt/dir10/file1
rm: cannot remove ‘/opt/dir10/file1’: Permission denied
[lcr@localhost ~]$ exit
logout
[root@localhost opt]# chmod o+w /opt/dir10
[root@localhost opt]# chmod 770 /opt/dir10/file1
[root@localhost opt]# ll -d dir10
drwxr-xrwx 2 root root 19 Nov 22 22:46 dir10
[root@localhost opt]# ll dir10
total 0
-rwxrwx--- 1 root root 0 Nov 22 22:46 file1
[root@localhost opt]# su - lcr
Last login: Mon Nov 22 22:47:15 CST 2021 on pts/0
[lcr@localhost ~]$ cat /opt/dir10/file1
cat: /opt/dir10/file1: Permission denied
[lcr@localhost ~]$ rm -rf /opt/dir10/file1
[lcr@localhost ~]$ touch /opt/dir10/file1
x:
[root@localhost opt]# mkdir dir11
[root@localhost opt]# touch dir11/file1
[root@localhost opt]# chmod 770 dir11
[root@localhost opt]# ll -d dir11
drwxrwx--- 2 root root 19 Nov 22 22:51 dir11
[root@localhost opt]# su - lcr
Last login: Mon Nov 22 22:49:05 CST 2021 on pts/0
[lcr@localhost ~]$ cd /opt/dir11
-bash: cd: /opt/dir11: Permission denied
[lcr@localhost ~]$ exit
logout
[root@localhost opt]# chmod o+x dir11
[root@localhost opt]# ll -d dir11
drwxrwx--x 2 root root 19 Nov 22 22:51 dir11
[root@localhost opt]# su - lcr
Last login: Mon Nov 22 22:52:14 CST 2021 on pts/0
[lcr@localhost ~]$ cd /opt/dir11
[lcr@localhost dir11]$
r:
[root@localhost opt]# chmod o=r dir11
[root@localhost opt]# su - lcr
Last login: Mon Nov 22 22:52:57 CST 2021 on pts/0
[lcr@localhost ~]$ ls /opt/dir11
ls: cannot access /opt/dir11/file1: Permission denied
file1
[lcr@localhost ~]$ exit
logout
[root@localhost opt]# chmod o=rx dir11
[root@localhost opt]# su - lcr
Last login: Mon Nov 22 22:54:08 CST 2021 on pts/0
[lcr@localhost ~]$ ls /opt/dir11
file1
[lcr@localhost ~]$ cd /opt/dir11
[lcr@localhost dir11]$ ls
file1
总结:
r:ls
x:cd
w:touch mkdir rm 

2. 更改权限

r4
w2
x1

                u        g       o
r               r         r         -                                     文件 640 
w              w        -         -
x               -         -         -

chmod 权限        文件名 [参数]
            对象类型
rwx---rw-   file1  706  给所属组增加写权限,去掉所有者的写权限,去掉所有人的读权限
chmod  g+w file1
chmod  u-w file1
chmod o+x file1
chmod a-r file1   chmod u-r,g-r,o-r file1

所有者拥有rw权限,所属组拥有x权限,其他人没有权限.
chmod u=rw,g=x,o= a.txt
r=4 x=1 w=2
所有者拥有rw权限,所属组拥有x权限,其他人没有权限. 6=u1=g0=o
770rwxrwx---
650rw-r-x---
124--x-w-r--
431r---wx--x
777rwxrwxrwx

=a. 使用符号

对象 赋值符 权限类型
u + r
chmod g - w file1
o = x
a
[root@xingdian ~]# chmod u+x file1 //属主增加执行
[root@xingdian ~]# chmod u-x file1 //属主去掉执行
[root@xingdian ~]# chmod a=rwx file1 //所有人等于读写执行
[root@xingdian ~]# chmod a=- file1 //所有人没有权限
[root@xingdian ~]# chmod ug=rw,o=r file1 //属主属组等于读写,其他人只读
[root@xingdian ~]# ll file1 //以长模式方式查看文件权限
-rw-rw-r-- 1 alice it 17 10-25 16:45 file1 //显示的结果
案例:
[root@localhost dir1]# ll 
total 0
-rw-r--r-- 1 alice hr 0 Jul  2 02:26 a.txt
[root@localhost dir1]# chmod u+x a.txt
[root@localhost dir1]# ll
total 0
-rwxr--r-- 1 alice hr 0 Jul  2 02:26 a.txt
[root@localhost dir1]# chmod a=rwx a.txt
[root@localhost dir1]# ll
total 0
-rwxrwxrwx 1 alice hr 0 Jul  2 02:26 a.txt
[root@localhost dir1]# chmod o-wx a.txt
[root@localhost dir1]# ll
total 0
-rwxrwxr-- 1 alice hr 0 Jul  2 02:26 a.txt
[root@localhost dir1]# chmod u=r,g=x,o=w a.txt
[root@localhost dir1]# ll
total 0
-r----x-w- 1 alice hr 0 Jul  2 02:26 a.txt

=b. 使用数字

[root@localhost ~]# chmod 644 file1
[root@localhost ~]# ll file1
-rw-r--r-- 1 alice it 17 10-25 16:45 file1

创建文件时默认权限是644  (umask) rw-r--r--
创建目录时默认权限是755                rwxr-xr-x

ugo 对象
rwx 权限 421
chmod chown chgrp 

3.高级权限

777
suid4
sgid2
sticky1

一:高级权限的类型

suid 4 普通用户可以通过suid权限进行提权 file(二进制文件)
sgid 2 组继承权限  dir1 属组hr  目录下的内容都会继承改组的权限 
sticky 1 防止其他人的误删除  dir2(alice,xingdian,zhansan)

a.txt u g o 

二:设置特殊权限

a、字符
添加权限

chmod u+s file
chmod g+s dir
chmod o+t dir
取消权限
chmod u-s file
chmod g-s dir
chmod 0-t  dir
b、数字
chmod 4777 file
chmod 2770 dir
chmod 1770 dir

示例1:suid 普通用户通过suid提权 <针对文件>
在进程文件(二进制,可执行)上增加suid权限
[lijin@xingdian-server-11 ~]$ cat /etc/shadow
cat: /etc/shadow: Permission denied
解决方案:
[root@xingdian-server-11 /]# which cat 查看cat这个命令对应二进制(可执行)文件的位置
[root@xingdian ~]# chmod u+s /usr/bin/cat
[lijin@xingdian-server-11 ~]$ cat /etc/shadow

作用: 
        suid只能给命令添加,当给命令添加了suid之后,后面再有人去执行这个命令的时候就会拥有命令操作符所有者的权限

示例2:sticky 用户只能删除自己的文件 <针对目录>
[root@xingdian ~]# mkdir /home/dir1
[root@xingdian ~]# chmod 777 /home/dir1
测试:user1在/home/dir1建立文件, user2尝试删除!
[root@xingdian ~]# chmod o+t /home/dir1
[root@xingdian ~]# ll -d /home/dir1
rwxrwxrwt 2 root root 4096 09-02 02:26 /home/dir1
谁可以删除:
root
文件的所有者
目录的所有者

示例3:sgid 新建文件继承目录属组 <针对目录>
[root@xingdian ~]# mkdir /home/hr
[root@xingdian ~]# chgrp hr /home/hr/
[root@xingdian ~]# chmod g+s /home/hr当我们需要,目录下的文件与目录的所属组相同时
[root@xingdian ~]# ll -d /home/hr/
drwxr-sr-x. 2 root hr 4096 Dec 5 16:03 /home/hr/
[root@xingdian ~]# touch /home/hr/file9
[root@xingdian ~]# ll /home/hr/
-rw-r--r--. 1 root hr 0 Dec 5 16:03 file92

4.权限掩码umask

示例1: 在shell进程中创建文件
[root@xingdian ~]# umask //查看当前用户的umask权限
0022
[root@xingdian ~]# touch file800
[root@xingdian ~]# mkdir dir800
[root@xingdian ~]# ll -d dir800 file800
drwxr-xr-x. 2 root root 4096 3月 11 19:40 dir800
-rw-r--r--. 1 root root 0 3月 11 19:40 file800
示例2:修改shell umask值(临时)
[root@xingdian ~]# umask 000
[root@xingdian ~]# mkdir dir900
[root@xingdian ~]# touch file900
[root@xingdian ~]# ll -d dir900 file900
drwxrwxrwx. 2 root root 4096 3月 11 19:44 dir900
-rw-rw-rw-. 1 root root 0 3月 11 19:44 file900

 主要应用:
  (umask 066; openssl genrsa -out private/cakey.pem 2048)
 ():代表的是当前shell的子shell
 ; :连接多个命令
666 110 110 110  
066 111 001 001 
       110 000 000
       600

为什么unmask掩码为022 创建出来的文件和目录权限分别是644和755???????(文件默认:666 | 目录默认:777 | 普通用户账户默认骀:022)
666->                            110 110 110
022->                            111 101 101
                                      110 100 100  644
777 ->                           111 111 111 
022 -> 000 010 010 -> 111 101 101 
                                      111 101 101  755 
可以根据umask来创建我们想要权限的文件

默认创建出来的文件和目录的权限是多少?
 umask: 666  目录--x--x--x 111    文件--------- 000
 umask: 021  目录rwxr-xrw-766   文件rw-r--rw- 646
 umask: 621  目录--xr-xrw- 156   文件---r--rw- 046

5.文件系统属性(隐藏权限)

文件系统属性(隐藏权限):root
查看 lsattr 文件
chattr +a 文件 -->允许往文件里追加内容
chattr +i 文件 -->只能看,其他的都不能
> 覆盖
>> 追加

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

9527灯塔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值