首页 > 技术文章 > 用户管理

yyyzyyyz 2021-11-18 15:44 原文

用户管理

1 介绍

linux支持多用户,并且Linux系统允许同一时刻多个用户同时登陆,登陆后相互之间操作并不影响。

由于root权限过大,很容易误操作造成系统风险,所以需要新建一些用户,使用普通用户管理服务器。同时,用户还有属组的概念,可以给用户分配到不同的组,使其拥有不同的权限。一个用户可以拥有多个组。

1.1 查看用户信息

id    # 查看当前登录的用户信息
# uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
# uid:用户id,gid:组id,groups:用户属于哪些组,context环境信息

1.2 用户存放位置

Linux系统会将用户的信息存放在/etc/passwd,记录了用户的信息,但没有密码信息,密码被存放在/etc/shadow中。也就是说这两个文件非常的重要,不要轻易删除与修改。

cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin  # nologin不能登录
daemon:x:2:2:daemon:/sbin:/sbin/nologin

image

cat /etc/shadow
bin:*:18353:0:99999:7:::

image

tips:查看系统的所有用户
cat /etc/passwd | grep bash

1.3 系统对用户uid的一个约定用法

用户UID 系统中约定的含义
0 超级管理员,最高权限,有着极强的破坏能力
1~200 系统用户,用来运行系统自带的进程,默认已创建
201~999 系统用户,用来运行用户安装的程序,所以此类用户无需登录系统
1000+ 普通用户,正常可以登陆系统的用户,权限比较小,能执行的任务有限

2 用户的CURD

2.1 新增

useradd bob		# adduser实际上是软链接到useradd
tail -1 /etc/passwd
# bob:x:1000:1000::/home/bob:/bin/bash

# 选项
# -u 指定要创建用户的UID,不允许冲突
# -g 指定要创建用户默认组
# -G 指定要创建用户附加组,逗号隔开可添加多个附加组
# -d 指定要创建用户家目录
# -s 指定要创建用户的bash shell  /bin/bash   /sbin/nologin
# -c 指定要创建用户注释信息
# -M 给创建的用户不创建家目录
# -r 创建系统账户,默认无家目录

#1.创建bgx用户,UID5001,基本组students,附加组sa 注释信息:2019 new student,登陆shell:/bin/bash
groupadd sa			# 创建组
groupadd students	# 创建组
useradd -u 5001 -g students -G sa -c "2019 new student" -s /bin/bash bgx

#2.创建mysql系统用户,-M不建立用户家目录 -s指定nologin使其用户无法登陆系统,创建一个用户只负责运行某个服务
useradd mysql -M -s /sbin/nologin
useradd -r dba -s /sbin/nologin

2.2 修改

usermod
# 选项
# -u 指定要修改用户的UID
# -g 指定要修改用户基本组
# -G 指定要修改用户附加组,使用逗号隔开多个附加组, 覆盖原有的附加组,-aG追加
# -d 指定要修改用户家目录 -md 旧家搬新家
# -s 指定要修改用户的bash shell
# -c 指定要修改用户注释信息
# -l 指定要修改用户的登陆名
# -L 指定要锁定的用户
# -U 指定要解锁的用户

# 修改bgx用户uid、gid,附加组  -a表示追加
groupadd -g 5008 network_sa
groupadd -g 5009 devops
usermod -u 6001 -g5008 -a -G 5009 bgx

2.3 删除

userdel
# 选项 -r 删除用户同时删除它的家目录

#1.删除user1用户,但不删除用户家目录和 mail spool
userdel user1
ls /var/spool/mail/

#2.-r参数可以连同用户家目录一起删除(慎用)
userdel -r user1

当我们想删除某个用户的时候,出现user xxx is currently used by process xxx,可能的原因是你创建用户user1之后,使用su命令切换到user1用户下,之后又想删除user1用户,使用su root切换到root用户下,使用userdel user1。出现上述情况的根本原因在于切换回root用户之后,user1还被某个进程占用。

解决方案:ctrl+d(退出当前用户)

2.4 密码修改

创建用户之后,如需要使用该用户登陆系统则需要为用户设定密码,设定密码使用passwd命令。建议密码复杂度一些,不要出现生日、人名等等。

  • 普通用户只允许变更自己的密码,无法修改其他人密码,并且密码长度必须8位字符
  • 管理员用户允许修改任何人的密码,无论密码长度多长或多短
# 使用passwd命令修改用户密码
passwd        # 给当前用户修改密码
passwd root   # 给root用户修改密码
passwd bob 	  # 给bob修改密码,普通用户只能修改自己的密码

3 用户组

3.1 新增组

#新增组
groupadd group1

3.2 修改组

# 选项 -g:修改组gid
groupmod -g 1111 group1

# 选项 -n:修改组名(把group1名字改为active_group)
groupmod group1 -n active_group 

3.3 删除组

删除时需要注意,如果用户存在基本组则无法直接删除该组,如果删除用户则会移除默认的私有组,而不会移除基本组。

# 删除组
groupdel active_group

# 无法删除用户基本组
groupdel network_sa
groupdel: cannot remove the primary group of user 'bgx_bob'
#只有删除用户或者用户变更基本后,方可删除该组

4 提权

生产环境中一般都是禁止root用户直接登录操作系统,通常使用的都是普通用户,当我们使用普通用户执行/sbin目录下的命令时没有权限运行,这种情况下我们无法正常的管理服务器,那如何才能不使用root用户直接登录系统,同时又保证普通用户能完成日常工作?

我们可以使用如下两种方式: su、sudo

  • su:切换用户,使用普通用户登录,然后使用su命令切换到root。优点:简单;缺点:需要知道root密码
  • sudo:提权,当需要使用root权限时进行提权,而无需切换至root用户,优点:安全;缺点:复杂

4.1 su

普通用户使用su -可以直接切换至root用户,但需要输入root用户的密码。
root用户使用su - username切换普通用户不需要输入密码。

su bob	# 表示不打开新的bash窗口,登录在当前目录,不加载环境变量
su - bob	# 打开新的bash窗口,登录在bob的家目录,加载环境变量

4.2 sudo

su命令在切换root用户时,都需要输入root密码,这样系统会变得非常不安全。sudo是linux系统管理指令,是允许系统管理员让普通用户执行一些或者全部的root命令的一个工具。sudo使一般用户不需要知道超级用户的密码即可获得权限。

首先超级用户将普通用户的名字、可以执行的特定命令、按照哪种用户或用户组的身份执行等信息,登记在特殊的文件中,在一般用户需要取得特殊权限时,其可在命令前加上sudo,此时sudo将会询问该用户自己的密码,回答后系统即会将该命令的进程以超级用户的权限运行。

# 1.快速配置sudo方式
[root@localhost ~]# usermod mike -G wheel # 把mike加到wheel组中

# 2.切换到mike用户
[root@localhost ~]# su - mike

#2.检查普通用户能提权的命令
[mike@localhost ~]$ sudo -l
User mike may run the following commands on this host:
    (ALL) ALL
用户 mike 可以在 localhost 上运行以下命令:
    (ALL) ALL

#3.普通用户无法在media下创建文件
[mike@localhost home]$ cd /media/
[mike@localhost media]$ touch a.txt
touch: 无法创建"a.txt": 权限不够

#4.使用sudo提权,需要输入mike自己的密码
[mike@localhost media]$ sudo touch a.txt
[mike@localhost media]$ ls
a.txt

这样做的缺点是提升的权限太大,有时只需要给普通用户几个权限就够了。前面提到过超级用户首先将授权信息登记在特殊的文件中,这个文件一般是/etc/sudoers,可以在这里配置。下面是该文件的内容:

## Sudoers allows particular users to run various commands as
## the root user, without needing the root password.
## 该文件允许特定用户像root用户一样使用各种各样的命令,而不需要root用户的密码 
## Examples are provided at the bottom of the file for collections
## of related commands, which can then be delegated out to particular
## users or groups.
## 在文件的底部提供了很多相关命令的示例以供选择,这些示例都可以被特定用户或用户组所使用  
## This file must be edited with the 'visudo' command.
## 该文件必须使用"visudo"命令编辑

## Host Aliases
## 主机别名
## Groups of machines. You may prefer to use hostnames (perhaps using 
## wildcards for entire domains) or IP addresses instead.
## 对于一组服务器,你可能会更喜欢使用主机名(可能是全域名的通配符)
## 或IP地址代替,这时可以配置主机别名
# Host_Alias     FILESERVERS = fs1, fs2
# Host_Alias     MAILSERVERS = smtp, smtp2

## User Aliases
## 用户别名
## These aren't often necessary, as you can use regular groups
## (ie, from files, LDAP, NIS, etc) in this file - just use %groupname 
## rather than USERALIAS
## 这并不很常用,因为你可以通过使用组来代替一组用户的别名
# User_Alias ADMINS = jsmith, mikem


## Command Aliases
## These are groups of related commands...
## 指定一系列相互关联的命令(当然可以是一个)的别名,通过赋予该别名sudo权限,  
## 可以通过sudo调用所有别名包含的命令,下面是一些示例

## Networking
## 网络操作相关命令别名
# Cmnd_Alias NETWORKING = /sbin/route, /sbin/ifconfig, /bin/ping, /sbin/dhclient, /usr/bin/net, /sbin/iptables, /usr/bin/rfcomm, /usr/bin/wvdial, /sbin/iwconfig, /sbin/mii-tool

## Installation and management of software
## 软件安装管理相关命令别名  
# Cmnd_Alias SOFTWARE = /bin/rpm, /usr/bin/up2date, /usr/bin/yum

## Services
## 服务相关命令别名 
# Cmnd_Alias SERVICES = /sbin/service, /sbin/chkconfig, /usr/bin/systemctl start, /usr/bin/systemctl stop, /usr/bin/systemctl reload, /usr/bin/systemctl restart, /usr/bin/systemctl status, /usr/bin/systemctl enable, /usr/bin/systemctl disable

## Updating the locate database
## 本地数据库升级命令别名
# Cmnd_Alias LOCATE = /usr/bin/updatedb

## Storage
## 磁盘操作相关命令别名
# Cmnd_Alias STORAGE = /sbin/fdisk, /sbin/sfdisk, /sbin/parted, /sbin/partprobe, /bin/mount, /bin/umount

## Delegating permissions
## 代理权限相关命令别名
# Cmnd_Alias DELEGATING = /usr/sbin/visudo, /bin/chown, /bin/chmod, /bin/chgrp 

## Processes
## 进程相关命令别名
# Cmnd_Alias PROCESSES = /bin/nice, /bin/kill, /usr/bin/kill, /usr/bin/killall

## Drivers
## 驱动命令别名
# Cmnd_Alias DRIVERS = /sbin/modprobe

# Defaults specification
# 环境变量的相关配置

#
# Refuse to run if unable to disable echo on the tty.
#
Defaults   !visiblepw

#
# Preserving HOME has security implications since many programs
# use it when searching for configuration files. Note that HOME
# is already set when the the env_reset option is enabled, so
# this option is only effective for configurations where either
# env_reset is disabled or HOME is present in the env_keep list.
#
Defaults    always_set_home
Defaults    match_group_by_gid

# Prior to version 1.8.15, groups listed in sudoers that were not
# found in the system group database were passed to the group
# plugin, if any. Starting with 1.8.15, only groups of the form
# %:group are resolved via the group plugin by default.
# We enable always_query_group_plugin to restore old behavior.
# Disable this option for new behavior.
Defaults    always_query_group_plugin

Defaults    env_reset
Defaults    env_keep =  "COLORS DISPLAY HOSTNAME HISTSIZE KDEDIR LS_COLORS"
Defaults    env_keep += "MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE"
Defaults    env_keep += "LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES"
Defaults    env_keep += "LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE"
Defaults    env_keep += "LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY"

#
# Adding HOME to env_keep may enable a user to run unrestricted
# commands via sudo.
#
# Defaults   env_keep += "HOME"

Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin

## Next comes the main part: which users can run what software on 
## which machines (the sudoers file can be shared between multiple
## systems).
## 下面是主要部分:什么用户在什么机器上上可以执行什么命令(sudoers文件可以在多个机器上共享)
## Syntax:
## 语法:
##
## 	user	MACHINE=COMMANDS
##  用户    登录的主机=可以使用sudo执行的命令
##
## The COMMANDS section may have other options added to it.
##  命令部分可以附带一些其它的选项
##
## Allow root to run any commands anywhere 
##  允许root用户在任意主机下执行任意命令 
root	ALL=(ALL) 	ALL

## Allows members of the 'sys' group to run networking, software, 
## service management apps and more.
# %sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS
## 允许sys中户组中的用户使用NETWORKING等所有别名中配置的命令

## Allows people in group wheel to run all commands
## 允许wheel用户组中的用户在任意位置执行所有命令
%wheel	ALL=(ALL)	ALL

## Same thing without a password
## 允许wheel用户组中的用户在不输入该用户的密码的情况下使用所有命令
# %wheel	ALL=(ALL)	NOPASSWD: ALL

## Allows members of the users group to mount and unmount the 
## cdrom as root
## 允许users用户组中的用户像root用户一样使用mount、unmount、chrom命令 
# %users  ALL=/sbin/mount /mnt/cdrom, /sbin/umount /mnt/cdrom

## Allows members of the users group to shutdown this system
## 允许users用户组中的用户像root用户一样使用shutdown命令
# %users  localhost=/sbin/shutdown -h now

## Read drop-in files from /etc/sudoers.d (the # here does not mean a comment)
#includedir /etc/sudoers.d

这个文件很长,但主要的部分很简单,我们只需要按照范例进行修改即可。但是并不推荐直接修改这个文件,而是使用visudo命令编辑。你只需要copy一行,然后把“root”改成你想要添加的那个用户名就可以了。使用visudo的好处就是它会帮你自动进行语法检查,加锁防止两个管理员同时修改这个文件。所以,强烈建议使用visudo

第一种方式:

# 1.使用sudo定义分组,这个系统group没什么关系
User_Alias OPS = ldd,zqs,xhl
User_Alias DEV = bob,mike

# 2.定义可执行的命令组,便于后续调用
Cmnd_Alias NETWORKING = /sbin/ifconfig, /bin/ping
Cmnd_Alias SOFTWARE = /bin/rpm, /usr/bin/yum
Cmnd_Alias SERVICES = /sbin/service, /usr/bin/systemctl start
Cmnd_Alias STORAGE = /bin/mount, /bin/umount
Cmnd_Alias DELEGATING = /bin/chown, /bin/chmod, /bin/chgrp
Cmnd_Alias PROCESSES = /bin/nice, /bin/kill, /usr/bin/kill, /usr/bin/killall

# 3.使用sudo开始分配权限,用于分配OPS组的人可以执行什么,DEV组的人可以执行什么
OPS  ALL=(ALL) NETWORKING,SOFTWARE,SERVICES,STORAGE,DELEGATING,PROCESSES
DEV  ALL=(ALL) SOFTWARE,PROCESSES

第二种方式:使用groupadd添加组,然后给组分配sudo的权限,如果有新用户加入,直接将用户添加到该组。

#1.添加两个真实的系统组,  group_dev group_op
groupadd group_dev
groupadd group_op

#2.添加两个用户,group_dev(user_a  user_b)   group_op(user_c  user_d)
useradd user_a -G group_dev
useradd user_b -G group_dev
useradd user_c -G group_op
useradd user_d -G group_op

#3.添加密码
echo "1" | passwd --stdin user_a
echo "1" | passwd --stdin user_b
echo "1" | passwd --stdin user_c
echo "1" | passwd --stdin user_d

#4.在sudo中配置规则
[root@localhost ~]# visudo
    Cmnd_Alias NETWORKING = /sbin/ifconfig, /bin/ping
    Cmnd_Alias SOFTWARE = /bin/rpm, /usr/bin/yum
    Cmnd_Alias SERVICES = /sbin/service, /usr/bin/systemctl start
    Cmnd_Alias STORAGE = /bin/mount, /bin/umount
    Cmnd_Alias DELEGATING = /bin/chown, /bin/chmod, /bin/chgrp
    Cmnd_Alias PROCESSES = /bin/nice, /bin/kill, /usr/bin/kill, /usr/bin/killall

    %group_dev ALL=(ALL) SOFTWARE
    %group_op ALL=(ALL) SOFTWARE,PROCESSES

#5.检查sudo是否配置有错
[root@localhost ~]# visudo -c
/etc/sudoers: parsed OK

最后了解一下sudo的执行过程

image

推荐阅读