Linux基础 权限管理

上节回顾,linux用户基本操作

如何查看哪些用户已经登录在linux服务器上了

w

[root@changmiao shell-test]# w

12:48:07 up 20:50, 2 users, load average: 0.00, 0.01, 0.05 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root tty1 五15 20:49m 0.01s 0.01s -bash root pts/0 192.168.37.1 五16 7.00s 0.15s 0.01s w

TTY

pts --远程终端(远程登录上来的),理论上来说可以有很多个

[root@changmiao shell-test]# ps -ef |grep pts root 1598 1081 0 11月24 ? 00:00:01 sshd: root@pts/0 root 1602 1598 0 11月24 pts/0 00:00:00 -bash root 2362 1602 0 11月24 pts/0 00:00:00 bash root 26082 2362 0 20:45 pts/0 00:00:00 ps -ef root 26083 2362 0 20:45 pts/0 00:00:00 grep --color=auto pts

tty1~6 直接登录的终端,6个。

使用alt + F1 ~F6 切换

linux下一切皆文件,终端设备文件 --dev

From 从哪里登录

如何知道哪些用户曾经登陆过linux系统?

last 记录最近登录过系统的用户

lastlog 判断系统里面哪些用户登录过,以及最近的登录时间

如何给用户重新设置密码?

password命令

linux root密码忘记了怎么办?

重启,进入单用户模式修改密码

如何禁用用户登录?

usermod -L

passwd -L

登录shell信息可以修改为 /sbin/nologin

删除用户

怎么将普通用户改成root用户?

uid - 0

*********

*********

LInux 权限管理

用户和组 -- 方便对资源的管控 安全

1.针对文件或文件夹

访问权限

读(read) : 允许查看文件内容,显示目录列表 --4

写(write) :允许修改文件内容,允许在目录中新建、移动、删除文件或子目录 --2

执行(execute):允许运行程序文件,切换目录 --5

归属权限

属主: 该文件或目录的拥有者 --》 user --》u

属组: 该文件或目录的组账号 --》 group --》g

其他人: 除了属主和属组之外的其他人 --》 others --》o

[root@changmiao opt]# ll 总用量 8 -rw-------. 1 root root 1253 10月 20 14:53 anaconda-ks.cfg drwxr-xr-x. 2 root root 6 10月 20 14:58 city -rw-r--r--. 1 root root 0 10月 20 15:53 sc-2.txt.back -rw-r--r--. 1 root root 0 10月 20 15:04 sc-back.txt drwxr-xr-x. 2 root root 25 11月 24 20:23 shell-test -rw-r--r--. 1 root root 28 9月 27 09:58 tianyantiange.txt -rw-r--r--. 1 root root 0 10月 20 15:58 wcm.txt

第一列:

第一个字符: 文件类型

- 代表普通文件类型

d 代表文件夹-- 目录 directory

l 表示链接文件 link

b 代表块设备文件 block 磁盘

c 字符设备文件 tty

s 表示socket文件

p 表示管道文件

第2~4个字符:属主的权限

第5~7个字符:属组的权限

第8~10个字符:其他人的权限

第三列: 属主

第四列:属组

普通用户只能在家目录或者是/tmp

chmod o+w sc3

chmod (对象(属主/属组/其他人)) + (权限(r/w/x)) (元素(文件或文件夹))

chmod -R o+w sc3

-R 递归修改,修改目标文件夹及其文件夹内的文件和文件夹,不单单只修改文件夹

数字赋予权限

chmod 775 sc3

其中 r(读)代表读权限 对应数字4

w(写)代表写权限 对应数字2

x(执行)代表执行权限 对应数字1

#使用数字 --》确定权限

#r --4 ,w --2 ,x --1

chmod  775   sc3
ls  -ld  sc3
drwxrwxr-x.  3 root  root  39  10月  13  11:53  sc3

chown -- 修改属主、属组(必须root用户才有权限执行该命令)

修改格式:

chown 属主 文件

chown :属组 文件

chown 属主:属组 文件

递归修改子文件或目录:

-R 选项

[root@changmiao shell-test]# chown root:root  user_add.sh
[root@changmiao shell-test]# ls -la
总用量 4
drwxr-xr-x. 2 root root  25 11月 24 20:23 .
drwxr-xr-x. 4 root root 139 12月  8 20:57 ..
-rw-r--r--. 1 root root 184 11月 24 20:23 user_add.sh

##########文件掩码

规定文件初始权限

[root@changmiao shell-test]# umask 0022

系统里面 文件夹初始权限 -- 777

文件初始权限 -- 666

[root@changmiao shell-test]# mkdir ab
[root@changmiao shell-test]# ls  -dl ab
drwxr-xr-x. 2 root root 6 12月 15 20:17 ab

临时修改智能作用于本次,后续重新登录无效

[root@changmiao shell-test]#umask 002

针对个人用户,想永久更改umask,可以写入家目录下的.bashrc或者.bash——profile文件

#########################

设置隐藏权限 chattr

a 追加 --append 文件不能修改,不能删除,不能移动,只能追加(在文件末尾增加内容)

i 不能变 不能修改,不能删除,不能追加,不能移动

chattr 设置隐藏权限

lsattr 查看隐藏权限

[root@changmiao shell-test]# chattr  +ai  test,sh 
[root@changmiao shell-test]# lsattr test,sh 
----ia---------- test,sh
[root@changmiao shell-test]# chattr  -ai  test,sh 
[root@changmiao shell-test]# lsattr test,sh 

##############
#suid权限 --针对可执行文件

suid权限:当某个用户执行某个具有suid权限命令的时候,会以这个命令的拥有者身份执行

[root@changmiao shell-test]# ls  -al  /usr/bin/mkdir
-rwxr-xr-x. 1 root root 79768 8月  20 2019 /usr/bin/mkdir
[root@changmiao shell-test]# chmod  u+s  /usr/bin/mkdir
[root@changmiao shell-test]# ls  -al  /usr/bin/mkdir
-rwsr-xr-x. 1 root root 79768 8月  20 2019 /usr/bin/mkdir
[root@changmiao shell-test]# su - alice
上一次登录:五 12月 15 20:27:02 CST 2023pts/1 上
[alice@changmiao ~]$ cd  /opt
[alice@changmiao opt]$ ls -ld /opt
drwxr-xr-x. 6 root root 162 12月 14 20:26 /opt
[alice@changmiao opt]$ mkdir  aa
[alice@changmiao opt]$ ls
aa               city  sc-2.txt.back  sc-back.txt  tianyantiange.txt
anaconda-ks.cfg  perm  sc3            shell-test   wcm.txt
​
[root@changmiao opt]# ls  -ld aa
d-wx-wxr-x. 2 root alice 6 12月 15 21:06 aa

尚方宝剑

##########粘滞位权限(sticky)

主要用途:

为公共目录设置权限,权限字符为’t'

用户不能删除该目录中其他用户的文件

1、目录需要先设置为权限为777

2、添加+t 特别权限

[root@changmiao tmp]# touch  sc1
[root@changmiao tmp]# pwd
/opt/tmp
[root@changmiao tmp]# ls
sc1
[root@changmiao tmp]# ls  -al
总用量 0
drwxrwxrwt. 2 root root  17 12月 15 21:27 .
drwxr-xr-x. 8 root root 183 12月 15 21:23 ..
-rw-r--r--. 1 root root   0 12月 15 21:27 sc1
[root@changmiao tmp]# su - alice 
上一次登录:五 12月 15 21:26:49 CST 2023pts/1 上
[alice@changmiao ~]$ cd /opt/tmp
[alice@changmiao tmp]$ ls  -ld  /opt
drwxr-xr-x. 8 root root 183 12月 15 21:23 /opt
[alice@changmiao tmp]$ ls  -ld  /opt/tmp
drwxrwxrwt. 2 root root 17 12月 15 21:27 /opt/tmp
[alice@changmiao tmp]$ touch alice.txt
[alice@changmiao tmp]$ ls -al
总用量 0
drwxrwxrwt. 2 root  root   34 12月 15 21:29 .
drwxr-xr-x. 8 root  root  183 12月 15 21:23 ..
--w--w-r--. 1 alice alice   0 12月 15 21:29 alice.txt
-rw-r--r--. 1 root  root    0 12月 15 21:27 sc1
[alice@changmiao tmp]$ su - wanchangmiao
密码:
上一次登录:五 11月 24 19:18:11 CST 2023pts/0 上
.bashrc 进入新bash时执行
.bash_profile 登录时执行...
[wanchangmiao@changmiao ~]$ cd /opt/tmp
[wanchangmiao@changmiao tmp]$ ls
alice.txt  sc1
[wanchangmiao@changmiao tmp]$ touch  wangyong.txt
[wanchangmiao@changmiao tmp]$ ls -al
总用量 0
drwxrwxrwt. 2 root         root          54 12月 15 21:31 .
drwxr-xr-x. 8 root         root         183 12月 15 21:23 ..
--w--w-r--. 1 alice        alice          0 12月 15 21:29 alice.txt
-rw-r--r--. 1 root         root           0 12月 15 21:27 sc1
-rw-rw-r--. 1 wanchangmiao wanchangmiao   0 12月 15 21:31 wangyong.txt
[wanchangmiao@changmiao tmp]$ rm  -rf alice.txt
rm: 无法删除"alice.txt": 不允许的操作
[wanchangmiao@changmiao tmp]$ vim  alice.txt 
​
[1]+  已停止               vim alice.txt

#使用数字来设置

#suid --》 4 --》针对可执行文件

#guid --》 2

#sticky --》 1 --》针对公共目录

[root@changmiao ~]# cd  /opt/tmp
[root@changmiao tmp]# ls
alice.txt  num-test  sc1  wangyong.txt
[root@changmiao tmp]# chmod  1777  num-test/
[root@changmiao tmp]# ls  -ld num-test/
drwxrwxrwt. 2 root wanchangmiao 6 12月 15 22:04 num-test/
[root@changmiao tmp]# ls  /usr/bin/mkdir -al
-rwsr-xr-x. 1 root root 79768 8月  20 2019 /usr/bin/mkdir
[root@changmiao tmp]# chmod  u-s  /usr/bin/mk
mkdir     mkfifo    mkinitrd  mknod     mktemp    
[root@changmiao tmp]# chmod  u-s  /usr/bin/mkdir 
[root@changmiao tmp]# ls  /usr/bin/mkdir -al
-rwxr-xr-x. 1 root root 79768 8月  20 2019 /usr/bin/mkdir
[root@changmiao tmp]# chmod  4755  /usr/bin/mkdir
[root@changmiao tmp]# ls  /usr/bin/mkdir -al
-rwsr-xr-x. 1 root root 79768 8月  20 2019 /usr/bin/mkdir
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值