Linux用户管理🍊
1、💓Linux用户和用户组介绍
1.1. ✅linux用户分类
Linux用户一般分为3类:
管理员用户:"root"拥有最高权限
普通用户:以个人姓名命名的账号
虚拟用户:启动进程必须使用一个用户称为“虚拟用户”,但这个用户不需要登录操作系统。
1.1.1. 管理员用户(root)
root用户他的UID和GID均为0,root用户在每台Unix/Linux操作系统中都是唯一且真实存在的,拥有最高的管理权限。
root@xiaoju:~]# id uid=0(root) gid=0(root) groups=0(root)
企业生产:
a.在企业生产环境中,一般禁止root账号通过SSH远程登录连接服务器。使用普通用户登录,然后切换到root。
b.平时使用普通用户登录,有必要的话再登录root。
c.在Linux系统中,UID为0的用户就是超级用户。
1.1.2. 普通用户
由管理员用户创建的。仅拥有操作自家目录中的文件及目录的权限(在自己家随便玩),权限范围:家目录 /home/用户名,/tmp。
UID整数范围: 1000-60000
普通用户如何管理系统:
a.普通用户可以通过su - root切换root用户, 角色改变,由普通用户变成了root。
b.不切换到root,可以使用root的权限去做事,使用sudo,赋予普通用户一部分权限。
1.1.3. 虚拟用户
虚拟用户的特点就是安装系统后会默认存在,不能登录系统,但是是系统正常运行不可缺少的,他们的存在是为了方便系统管理,满足相应的系统进程对文件属组的要求。
UID 整数范围:1-999
1.1.4. 用户组(group)
Linux系统中的用户组就是具有相同特征的用户的集合,1个用户可以在多个组里,1个组可以有多个用户。
用户组怎么产生的?
1)创建用户的时候默认产生的,创建一个test1,默认情况就会生成test1组,用户和组同名,且UID和GID相同。
2)由root用户直接创建。
2. 💓配置文件💥💥💥
2.1. ✅与用户相关的配置文件
2.1.1. /etc/passwd💥💥💥——查看用户信息
用户主配置文件,存放用户的信息,登录系统通过passwd文件验证登录
查看当前有多少个用户
[root@linux:~]# head -n 3 /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin
passwd 字段解释说明
|
root |
x |
0 |
0 |
root |
/root |
/bin/bash |
|
用户名称 |
用户密码 |
用户UID |
用户GID |
用户说明 |
用户家目录 |
命令解释器 |
2.1.2. /etc/shadow ——用户的密码文件,存放密码及密码属性
[root@linux:~]# head -n 3 /etc/shadow root:$6$/OWWJtFJXvnsvCau$j59eGRmKujZkyUb08kQJFSAVyGrDjxn/GuzT5pJIsMbuyrgyrZzS55g8289Y6RMJ345ftDmUPnOQWnYtsE3IO0::0:99999:7::: bin:*:18699:0:99999:7::: daemon:*:18699:0:99999:7:::
shadow字段解释说明
|
字段名称 |
注释说明 |
|
|
: |
分隔符 |
|
|
root |
用户名称 |
用户名称 |
|
* |
用户密码 |
该密码是加密过的字符串 |
|
最近更改密码的时间 |
从1970年1月1日起,到用户最近一次更改密码的天数 |
|
|
禁止修改密码的天数 |
从1970年1月1日起,到用户可以更改密码的天数 |
|
|
用户必须更改口令的天数 |
从1970年1月1日起,到用户必须更改密码的天数 |
|
|
警告更改密码的期限 |
在用户密码过期之前多少天提醒用户更改密码 |
|
|
不活动时间 |
在用户密码过期之后到禁用账户的天数 |
|
|
失效时间 |
从1970年1月1日起,到用户被禁用的天数 |
|
|
标志 |
保留 |
2.2. ✅与用户组相关的配置文件
2.2.1. /etc/group——存放用户组信息及属性文件
[root@linux:~]# head -n 3 /etc/group root:x:0: bin:x:1: daemon:x:2:
group字段解释说明
|
字段名称 |
注释说明 |
|
用户组名 |
用户组的名称 |
|
用户组密码 |
通常不需要设置密码,基于安全原因,该密码被记录在/etc/gshadow中,因此显示为“X” |
|
用户组成员 |
加入这个组的所有成员 |
2.2.2. /etc/gshadow ——用户组的密码文件(废弃不用)
[root@linux:~]# head -n 5 /etc/gshadow root::: bin::: daemon::: sys::: adm:::
gshadow字段解释说明
|
字段名称 |
注释说明 |
|
用户组名 |
用户组的名称 |
|
用户组密码 |
这个字段可以用空或者“!”,如果是空或有“!”,则表示没有密码 |
|
用户组管理员用户 |
用户组管理者,这个字段也可以用空 |
|
用户组成员 |
加入这个组的所有成员 |
2.3. ✅和创建用户相关的文件💥💥💥
2.3.1. /etc/default/useradd——创建用户useradd 命令的配置文件
/etc/default/useradd文件是在使用useradd添加用户时预先加载的默认用户信息配置文件,可以用useradd -D修改此文件。
[root@linux:~]# cat /etc/default/useradd # useradd defaults file GROUP=100 ###依赖于/etc/login.defs的USERGROUPS_ENAB参数,如果为no,则此处控制 HOME=/home ###在/home 下创建用户家目录 INACTIVE=-1 ###是否启用用户过期停权,-1表示不启用 EXPIRE= ###用户终止日期,不设置表示不启用 SHELL=/bin/bash ###新用户默认使用shell解释器类型 SKEL=/etc/skel ###配置新用户家目录的默认环境变量文件的存放路径 CREATE_MAIL_SPOOL=yes ###创建mail文件
2.3.2. /etc/skel💥💥💥——创建用户环境变量原始文件存放地。
/etc/skel 目录是用来存放新用户需要的所有基础环境变量文件的目录,当添加新用户时,这个目录下的所有文件都会被自动复制到新添加用户的家目录下。
[root@xiaozhou /]# ls -la /etc/skel -rw-r--r--. 1 root root 18 11月 25 2021 .bash_logout --->用户环境变量文件,退出时加载 -rw-r--r--. 1 root root 193 11月 25 2021 .bash_profile --->用户环境变量文件,登录时加载 -rw-r--r--. 1 root root 231 11月 25 2021 .bashrc --->用户环境变量文件,登录时加载
以长格式查看目录及目录下的全部文件,包括隐藏文件
2.3.3. /etc/login.defs ——创建用户系统配置,对应文件。
/etc/login.defs文件的作用就是预先定义创建用户时用户默认配置信息。如创建用户时,是否需要家目录,以及UID和GID的取值起始值及取值范围,用户及密码的有效期限等
[root@xiaozhou ~]# cat /etc/login.defs # # Please note that the parameters in this configuration file control the # behavior of the tools from the shadow-utils component. None of these # tools uses the PAM mechanism, and the utilities that use PAM (such as the # passwd command) should therefore be configured elsewhere. Refer to # /etc/pam.d/system-auth for more information. # # *REQUIRED* # Directory where mailboxes reside, _or_ name of file, relative to the # home directory. If you _do_ define both, MAIL_DIR takes precedence. # QMAIL_DIR is for Qmail # #QMAIL_DIR Maildir MAIL_DIR /var/spool/mail ###设定用户对应的mail文件所在的路径 #MAIL_FILE .mail # Password aging controls: # # PASS_MAX_DAYS Maximum number of days a password may be used. # PASS_MIN_DAYS Minimum number of days allowed between password changes. # PASS_MIN_LEN Minimum acceptable password length. # PASS_WARN_AGE Number of days warning given before a password expires. # PASS_MAX_DAYS 99999 ###密码最长可以设置天数 PASS_MIN_DAYS 0 ###更换密码的最小天数 PASS_MIN_LEN 5 ###密码的最小长度 PASS_WARN_AGE 7 ###密码失效之前提前几天开始警告 # # Min/max values for automatic uid selection in useradd # UID_MIN 1000 ###默认最小UID为1000,也就是说添加用户时,UID从1000开始 UID_MAX 60000 ###默认最大UID为60000 # System accounts SYS_UID_MIN 201 SYS_UID_MAX 999 # # Min/max values for automatic gid selection in groupadd # GID_MIN 1000 ###默认最小GID为1000,也就是说添加用户时,GID从1000开始 GID_MAX 60000 ###默认最大GID为60000 # System accounts SYS_GID_MIN 201 SYS_GID_MAX 999 # # If defined, this command is run when removing a user. # It should remove any at/cron/print jobs etc. owned by # the user to be removed (passed as the first argument). # #USERDEL_CMD /usr/sbin/userdel_local ###删除用户时,删除与用户相关的所有工作,默认关闭 # # If useradd should create home directories for users by default # On RH systems, we do. This option is overridden with the -m flag on # useradd command line. # CREATE_HOME yes ###是否创建用户家目录,默认创建,可以用-m参数控制,参数-M为不创建 # The permission mask is initialized to this value. If not specified, # the permission mask will be initialized to 022. UMASK 077 ###家目录对应的umask默认值0022,系统默认 # This enables userdel to remove user groups if no members exist. # USERGROUPS_ENAB yes ###删除用户同时删除用户组(如果用户组没有其他成员则删除) # Use SHA512 to encrypt password. ENCRYPT_METHOD SHA512 ###密码加密算法,SHA512 [root@xiaozhou ~]
重点记忆
1)UID、GID 默认 大于 1000、小于 60000
2)umask 系统默认值 0022
3. 💓Linux 用户及用户组命令💥💥💥
3.1. ✅linux 用户命令
3.1.1. 💥useradd:创建用户(kliny、centos)
3.1.1.1. 工作原理
当不带任何参数使用useradd命令添加用户时,首先会读取配置文件/etc/login.defs和/etc/default/useradd 中预先定义的规则,并根据设置的规则添加用户,同时还会向/etc/passwd和/etc/group文件内添加新建的用户和用户组记录。当然,/etc/shadow和/etc/gshadow也会同步生成记录,同时系统还会根据/etc/login.defs和/etc/default/useradd文件中的配置信息建立用户的家目录,并复制/etc/skel中所有隐藏的环境配置文件到新用户的家目录中,已完成对用户环境的初始化设置。
3.1.1.2. useradd:添加用户命令
参数选项:
|
参数选项 |
注释说明 |
参数选项 |
注释说明 |
|
-u |
添加用户,指定用户UID |
-d |
指定用户登录时使用的家目录 |
|
-g |
添加新用户指定所属于组GID |
-m |
创建家目录 |
|
-s |
指定解释器 /bin/bash /sbin/nologin |
-M |
添加用户不创建家目录 |
|
-c |
添加新用户在passwd文件中的说明字段的信息 |
-G |
将新用户指定为多个不同用户组的成员,每个组用“,”隔开 |
|
-e |
添加用户设置过期时间 |
💥💥实践案例1:
案例1.创建用户 [root@linux:~]# useradd test001 [root@linux:~]# id test001 uid=1001(test001) gid=1001(test001) groups=1001(test001) [root@linux:~]# grep test001 /etc/passwd test001:x:1001:1001::/home/test001:/bin/bash [root@linux:~]# ll /home/ total 0 drwx------ 2 test001 test001 76 Jul 16 19:50 test001 案例2.创建一个test002的普通用户,uid是1002,gid是1002,有家目录,用户使用登录/bin/bash [root@linux:~]# groupadd -g1002 test002 [root@linux:~]# useradd -u1002 -g1002 -m -s /bin/bash test002 [root@linux:~]# id 002 uid=2(daemon) gid=2(daemon) groups=2(daemon) [root@linux:~]# id test002 uid=1002(test002) gid=1002(test002) groups=1002(test002) [root@linux:~]# grep test002 /etc/passwd test002:x:1002:1002::/home/test002:/bin/bash [root@linux:~]# ll /home/ total 0 drwx------ 2 test001 test001 76 Jul 16 19:50 test001 drwx------ 2 test002 test002 76 Jul 16 19:57 test002 案例3.创建一个test003的用户,指定uid=777,gid=777 ,不允许登录,不创建家目录 [root@linux:~]# groupadd -g777 test003 [root@linux:~]# useradd -u777 -g777 -M -s /sbin/nologin test003 [root@linux:~]# id test003 uid=777(test003) gid=777(test003) groups=777(test003) [root@linux:~]# grep test003 /etc/passwd test003:x:777:777::/home/test003:/sbin/nologin [root@linux:~]# ll /home/ total 0 drwx------ 2 test001 test001 76 Jul 16 19:50 test001 drwx------ 2 test002 test002 76 Jul 16 19:57 test002
⚡注意:
创建新用户如果需要指定GID时,必须先给用户创建GID,使用命令groupadd -g888 用户名
实践案例2:
案例1.批量创建test01到test10十个用户 [root@linux:~]# echo test{01..10} test01 test02 test03 test04 test05 test06 test07 test08 test09 test10 [root@linux:~]# echo useradd" "test{01..10} useradd test01 useradd test02 useradd test03 useradd test04 useradd test05 useradd test06 useradd test07 useradd test08 useradd test09 useradd test10 [root@linux:~]# echo useradd" "test{01..10}|xargs -n2 useradd test01 useradd test02 useradd test03 useradd test04 useradd test05 useradd test06 useradd test07 useradd test08 useradd test09 useradd test10 [root@linux:~]# echo useradd" "test{01..10}|xargs -n2|bash [root@linux:~]# tail -10 /etc/passwd test01:x:1006:1006::/home/test01:/bin/bash test02:x:1007:1007::/home/test02:/bin/bash test03:x:1008:1008::/home/test03:/bin/bash test04:x:1009:1009::/home/test04:/bin/bash test05:x:1010:1010::/home/test05:/bin/bash test06:x:1011:1011::/home/test06:/bin/bash test07:x:1012:1012::/home/test07:/bin/bash test08:x:1013:1013::/home/test08:/bin/bash test09:x:1014:1014::/home/test09:/bin/bash test10:x:1015:1015::/home/test10:/bin/bash
实践案例3:
批量创建多个用户: 方法1: [root@linux:~]# seq -w 10|sed -rn 's#(.*)#useradd oldboy\1;echo oldboy123.com | passwd --stdin oldboy\1#gp' useradd oldboy01;echo oldboy123.com | passwd --stdin oldboy01 useradd oldboy02;echo oldboy123.com | passwd --stdin oldboy02 useradd oldboy03;echo oldboy123.com | passwd --stdin oldboy03 useradd oldboy04;echo oldboy123.com | passwd --stdin oldboy04 useradd oldboy05;echo oldboy123.com | passwd --stdin oldboy05 useradd oldboy06;echo oldboy123.com | passwd --stdin oldboy06 useradd oldboy07;echo oldboy123.com | passwd --stdin oldboy07 useradd oldboy08;echo oldboy123.com | passwd --stdin oldboy08 useradd oldboy09;echo oldboy123.com | passwd --stdin oldboy09 useradd oldboy10;echo oldboy123.com | passwd --stdin oldboy10 [root@linux:~]# seq -w 10|sed -rn 's#(.*)#useradd oldboy\1;echo oldboy123.com | passwd --stdin oldboy\1#gp' | bash Changing password for user oldboy01. passwd: all authentication tokens updated successfully. Changing password for user oldboy02. passwd: all authentication tokens updated successfully. Changing password for user oldboy03. passwd: all authentication tokens updated successfully. Changing password for user oldboy04. passwd: all authentication tokens updated successfully. Changing password for user oldboy05. passwd: all authentication tokens updated successfully. Changing password for user oldboy06. passwd: all authentication tokens updated successfully. Changing password for user oldboy07. passwd: all authentication tokens updated successfully. Changing password for user oldboy08. passwd: all authentication tokens updated successfully. Changing password for user oldboy09. passwd: all authentication tokens updated successfully. Changing password for user oldboy10. passwd: all authentication tokens updated successfully. 方法2: [root@linux:~]# seq -w 10|awk '{print "useradd oldboy"$1 "; echo oldboy123.com | passwd --stdin oldboy"$1}'|bash Changing password for user oldboy01. passwd: all authentication tokens updated successfully. Changing password for user oldboy02. passwd: all authentication tokens updated successfully. Changing password for user oldboy03. passwd: all authentication tokens updated successfully. Changing password for user oldboy04. passwd: all authentication tokens updated successfully. Changing password for user oldboy05. passwd: all authentication tokens updated successfully. Changing password for user oldboy06. passwd: all authentication tokens updated successfully. Changing password for user oldboy07. passwd: all authentication tokens updated successfully. Changing password for user oldboy08. passwd: all authentication tokens updated successfully. Changing password for user oldboy09. passwd: all authentication tokens updated successfully. Changing password for user oldboy10. passwd: all authentication tokens updated successfully. 批量删除多个用户 [root@linux:~]# find /home -name "oldboy*"|awk -F"/" '{print "userdel -r"" " $NF}'|bash
3.1.1.3. ubuntu 乌班图创建用户
案例1.直接创建新用户 root@oldboy-lnb:~# useradd test01 root@oldboy-lnb:~# grep test01 /etc/passwd test01:x:1001:1001::/home/test01:/bin/sh root@oldboy-lnb:~# ll /home/ total 12 drwxr-xr-x 3 root root 4096 Jun 27 19:23 ./ drwxr-xr-x 20 root root 4096 Jun 27 19:22 ../ drwxr-x--- 4 oldboy oldboy 4096 Jul 8 09:59 oldboy/ 案例2.创建指定用户 root@oldboy-lnb:~# groupadd -g1005 test02 root@oldboy-lnb:~# useradd -u1005 -g1005 -m -s /bin/bash test02 root@oldboy-lnb:~# id uid=0(root) gid=0(root) groups=0(root) root@oldboy-lnb:~# grep test02 /etc/passwd test02:x:1005:1005::/home/test02:/bin/bash root@oldboy-lnb:~# ll /home/ total 16 drwxr-xr-x 4 root root 4096 Jul 16 20:17 ./ drwxr-xr-x 20 root root 4096 Jun 27 19:22 ../ drwxr-x--- 4 oldboy oldboy 4096 Jul 8 09:59 oldboy/ drwxr-x--- 2 test02 test02 4096 Jul 16 20:17 test02/ 案例3.批量创建多个用户 root@oldboy-lnb:~# seq -w 03 |awk '{print "useradd -m -s /bin/bash test"$NF";echo test"$NF":123456|chpasswd"}' | bash root@oldboy-lnb:~# seq -w 003 | sed -r 's#(.*)#useradd -m -s /bin/bash wwwxq\1;echo wwwxq\1:123456|chpasswd#g' | bash 案例4.批量删除多个用户 root@oldboy-lnb:~# find /home/ -name "wwwxq*" | awk -F"/" '{print "userdel -r "$NF}' | bash
3.1.2. 💥usermod :修改用户信息命令
参数选项:
|
参数选项 |
注释说明 |
参数选项 |
注释说明 |
|
-u |
修改用户 UID |
-g |
修改用户所属于组 |
|
-s |
修改用户的登录shell |
-e |
修改用户过期时间 |
|
-d |
修改用户登录时使用的家目录 |
[root@xiaozhou /]# id test2 uid=5000(test2) gid=5000(test2) 组=5000(test2) [root@xiaozhou /]# usermod -u 5002 test2 [root@xiaozhou /]# id test2 uid=5002(test2) gid=5000(test2) 组=5000(test2) [root@xiaozhou /]#
3.1.3. 💥userdel:删除用户命令
参数选项:
|
参数选项 |
注释说明 |
参数选项 |
注释说明 |
|
-f |
强制删除用户而不询问 |
-r |
删除用户的家目录及其全部子目录 |
实践案例:
root@oldboy-lnb:~# ll /home/ drwxr-x--- 2 test02 test02 4096 Jul 16 20:14 test02/ root@oldboy-lnb:~# userdel -r test02 userdel: test02 mail spool (/var/mail/test02) not found root@oldboy-lnb:~# ll /home/ drwxr-x--- 4 oldboy oldboy 4096 Jul 8 09:59 oldboy/
创建用户流程📍
第一步:创建新用户 [root@linux:~]# groupadd -g1005 test001 [root@linux:~]# useradd -u1005 -g1005 -m -s /bin/bash test001 [root@linux:~]# ll /home/ total 0 drwx------ 2 test001 test001 76 Jul 16 21:55 test001 第二步:系统将/etc/skel/隐藏文件复制到/home/test001家目录中(复制的是文件的环境变量) 查看/home/test001下的环境变量文件 [root@linux:~]# ll -a /home/test001/ total 16 drwx------ 2 test001 test001 76 Jul 16 21:55 . drwxr-xr-x 4 root root 37 Jul 16 21:55 .. -rw-r--r-- 1 test001 test001 75 Aug 25 2022 .bash_logout -rw-r--r-- 1 test001 test001 71 Aug 25 2022 .bash_profile -rw-r--r-- 1 test001 test001 138 Aug 25 2022 .bashrc -rw-r--r-- 1 test001 test001 204 Mar 22 2022 .zshrc 第三步:将复制过去的文件属主属组修改为test001用户 演示家目录下的环境变量: 第一步: 注释/etc/profile中 PS1变量 第二步: 创建普通账号oldboy01 第三步: 新打开窗口切换到oldboy01账号 第四步: 删/ 第五步: 退出重新连接到oldboy01 重新连接后命令行提示符显示如下(centos显示如下,kylin显示正常) [root@linuxnc ~]# su - test01 Last login: Tue Jul 16 17:32:22 CST 2024 on pts/0 -bash-4.2$ # 表示家目录的变量文件没了,被误删除 出现以上情况的解决方法: 第一步:复制/etc/skel 到家目录下 -bash-4.2$ cp /etc/skel/.* . cp: omitting directory ‘/etc/skel/.’ cp: omitting directory ‘/etc/skel/..’ 第二步:查看环境变量文件 -bash-4.2$ ll -a total 24 drwx------ 2 test01 test01 4096 Jul 16 17:33 . drwxr-xr-x. 5 root root 4096 Jul 16 17:32 .. -rw------- 1 test01 test01 31 Jul 16 17:32 .bash_history -rw-r--r-- 1 test01 test01 18 Jul 16 17:33 .bash_logout -rw-r--r-- 1 test01 test01 193 Jul 16 17:33 .bash_profile 第三步:“退出xshell重新连接”或者“source .bash_profile”
3.2. ✅linux 用户组命令
3.2.1. 💥groupadd: 添加用户组
参数选项:
|
参数选项 |
注释说明 |
|
-g |
指定用户组GID,如果不使用-g参数,则GID从1000开始 |
实践案例:
[root@linux:~]# groupadd test001 [root@linux:~]# tail -1 /etc/group test001:x:1000: [root@linux:~]# groupadd -g888 test002 [root@linux:~]# tail -1 /etc/group test002:x:888:
3.2.2. 💥groupdel:删除用户组命令
实践案例:
[root@linux:~]# groupdel test002
[root@linux:~]# grep -w test002 /etc/group
3.3. ✅linux 修改用户密码命令
3.3.1. 💥passwd:修改用户密码命令
修改用户密码有两种方式:
- 交互式修改
- 非交互式修改
passwd命令可以修改用户密码及密码过期时间等内容,在工作中很常用;
用户自己设置密码,直接输入passwd后,即可设置密码;例:root用户给普通用户设置密码,需要在passwd后边加普通用户的用户名;例:参数选项:
|
参数选项 |
注释说明 |
参数选项 |
注释说明 |
|
--stdin |
从标准输入读取密码字符串※ |
-e |
使用户密码立即过期 |
|
-n |
设置修改密码的最短天数 |
-l |
锁定用户,锁定后不可登录 |
|
-x |
设置修改密码的最长天数 |
-u |
解除对用户的锁定 |
|
-w |
设置用户密码到期前多少天收到警告信息 |
-d |
删除用户的密码 |
|
-i |
设置密码过期多少天之后禁用账户 |
实践案例1:
[root@xiaozhou /]# passwd study 更改用户 study 的密码 。 新的 密码: passwd:所有的身份验证令牌已经成功更新。 [root@xiaozhou /]# passwd 更改用户 root 的密码 。 新的 密码: passwd:所有的身份验证令牌已经成功更新。 [root@xiaozhou /]# echo 12345678 |passwd --stdin study 在用户root里更改用户 study 的密码 。 passwd:所有的身份验证令牌已经成功更新。
案例实践2.
方法一: [root@linux:~]# seq -w 03 | awk '{print "useradd -m -s /bin/bash test"$1 " ;pass=`mkpasswd`" " ;echo $pass|passwd --stdin test"$1" ;echo test"$1" $pass >>pass.txt "}' useradd -m -s /bin/bash test01 ;pass=`mkpasswd` ;echo $pass|passwd --stdin test01 ;echo test01 $pass >>pass.txt useradd -m -s /bin/bash test02 ;pass=`mkpasswd` ;echo $pass|passwd --stdin test02 ;echo test02 $pass >>pass.txt useradd -m -s /bin/bash test03 ;pass=`mkpasswd` ;echo $pass|passwd --stdin test03 ;echo test03 $pass >>pass.txt [root@linux:~]# seq -w 03 | awk '{print "useradd -m -s /bin/bash test"$1 " ;pass=`mkpasswd`" " ;echo $pass|passwd --stdin test"$1" ;echo test"$1" $pass >>pass.txt "}' | bash useradd: user 'test01' already exists Changing password for user test01. passwd: all authentication tokens updated successfully. useradd: user 'test02' already exists Changing password for user test02. passwd: all authentication tokens updated successfully. useradd: user 'test03' already exists Changing password for user test03. passwd: all authentication tokens updated successfully. [root@linux:~]# cat pass.txt test01 dpRV?q92d test02 3gVFs9{rb test03 9skr*6BSt 方法二:
3.3.2. 💥chpasswd:ubuntu 乌班图修改用户密码
root@oldboy-lnb:~# echo test02:666666|chpasswd
3.3.3. 💥chage:查看和更改密码属性
参数选项:
|
参数选项 |
注释说明 |
参数选项 |
注释说明 |
|
-l |
查看用户和更改密码属性※ |
-m |
设置密码可更改的最小天数 |
|
-E |
设置账号到期时间 |
-M |
设置密码保持有效的最大天数 |
|
-i |
设置密码到期后还可以使用几天 |
-W |
设置提前收到警告信息的天数 |
实践案例:
[root@xiaozhou /]# chage -l test001 最近一次密码修改时间 :4月 03, 2024 密码过期时间 :从不 密码失效时间 :从不 帐户过期时间 :从不 两次改变密码之间相距的最小天数 :0 两次改变密码之间相距的最大天数 :99999 在密码过期之前警告的天数 :7 [root@xiaozhou /]# chage -E "2024/05/01" test001 [root@xiaozhou /]# chage -l test001 最近一次密码修改时间 :4月 03, 2024 密码过期时间 :从不 密码失效时间 :从不 帐户过期时间 :5月 01, 2024 两次改变密码之间相距的最小天数 :0 两次改变密码之间相距的最大天数 :99999 在密码过期之前警告的天数 :7
3.4. ✅linux 查看用户信息命令
3.4.1. 💥id:显示用户与用户组信息
实践案例:
显示当前用户 root@oldboy /]# id uid=0(root) gid=0(root) 组=0(root) 环境=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 显示指定用户 [root@oldboy /]# id study uid=1000(study) gid=1000(study) 组=1000(study)
3.4.2. 💥whoami:显示当前用户名
实践案例:
[root@oldboy /]# whoami
root
3.4.3. 💥who:查看当前登录用户信息
参数选项:
|
参数选项 |
注释说明 |
参数选项 |
注释说明 |
|
-a |
显示全部信息 |
-H |
显示用户名、线路、时间、终端等 |
实践案例:
[root@oldboy /]# who root :0 2023-11-08 19:09 (:0) study pts/0 2024-03-25 13:23 (192.168.189.1) study pts/2 2024-04-03 15:47 (192.168.189.1) [root@oldboy /]# who -H 名称 线路 时间 备注 root :0 2023-11-08 19:09 (:0) study pts/0 2024-03-25 13:23 (192.168.189.1) study pts/2 2024-04-03 15:47 (192.168.189.1) [root@oldboy /]# who -H -a 名称 线路 时间 空闲 进程号 备注 退出 系统引导 2023-11-08 19:08 运行级别 5 2023-11-08 19:08 root ? :0 2023-11-08 19:09 ? 2190 (:0) study + pts/0 2024-03-25 13:23 旧的 21904 (192.168.189.1) pts/1 2023-11-15 20:52 9902 id=ts/1 终端=0 退出=0 study + pts/2 2024-04-03 15:47 . 23063 (192.168.189.1) pts/1 2024-04-03 15:48 0 id=/1 终端=0 退出=0
3.5. ✅Linux更改文件属性命令
3.5.1. 💥chown:改变文件或目录的用户或用户组
语法结构:
chown 【参数】 【用户:用户组】 【目录或文件】
参数选项:
|
参数选项 |
注释说明 |
参数选项 |
注释说明 |
|
-R |
递归处理所有子文件 |
-h |
仅对链接文件进行更改 |
实践案例:
环境准备: [root@oldboy /]# echo "I am xiaozhou " >text.file [root@oldboy /]# cat text.file I am xiaozhou [root@oldboy /]# ls -l text.file -rw-r--r--. 1 root root 15 4月 3 19:09 text.file chown 【用户】 【目录或文件】 ###=>仅授权用户 [root@oldboy /]# chown study text.file [root@oldboy /]# ls -l text.file -rw-r--r--. 1 study root 15 4月 3 19:09 text.file chown 【.用户组】 【目录或文件】 ###=>仅授权用户组 [root@oldboy /]# chown :study text.file [root@oldboy /]# ls -l text.file -rw-r--r--. 1 study study 15 4月 3 19:09 text.file chown 【用户:用户组】 【目录或文件】 ###=>同时授权用户和用户组 [root@oldboy /]# chown study:study text.file [root@oldboy /]# ls -l text.file -rw-r--r--. 1 study study 15 4月 3 19:09 text.file
3.5.2. 💥chgrp:更改文件所属用户组
参数选项:
|
参数选项 |
注释说明 |
参数选项 |
注释说明 |
|
-R |
递归更改目录的用户和用户组 |
-h |
对符号链接文件进行更改 |
实践案例:
[root@oldboy /]# ls -l text.file -rw-r--r--. 1 study study 15 4月 3 19:09 text.file [root@oldboy /]# chgrp root text.file [root@oldboy /]# ls -l text.file -rw-r--r--. 1 study root 15 4月 3 19:09 text.file
3.5.3. chattr:更改文件隐藏权限
参数选项:
|
参数选项 |
注释说明 |
参数选项 |
注释说明 |
|
-R |
递归处理所有子文件 |
+ |
给文件或目录增加指定的隐藏属性 |
|
-V |
显示执行过程 |
- |
给文件或目录移除指定的隐藏属性 |
|
= |
给文件或目录更新指定的隐藏属性 |
|
常用权限 |
注释说明 |
|
i |
无法对文件进行任何更改 |
|
a |
只能对文件添加内容,不能覆盖或删除,多用于服务器日志文件安全 |
|
A |
不修改这个文件或目录的最后访问时间 |
[root@oldboy /]# lsattr file.txt --->lsattr ——显示文件的隐藏属性 ---------------- file.txt [root@oldboy /]# chattr +i file.txt [root@oldboy /]# lsattr file.txt ----i----------- file.txt [root@oldboy /]# chattr -i file.txt [root@oldboy /]# lsattr file.txt ---------------- file.txt [root@oldboy /]# chattr +a file.txt [root@oldboy /]# lsattr file.txt -----a---------- file.txt [root@oldboy /]# rm -rf file.txt rm: 无法删除"file.txt": 不允许的操作
3.5.4. lsattr:查看文件隐藏权限
|
参数选项 |
注释说明 |
参数选项 |
注释说明 |
|
-R |
递归处理所有子文件 |
-V |
显示执行过程 |
|
-a |
显示目录中所有文件 |
4. 💓✅su 命令介绍
su命令就是切换用户身份的命令,su命令可以在用户之间进行切换。
- root用户切换普通用户不需要密码验证;
- 普通用户切换root用户需要用户密码验证;
- 普通用户和普通用户之间切换需要用户密码验证。
4.2. ✅💥su 命令用法
参数选项:
|
参数选项 |
注释说明 |
|
- |
切换用户时携带用户的环境变量 |
|
-c |
以切换的用户身份执行一个命令,然后在退回之前用户的环境 |
实践案例:
案例1.用su root直接切换root用户 [xiaozhou@linux:~]$ whoami ###当前登录用户为普通用户 xiaozhou [xiaozhou@linux:~]$ su root ###切换root用户 Password: [root@linux:xiaozhou]# env ###查看当前登录用户root的环境变量 SHELL=/bin/bash HISTCONTROL=ignoredups HOSTNAME=linux HISTSIZE=1000 PWD=/home/xiaozhou ###已切换到root用户了,但家目录还是xiaozhou LOGNAME=xiaozhou _=/usr/bin/env HOME=/root LANG=en_US.UTF-8 LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.webp=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36: TERM=linux USER=xiaozhou SHLVL=2 PS1=[\[\033[1;32m\]\u\[\033[1;37m\]@\[\033[1;36m\]\h\[\033[00m\]:\[\033[1;34m\]\W\[\033[00m\]]\$ PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin MAIL=/var/spool/mail/xiaozhou [root@linux:xiaozhou]# exit ###退出当前用户 exit 案例2.用su - root直接切换root用户 [xiaozhou@linux:~]$ su - root ###使用su - root再次切换到root用户 Password: Last login: Wed Jul 17 11:03:03 CST 2024 on pts/1 [root@linux:~]# env SHELL=/bin/bash HISTCONTROL=ignoredups HISTSIZE=1000 HOSTNAME=linux PWD=/root ###切换到root用户,家目录变为root LOGNAME=root HOME=/root LANG=en_US.UTF-8 LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.webp=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36: TERM=linux USER=root SHLVL=1 PS1=[\[\033[1;32m\]\u\[\033[1;37m\]@\[\033[1;36m\]\h\[\033[00m\]:\[\033[1;34m\]\W\[\033[00m\]]\$ PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin MAIL=/var/spool/mail/root _=/usr/bin/env [root@linux:~]#
5. 💓sudo :授权普通用户执行管理员命令
5.1. ✅sudo 命令介绍
sudo命令可以让普通用户在执行指定命令或程序时,拥有root用户的权限,并且可以有针对性的将不同命令或程序授权给指定的普通用户,同时普通用户不需要知道root用户密码,这个授权可以使用visudo配置管理。
5.2. ✅💥sudo 命令用法
参数选项:
|
参数选项 |
注释说明 |
|
-l |
查看当前用户被授予的sudo权限集合 |
实践案例:
第一步:查看拥有的sudo 权限(普通用户) [xiaozhou@linux:~]$ sudo -l We trust you have received the usual lecture from the local System Administrator. It usually boils down to these three things: #1) Respect the privacy of others. #2) Think before you type. #3) With great power comes great responsibility. [sudo] password for xiaozhou: Sorry, user xiaozhou may not run sudo on linux. 第二步:授权xiaozhou用户less命令(相当于root使用less执行命令),授权需要在root用户执行 [root@linux:~]# visudo ......(省略) ## Allow root to run any commands anywhere root ALL=(ALL) ALL xiaozhou ALL=(ALL) /usr/bin/less ---> xiaozhou可以使用root身份的less这个权限 # %wheel ALL=(ALL) NOPASSWD: ALL ...省略... "/etc/sudoers.tmp" 121L, 4397C written 第三步:使用xiaozhou 用户测试less命令 [xiaozhou@linux:~]$ sudo -l [sudo] password for xiaozhou: Matching Defaults entries for xiaozhou on linux: !visiblepw, always_set_home, match_group_by_gid, always_query_group_plugin, env_reset, env_keep="COLORS DISPLAY HOSTNAME HISTSIZE KDEDIR LS_COLORS", env_keep+="MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE", env_keep+="LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES", env_keep+="LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE", env_keep+="LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY", secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin User xiaozhou may run the following commands on linux: (ALL) /usr/bin/less [xiaozhou@linux:~]$ less /var/log/messages /var/log/messages: Permission denied [xiaozhou@linux:~]$ sudo less /var/log/messages [sudo] password for xiaozhou: --->(输入xiaozhou的密码) Jul 8 03:41:12 linux systemd[1]: kylin-kms-activation.service: Service RestartSec=30s expired, scheduling restart. Jul 8 03:41:12 linux systemd[1]: kylin-kms-activation.service: Scheduled restart job, restart counter is at 216. Jul 8 03:41:12 linux systemd[1]: Stopped run kylin_kms_daemon at boot time. Jul 8 03:41:12 linux systemd[1]: Started run kylin_kms_daemon at boot time. Jul 8 03:42:12 linux systemd[1]: kylin-kms-activation.service: Main process exited, code=exited, status=255/EXCEPTION Jul 8 03:42:12 linux systemd[1]: kylin-kms-activation.service: Failed with result 'exit-code'. Jul 8 03:42:42 linux systemd[1]: kylin-kms-activation.service: Service RestartSec=30s expired, scheduling restart. Jul 8 03:42:42 linux systemd[1]: kylin-kms-activation.service: Scheduled restart job, restart counter is at 217. Jul 8 03:42:42 linux systemd[1]: Stopped run kylin_kms_daemon at boot time. Jul 8 03:42:42 linux systemd[1]: Started run kylin_kms_daemon at boot time. Jul 8 03:43:42 linux systemd[1]: kylin-kms-activation.service: Main process exited, code=exited, status=255/EXCEPTION /var/log/messages
💥visudo 和 vim/etc/sudoers区别:
- visudo #可以检测语法(该命令直接编辑/etc/sudoers 文件)
- vim /etc/sudoers #不检测语法
sudo授权方式:
- 授权单个命令
xiaozhou ALL=(ALL) /usr/bin/less
- 授权多个命令
xiaozhou ALL=(ALL) /usr/bin/less,/usr/bin/touch,/usr/bin/vim
- 授权 ALL
xiaozhou ALL=(ALL) NOPASSWD: ALL, !/usr/bin/rm
总结:
- 通过sudo授权管理后,所有用户执行授权管理的特殊权限格式为“sudo 命令”;
- 如果需要切换到root执行相关操作,则可以通过“sudo su - "命令,注意此命令提示的密码为当前用户的密码,而不是root的密码;
- 执行“sudo -l”命令就可以查看当前用户被授予的sudo权限集合;
5.3. ✅sudo命令的工作原理

5.3.1. visudo:直接编辑/etc/sudoers 文件(ubuntu)
visudo 默认是nano编辑器 第一步:visudo #修改etc/sudoers 在Defaults后面添加 Defaults editor=/usr/bin/vim, env_editor 第二步:保存退出 按ctrl+x保存退出 (#可以使用vim编辑器编辑) 第三步:visudo 第四步:给用户添加赋权 xiaozhou ALL =(ALL:ALL) /usr/bin/less,/usr/bin/touch
ubuntu给用户赋sudo的权限时,常用的有两种方式:
第一种:root@oldboy-lnb:~# usermod -G27 xiaozhou
这种方式是直接将用户添加至sudo组,用户可以执行sudo的所有权限
第二种:编辑配置文件visudo,为用户单独增加授权
xiaozhou ALL =(ALL:ALL) /usr/bin/less,/usr/bin/touch,用户只能执行授权的的命令
如果将用户添加至sudo组,并且又单独增加了权限,用户执行时将执行sudo的全部权限

浙公网安备 33010602011771号