Linux学习整理-用户权限相关命令

1. su

su 是substitute user identity的缩写,但是大家常用的是它的switch user的功能,所以也可以看作是switch user的缩写。

Usage:
 su [options] [-] [USER [arg]...]

1-1. 属性 -m,-p

su 加上 -m或者-p属性,不会reset环境变量,像HOME,LOGNAME一类的。

[root@centos cmdtest]# echo $USER $HOME $LOGNAME
root /root root

先看一下不加属性-m,-p的结果,发现环境变量全部被设置成登录用户的内容。

[root@centos cmdtest]# su lihg
[lihg@centos cmdtest]$ echo $USER $HOME $LOGNAME
lihg /home/lihg lihg

看一下加上-m,-p的结果。

[root@centos cmdtest]# su -m lihg
bash: /root/.bashrc: Permission denied
bash-4.2$ echo $USER $HOME $LOGNAME
root /root root

可以看到切换用户后,这些环境变量没有变化。

1-2. 属性 -l ★(常用命令)

su [user] :user省略的时候,是切换到root用户。
su后不加 --l--login 工作目录不会切换到家目录

[lihg@centos ~]$ pwd
/home/lihg
[lihg@centos ~]$ su
Password: 
[root@centos lihg]# pwd
/home/lihg

su - [user]:user省略的时候,切换到root用户。
并且工作目录会切换到切换用户的家目录
su -lsu --login都一样。

[lihg@centos ~]$ pwd
/home/lihg
[lihg@centos ~]$ su -l maff
Password: 
Last login: Sun Mar 13 00:57:50 JST 2022 on pts/3
-sh-4.2$ pwd
/home/maff
-sh-4.2$ 

1-3. 属性 -s

关于这个属性,有个执行顺序问题,按照下面的级别
1 属性-s 指定的shell
2 如果指定属性-m,-p,就用前面用户环境变量里设定的SHELL
3 目标用户在/etc/passwd里定义的shell
4 /bin/sh

[root@centos cmdtest]# usermod -s /usr/bin/bash maff
[root@centos cmdtest]# echo $SHELL
/bin/bash
[root@centos cmdtest]# su - maff
Last login: Sun Mar 13 20:38:30 JST 2022 on pts/3
[maff@centos ~]$ echo $SHELL
/usr/bin/bash

[root@centos cmdtest]# grep maff /etc/passwd
maff:x:1001:1001::/home/maff:/usr/bin/bash

什么参数都不指定,用的是/etc/passwd里定义的shell

[root@centos cmdtest]# su -s /bin/sh maff
sh-4.2$ 
sh-4.2$ echo $SHELL
/bin/sh
sh-4.2$ 

指定shell的情况下用的是指定的shell

[root@centos cmdtest]# su -m maff
bash: /root/.bashrc: Permission denied
bash-4.2$ echo $SHELL
/bin/bash
bash-4.2$ 

指定属性-m的情况下,用切换前用户的环境变量SHELL

1-4. 属性 -c

指定用户执行command

[root@centos cmdtest]# su -c "mkdir sutestfolder" maff
[root@centos cmdtest]# 
[root@centos cmdtest]# ll
total 12
-rw-r--r--. 1 root root 10 Mar 12 10:32 a.txt
-rw-r--r--. 2 root root  4 Mar 12 09:45 li.log
lrwxrwxrwx. 1 root root  6 Mar 12 09:45 ma.log -> li.log
-rw-r--r--. 2 root root  4 Mar 12 09:45 my.log
drwxrwxr-x. 2 root root  6 Mar 13 00:42 sutest
drwxr-xr-x. 2 maff maff  6 Mar 13 21:14 sutestfolder
[root@centos cmdtest]# 

新建的文件夹是属于执行用户maff的。

1-5. 属性 --session-command

指定用户执行command,但是不会创建新的会话(session)

[root@centos cmdtest]# ps -o sid
   SID
  3077
  3077
[root@centos cmdtest]# su -c "ps -o sid" maff
   SID
  5240
[root@centos cmdtest]# su --session-command="ps -o sid" maff
   SID
  3077
[root@centos cmdtest]# 

-c,发现sid变成一个新的值,而用--session-commandsid不变。

2. sudo

sudo 是substitute user do的缩写,但是大家普遍认为是superuser do的缩写,就是用root权限去执行command。实际上它的功能不只这么简单。

2-1. sudo [command] ★(常用命令)

[lihg@centos ~]$ sudo ls
[sudo] password for lihg: 
lihg is not in the sudoers file.  This incident will be reported.
[lihg@centos ~]$ 

提示该用户不在sudoers文件里,这里要用root权限,查看一下/etc/sudoers
发现需要用户在root或者wheel组里才可以。

[root@centos cmdtest]# cat /etc/sudoers 

在这里插入图片描述

groupmems把用户lihg添加到组wheel里

[root@centos cmdtest]# groupmems -g wheel -a lihg
[root@centos cmdtest]# groupmems -g wheel -l
lihg 
[root@centos cmdtest]# id lihg
uid=1000(lihg) gid=1000(lihg) groups=1000(lihg),10(wheel),995(nginx)
[root@centos cmdtest]# 

现在再用sudo

[lihg@centos ~]$ sudo ls
[sudo] password for lihg: 
test.txt
[lihg@centos ~]$ 

2-2. visudo

现在用sudo还需要输入当前用户的密码,可以修改/etc/sudoers 来不输入密码就可以执行sudo。用visudo命令来修改

106 ## Allows people in group wheel to run all commands
107 # %wheel        ALL=(ALL)       ALL
108 
109 ## Same thing without a password
110  %wheel ALL=(ALL)       NOPASSWD: ALL

修改完配置以后

[lihg@centos ~]$ sudo ls
test.txt
[lihg@centos ~]$ 

还可以限制sudo的权限,只让执行特定的命令
比如nginx组下的用户可以用sudo 执行ls命令

 %nginx ALL=/usr/bin/ls
[maff@centos cmdtest]$ id maff
uid=1001(maff) gid=1001(maff) groups=1001(maff),995(nginx)
[maff@centos cmdtest]$ sudo ls
a.txt  li.log  ma.log  my.log  sutest  sutestfolder
[maff@centos cmdtest]$ sudo ps
Sorry, user maff is not allowed to execute '/bin/ps' as root on centos.
[maff@centos cmdtest]$ 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值