0
点赞
收藏
分享

微信扫一扫

用户组管理及用户提权

用户组

## 相关文件
/etc/group
[root@localhost ~]# cat /etc/group
root:x:0:
# 1.组名字
# 2.组密码占位符
# 3.GID
# 4.显示该组的附加成员

/etc/gshadow
root:::jyj5
# 1.组名字
# 2.组密码(空和!是没有密码)
# 3.组管理员
# 4.显示该组的附加成员

## 相关命令
# 增加组
groupadd [选项] 组名

## 选项
-g:指定组的gid
-r:指定gid范围201-999之间的系统组

#举例
[root@localhost ~]# groupadd test100
[root@localhost ~]# id test100
id: test100: no such user
[root@localhost ~]# tail -1 /etc/passwd
jyj001:x:1009:1009::/home/jyj001:/bin/bash
[root@localhost ~]# tail -1 /etc/group
test100:x:1010:

-g
[root@localhost ~]# groupadd test200 -g 10010
[root@localhost ~]# tail -1 /etc/group
test200:x:10010:

-r
[root@localhost ~]# groupadd test300 -r
[root@localhost ~]# tail -1 /etc/group
test300:x:995

# 删
[root@localhost ~]# groupdel test100

# 改
groupmod
-g:修改组的gid
-n:修改组名字

[root@localhost ~]# groupmod -g 10010 test200
[root@localhost ~]# tail -2 /etc/group
test200:x:10010:
test300:x:995:
[root@localhost ~]# groupmod -n test400 test300
[root@localhost ~]# tail -2 /etc/group
test200:x:10010:
test400:x:995:
[root@localhost ~]# groupmod test400 -n test666
[root@localhost ~]# tail -2 /etc/group
test200:x:10010:
test666:x:995:

# 查
[root@localhost ~]# cat /etc/group

用户身份切换

Linux系统中,有时候普通用户有些事情是没办法操作,除非是root管理员用户才能做到。这时就需要临时切换到root管理员身份来做事了。那么在学习如何切换用户之前,我们先来了解下用户工作环境。

如何在普通用户的情况下,完成日常工作?

1)su 切换用户,使用普通用户登录,然后使用su命令切换到root。

优点:简单,方便

缺点:需要知道root密码,不安全,切换到root没有日志审计功能

2)sudo 提权,当需要使用root权限时,进行提权,而无需切换至root用户。

优点:安全,方便

缺点:复杂

su命令前戏

shell的种类

  • 交互式shell
  • 非交互式shell
  • 登录式shell
  • 需要输入用户名和密码,才可以登录
  • 非登录式shell
  • 不需要输入用户名和密码,就可以登录

系统的环境变量文件

## 局部环境变量
~/.bashrc
~/.bash_profile
## 全局环境变量
/etc/profile
/etc/profile.d/*.sh
/etc/bashrc
##加载顺序
/etc/profile
/etc/profile.d/*.sh
~/.bash_profile
~/.bashrc
/etc/bashrc

#bash,exit
非登录式shell,只加载三个环境变量
~/.bashrc
/etc/profile.d/*.sh
/etc/bashrc

##su su -
su - 会加载所有环境变量
su只加载一部分

用户身份提权

sudo命令提权

  • 什么是sudo

sudo就是普通用户可以提权,执行root用户可以执行的命令

  • 为什么要用到sudo

如果在公司中,入职后,领导给运维的用户是普通用户,但是有些命令只能root执行

sudo如何使用

# 1.系统的超级管理员(root)需要做sudo的配置(发一个兵符给指定的普通用户)
# 2.普通用户只需要在,执行的命令之前,加上sudo即可

sudo的配置(root发兵符的过程

# 没有sudo命令,则需要安装
[root@localhost ~]# yum install -y sudo

# sudo的配置文件:
[root@localhost ~]$ vim /etc/sudoers(不推荐,只读文件)

# 用户名 所有主机=(所有角色) 所有命令的执行权限
root ALL=(ALL) ALL
jyj 10.0.0.100=(ALL) ALL
zls ALL=(ALL) /bin/cp
#普通用户也不需要输入自己的密码
zls ALL=(ALL) NOPASSWD:ALL

## 推荐修改sudo的方式
[root@localhost ~]$ visudo
-c:检查sudoers文件的语法

## 免密切换到root用户,就算不免密,也是输入zls用户的密码,切换到root用户
[zls@localhost ~]$ sudo su -

## 报错
[root@localhost ~]# sudo ll
sudo: ll: command not found
原因:ll是别名,不是系统命令,sudo不走别名,只认识系统命令

## 普通用户以root身份执行命令
[zls@localhost ~]$ mkdir /root/zls_sudo
mkdir: cannot create directory ‘/root/zls_sudo’: Permission denied
[zls@localhost ~]$ ls-l /root/
ls: cannot open directory /root/: Permission denied
[zls@localhost ~]$ sudols-l /root/
total 0
drwxr-xr-x 2 root root 6 Apr 809:06 zls
[zls@localhost ~]$ sudomkdir /root/zls_sudo
[zls@localhost ~]$ sudols-l /root/
total 0
drwxr-xr-x 2 root root 6 Apr 809:06 zls
drwxr-xr-x 2 root root 6 Apr 810:19 zls_sud

## sudoers其他别名配置
Host_Alias FILESERVERS = localhost, web01
Cmnd_Alias ZLSCMD = /bin/cp,/bin/mv
Cmnd_Alias ZLSUNCMD =!/bin/rm,!/bin/su(!禁止使用)

Cmnd_Alias:命令别名
Host_Alias:主机别名
User_Alias:角色别名


## 给组发兵符
%test666 ALL=(ALL) NOPASSWD:ALL(%代表后面的是组名)
usermod 用户名-G提权组

# 1.会修改visudo,添加用户提权
# 2.给用户免密执行sudo的权限
# 3.自定义用户的可执行命令,和不可执行命令
# 4.给组分配提权的权限
# 5.提权不用修改visudo,只需要加入wheel组,即可-G wheel

注意:除非企业中有要求,哪些命令需要用,哪些不能使用
举报

相关推荐

0 条评论