Linux-用户管理

概览

Linux是一个支持多用户同时使用的操作系统,事实上我们使用一个用户,比如root用户就可以同时登录到一个系统里,在Linux中很多服务(软件)都是用专门的用户运行,区分不同的权限以确保安全

重要配置文件介绍

在Linux中,创建,删除用户,实际上都是修改系统中指定的文件,比较重要的4个文件是

/etc/passwd   存储用户信息
/etc/shadow   存储用户密码信息

/etc/group    用户组信息文件
/etc/gshadow  用户组密码信息文件


[root@node1 ~]# head -2 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
这里一共有7列,含义分别是 
username password uid gid 备注 用户家目录 该用户使用的shell解释器
我们可以直接修改这个文件的内容来创建用户,但是不推荐这样,有专门的命令来创建各种符合要求的用户
username是给人类识别用的,系统实际判断用户的权限用的是uid,可以用 id username 命令查看,如果我们遇到文件的权限与我们预期不符,不妨检查一下这个文件对应用户的uid是否符合预期
一般创建用户的时候,我们给的是username,系统会自动分配uid,如果是自己手动指定uid,建议大于1000,小于1000的uid可能会被系统预分配的服务使用
系统中可用的shell解释器可以在 cat /etc/shells 看到,如果shell解释器是 /sbin/nologin 代表该用户不能直接登录,但是我们仍然可用通过 su -s /bin/bash username 来切换到该用户下面

由于所有用户都需要取得uid和gid来判断权限,因此/etc/passwd的权限是644,所有人都可以读,所以密码文件现在不存放在这里了,而是存放在 /etc/shadow
[root@node1 ~]# ls -l /etc/passwd
-rw-r--r-- 1 root root 1289 Jan 20 11:33 /etc/passwd

可用看到/etc/shadow默认是所有人都没有权限(root除外,权限问题不考虑root)
[root@node1 ~]# ls -l /etc/shadow
---------- 1 root root 740 Jan 20 11:33 /etc/shadow


[root@node1 ~]# head -3  /etc/shadow
root:$6$Vk85PXWvektof1cd$Eo/uwutfguLfOGbAGffOgtqzrZr9NcGtCnYPkSXQ2h9U.Rs3saIXd1A5IeyXbr3MExdubOYmucp0pdXPxooNj.::0:99999:7:::
bin:*:17834:0:99999:7:::
daemon:*:17834:0:99999:7:::
按照冒号分割,一共有9列,含义分别是:
用户名称 加密过后的用户密码 最近更改密码的时间 禁止修改密码的天数 用户必须更改密码的天数 警告更新密码的期限 在用户密码过期之后禁用的天数 失效时间 保留字段

使用useradd添加用户会更改 /etc/passwd  /etc/shadow  /etc/group /etc/gshadow
使用passwd为用户设置密码会更改/etc/shadow


/etc/group 用户组信息文件,同样该文件的权限是644
[root@node1 ~]# ls -l /etc/group
-rw-r--r-- 1 root root 584 Jan 20 11:33 /etc/grou

[root@node1 ~]# head -2 /etc/group
root:x:0:
bin:x:1:
/etc/group 里的内容按照冒号分割为4列,含义分别是:
groupName groupPassword gid 用户组成员

使用groupadd添加用户组会更改 /etc/group /etc/gshadow

/etc/gshadow是存放用户组密码的,目前几乎无实际使用场景,无需了解


对比使用useradd前后,这几个文件的变化如下:
[root@node1 xtest]# id hello
id: hello: no such user
[root@node1 xtest]# cp /etc/passwd /etc/shadow /etc/group /etc/gshadow .
[root@node1 xtest]# ls
group  gshadow  passwd  shadow
[root@node1 xtest]# useradd hello
[root@node1 xtest]# diff /etc/passwd passwd 
27d26
< hello:x:1000:1000::/home/hello:/bin/bash
[root@node1 xtest]# diff /etc/group group 
47d46
< hello:x:1000:
[root@node1 xtest]# diff /etc/shadow shadow 
27d26
< hello:!!:18329:0:99999:7:::

相关命令概览

虽然可以直接通过修改用户相关的文件直接操作用户,但经量还是使用系统提供的命令(接口)来操作比较稳妥

useradd
usermod
userdel

passwd
chpasswd
chage

id
su
w
last
lastlog
who
whoami


sudo
visudo

groupadd
groupdel
groupmod
groups
newgrp

操作Linux中用户管理相关的命令有很多,后面我也会简单介绍部分重要命令的用法,其实对于Linux的用户管理,更重要的是我们要有整体的框架,捋清楚这些命令能干什么,能给我们解决什么问题,事实上对于初学者,或者测试环境,可能永远都用不着了解Linux中的用户管理,一手root用户走天下

比较常见的的需要我们手动建立用户的情况,一般是一些需要比较高安全性的场景,比如elasticsearch,它强制要求必须运行在非root用户下,如果我们安装软件的方式是RPM包安装之类,那么系统会自动处理好创建用户,设置用户组等等过程,不过有时我们安装软件的方式是二进制安装,这时候就需要自己手动创建相关的用户和用户组

一定要弄清楚的是,只有uid,gid才唯一代表一个用户或用户组,尤其是我们使用类似于NFS,或docker容器映射出来的文件,或者其它跨越多个系统的文件,可能在机器A上某用户的uid和机器B上同样用户名称的uid不一样,如果我们只看用户名称,就容易造成权限管理不当,以至于引发一些问题

在一些场景中,有些时候时间紧急或者图方便(实则还是技术不够),会直接授权某个目录777权限,以便快速解决权限不足问题,这样虽然不专业但是确实能解决问题; 不过最好不要授予某个非root用户的uid是0,或者把某个用户强行加入到root组(gid是0),这样做风险太大,建议不要使用

useradd

创建用户最常见的就是使用 useradd 命令,Ex:
# useradd xtest
# id xtest
uid=1001(xtest) gid=1001(xtest) groups=1001(xtest)

当我们不带任何参数使用useradd命令添加用户时,系统做了如下操作
- 读取配置文件 /etc/login.defs /etc/default/useradd 根据配置文件的规则做相应的操作 (直接打开这两个文件阅读即可)
- 创建一个同名的用户组,同步更新 /etc/passwd /etc/shadow /etc/group /etc/gshadow
- 为这个用户创建一个家目录,把 /etc/skel 中所有隐藏文件复制新用户的家目录

/etc/skel 里的文件主要是控制新用户的命令提示符,初始环境变量什么的
[root@node1 skel]# ls -la /etc/skel
total 24
drwxr-xr-x.  2 root root   62 Apr 11  2018 .
drwxr-xr-x. 86 root root 8192 Mar  8 22:42 ..
-rw-r--r--.  1 root root   18 Oct 31  2018 .bash_logout
-rw-r--r--.  1 root root  193 Oct 31  2018 .bash_profile
-rw-r--r--.  1 root root  231 Oct 31  2018 .bashrc

比如如果某同事不小心误删了家目录的隐藏文件:
[xtest@node1 ~]$ ls -a
.  ..  .bash_history  .bash_logout  .bash_profile  .bashrc
[xtest@node1 ~]$ rm -rf .bash*
当该同事下次再登录机器,就会发现,提示符变化了
-bash-4.2$ whoami
xtest
这就是因为缺失了默认的环境变量配置,把缺失的环境变量复制回去就好
cp /etc/skel/.bash* ~
然后退出重新登录,即可


useradd --help
man useradd
info useradd

-c, --comment COMMENT
    在字段5添加一些个人说明。

    [root@as4k ~]# useradd -c this_is_my_boss steam
    [root@as4k ~]# grep steam /etc/passwd
    steam:x:606:609:this_is_my_boss:/home/steam:/bin/bash

-g, --gid GROUP
    创建用户时指定一个用户组,该组必须已经存在。添加此参数,则不会在默认
    创建一个与用户名称相同的组。

    [root@as4k ~]# useradd -g steam steam2
    [root@as4k ~]# id steam2
    uid=607(steam2) gid=609(steam) groups=609(steam)
    [root@as4k ~]# grep steam2 /etc/passwd
    steam2:x:607:609::/home/steam2:/bin/bash

-u, --uid UID
    创建用户的时候指定UID值,该值需比当前最小的UID值大,此后新用户的UID,也按照
    当前最大的UID,向后递增。

    [root@as4k ~]# useradd -u 620 steam3
    [root@as4k ~]# grep steam3 /etc/passwd
    steam3:x:620:620::/home/steam3:/bin/bash

-s, --shell SHELL
    创建新用户时指定shell,通常我们不会修改默认shell解释器,但有时会把shell解释器
    更改成/sbin/nologin,表示禁止登录,此例在企业中部署nginx、mysql等服务时经常
    用到。

    [root@as4k ~]# useradd -s /sbin/nologin steam4
    [root@as4k ~]# grep steam4 /etc/passwd
    steam4:x:621:621::/home/steam4:/sbin/nologin

-r, --system                  create a system account

#手动创建家目录
useradd  --no-create-home --home-dir /data/myftp  myftp
mkdir -p /data/myftp
chown -R myftp:myftp /data/myftp

usermod

usermod --help

修改系统已经存在的用户信息。

usermod [options] LOGIN

-a, --append
    Add the user to the supplementary group(s). 
    Use only with the -G option.

-G, --groups GROUP1[,GROUP2,...[,GROUPN]]
    A list of groups that contain the user as a member. Each group is 
    separated from the next by a comma, with no intervening whitespace. 
    The groups are subject to the same restrictions as the group given 
    with the -g option. If the user is currently a member of a group that 
    is not listed, the user will be removed from the group.

    [root@as4k ~]# usermod -a -G gas1,gas2,gas3 as2
    [root@as4k ~]# id as2
    uid=624(as2) gid=632(as2) groups=632(as2),629(gas1),630(gas2),631(gas3)

-c COMMENT, --comment COMMENT
    The new value of the user's password file comment field.

    [root@as4k ~]# grep as2 /etc/passwd
    as2:x:624:632::/home/as2:/bin/bash
    [root@as4k ~]# usermod -c hello-world as2
    [root@as4k ~]# grep as2 /etc/passwd
    as2:x:624:632:hello-world:/home/as2:/bin/bash

-g GROUP, --gid GROUP
    更改初始用户组。
    The group name or number of the user's new initial login group. 
    The group must exist. Any file from the user's home directory 
    owned by the previous primary group of the user will be owned 
    by this new group.

    [root@as4k ~]# id as2
    uid=624(as2) gid=632(as2) groups=632(as2),629(gas1),630(gas2),631(gas3)
    [root@as4k ~]# usermod -g as1 as2
    [root@as4k ~]# id as2
    uid=624(as2) gid=628(as1) groups=628(as1),629(gas1),630(gas2),631(gas3)

-l NEW_LOGIN, --login NEW_LOGIN
    The name of the user will be changed from LOGIN to NEW_LOGIN. Nothing else 
    is changed. In particular, the user's home directory or mail spool should 
    probably be renamed manually to reflect the new login name.

    [root@as4k ~]# id xing
    uid=622(xing) gid=609(steam) groups=609(steam),621(steam4)
    [root@as4k ~]# usermod -l sheng xing
    [root@as4k ~]# id xing
    id: xing: No such user
    [root@as4k ~]# id sheng
    uid=622(sheng) gid=609(steam) groups=609(steam),621(steam4)

userdel

userdel - delete a user account and related files

userdel [options] LOGIN

The userdel command modifies the system account files, deleting all
entries that refer to the user name LOGIN. The named user must exist.

-r, --remove
    Files in the user´s home directory will be removed along with the
    home directory itself and the user´s mail spool. Files located in
    other file systems will have to be searched for and deleted
    manually.
    The mail spool is defined by the MAIL_DIR variable in the
    login.defs file.

    [root@as4k ~]# userdel -r sheng

不加参数删除用户,只删除用户的个人信息,不能删除用户的邮箱信息,想彻底删除用户,
就要进入到邮箱所存在的路径,手动删除。

    [root@as4k ~]# ls /var/spool/mail
    agan11  agan6  agan8  as2  kdb     oldboy2  steam   
    steam3  tian  u3  www  y2 agan30  agan7  as1    bz   
    oldboy  root     steam2  steam4  u2    u6  y1

passwd

passwd - update user’s authentication tokens

passwd  [-k]  [-l]  [-u  [-f]] [-d] [-e] [-n mindays] [-x maxdays] 
[-w warndays] [-i inactivedays] [-S] [--stdin] [username]

--stdin
    This option is used to indicate that passwd should read the new
    password from standard input, which can be a pipe.

Change Your Password
    passwd

Change Another User's Password
    passwd oldboy

root用户修改密码,不需要知道原始密码,其它用户修改密码,需要原始密码。

一条命令非人工交互设置密码(企业使用技巧)
    [root@as4k ~]# echo '123456' | passwd --stdin as1
    Changing password for user as1.
    passwd: all authentication tokens updated successfully.

id - 查看用户ID信息

[root@192_168_31_102 ~]# id root
uid=0(root) gid=0(root) groups=0(root)

root用户特殊,uid和gid都是0,一般新建的用户uid从1000开始

[root@192_168_31_102 ~]# useradd test2
[root@192_168_31_102 ~]# id test2
uid=1001(test2) gid=1001(test2) groups=1001(test2)

su sudo

su的作用是变更为其它使用者的身份,超级用户除外,需要键入该使用者的密码。
su 切换用户却不切换工作环境 , su - 同时切换用户与工作环境

注:
su的缺点造就了sudo的诞生
由于用户通过 su root 命令直接获取root权限,从而造成用户的权限太大,也就可能给系统
造成危险。为了既保证系统的安全又可以执行相应命令,sudo 也就以此诞生。

范例1:给普通用户u1提权,让普通用户可以查看root用户的家目录;普通用户可以使用
useradd命令,创建新用户
范例分析步骤:
1)  useradd   u1
2) visudo=vi打开/etc/sudoers文件 或  vim  /etc/sudoers 
注:visudo会检查内部语法,避免用户输入错误信息,所以我们一般使用visudo,
编辑此文件要用root权限
1)  编辑文件的第98行,编辑完成后,wq! 强制保存退出
root    ALL=(ALL)     ALL
u1      ALL=(ALL)     /bin/ls,/usr/sbin/useradd 

as4k ALL=(ALL) NOPASSWD:ALL
    
4)使用u1 用户登录测试       
    sudo    useradd  u11      //可成功创建用户,证明提权成功
    sudo    ls   /root            //可查看root的家,证明提权成功
5) sudo   -l         //-l 参数是列出当前用户可执行的命令,但只有在
sudoers文件里的用户才能使用该选项。


sudo -i 输入当前用户的密码,切换到root用户
su - root OR su --login root 切换到root用户并更新环境变量信息
su root  切换到root用户,不更新环境变量信息

直接以某个用户的身份执行命令
[root@node1 ~]# whoami
root
[root@node1 ~]# su -c "whoami" - xtest
xtest

sudo ls /root
sudo -i



Linux中su、sudo、sudo -i的用法和区别
https://www.cnblogs.com/csnd/p/11807646.html

在这里插入图片描述

用户环境变量

用户的bash环境变量配置文件是什么?
/etc/profile
/etc/bashrc
/etc/bash.bash_logout
/etc/profile.d/*.sh 
~/.bash_login
~/.bash_profile
~/.bashrc
~/.profile

实践场景

useradd as1
id as1
groupadd gas1
groupadd gas2
groupadd gas3
给as1用户,追加1个gas1组
usermod -G gas1 as1
# id as1
uid=623(as1) gid=628(as1) groups=628(as1),629(gas1)


虽然设置的是nologin的shell,但也可以登录
su -s /bin/bash username
比如切换到jenkins用户下拉取代码
su -s /bin/bash jenkins

csdn 112373377