当前位置:网站首页>用户和组管理
用户和组管理
2022-07-22 18:11:00 【爱吃香菜的猪】
一、用户管理
用户类型:
- 超级管理员 root
- 普通用户
- 系统用户
- 保证应用程序可正常使用、运行
- 不允许其登录系统,有安全性
用户相关文件
1、/etc/passwd
保存用户信息
root:x:0:0:root:/root:/bin/bash
格式: 用户名:密码占位符:uid:gid:说明:家目录:shell
uid 用户ID
管理员 0
系统用户 1----999
普通用户 1000+
shell 给Linux内核翻译命令
/bin/bash
默认shell, 可正常登录系统、执行命令
/sbin/nologin
不允许登录系统
2、/etc/shadow文件
保存用户的密码
二、用户管理操作
1、创建用户
useradd [选项] 用户名
[[email protected] ~]# useradd user01
常用选项:
-u 指定用户ID
[[email protected] ~]# useradd -u 2022 user02
-s 指定用户的shell
-M 不创建用户的家目录
[[email protected] ~]# useradd -s /sbin/nologin -M nginx
[[email protected] ~]# useradd -u 2001 -s /sbin/nologin -M tomcat
-g 基本组
[[email protected] ~]# groupadd group1
[[email protected] ~]# useradd -g group1 apache
-G 附加组,附加组,附加组
[[email protected] ~]# groupadd nginx
[[email protected] ~]# groupadd httpd
[[email protected] ~]# groupadd tomcat
[[email protected] ~]#
[[email protected] ~]# useradd -G nginx,httpd,tomcat apache //多个附加组用逗号隔开
2、切换用户
su - 用户名
[[email protected] ~]# su - apache
3、设置用户密码
passwd [选项] [用户名]
[[email protected] ~]# passwd //修改当前用户密码
Changing password for user root.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
//修改指定用户的密码, 只有管理员root可成功执行
[[email protected] ~]# passwd user01
-l 锁定用户密码,不能登录系统
[[email protected] ~]# passwd -l user01
Locking password for user user01.
passwd: Success
-u 解锁用户密码
[[email protected] ~]# passwd -u user01
Unlocking password for user user01.
passwd: Success
-e 强制用户密码过期,下次登录必须修改密码
[[email protected] ~]# passwd -e user01
Expiring password for user user01.
passwd: Success
4、查看用户ID信息
[[email protected] ~]# id apache
5、修改用户信息
usermod [选项] 用户名
-u 修改用户ID
[[email protected] ~]# id userA
uid=1003(userA) gid=1003(userA) groups=1003(userA)
[[email protected] ~]#
[[email protected] ~]# usermod -u 3000 userA
[[email protected] ~]# id userA
uid=3000(userA) gid=1003(userA) groups=1003(userA)
-s 修改用户shell
[[email protected] ~]# grep "userB" /etc/passwd
userB:x:1004:1004::/home/userB:/bin/bash
[[email protected] ~]# usermod -s /sbin/nologin userB
[[email protected] ~]# grep "userB" /etc/passwd
userB:x:1004:1004::/home/userB:/sbin/nologin
-G 附加组, 附加组 将用户加入附加组
[[email protected] ~]# id userC
uid=2000(userC) gid=2000(userC) groups=2000(userC)
[[email protected] ~]# groupadd caiwu
[[email protected] ~]# usermod -G caiwu userC
[[email protected] ~]# id userC
uid=2000(userC) gid=2000(userC) groups=2000(userC),2008(caiwu)
[[email protected] ~]# usermod -aG caiwu userC //-a参数是保留用户当前的组,重新添加一个组
[[email protected] ~]# id userC
uid=2000(userC) gid=2000(userC) groups=2000(userC),2008(caiwu),2009(jishu)
-L 锁定用户密码
-U 解锁用户密码
6、删除用户
userdel [选项] 用户名
-r 删除用户的家目录
[[email protected] ~]# userdel -r userA
7、查看用户登录信息
[[email protected] ~]# who
root pts/0 2020-12-17 17:09 (192.168.183.1)
root pts/1 2020-12-17 18:00 (192.168.183.1)
user01 :2 2020-12-17 19:35 (:2)
8、查看失败的登录
[[email protected] ~]# lastb
9、查看所有登录行为
[[email protected] ~]# last
三、用户组管理操作
用户组相关文件:
/etc/group
1、创建用户组
groupadd 组名
2、删除用户组
groupdel 组名
3、从组中删除用户
gpasswd [选项] 用户 组名
[[email protected] ~]# gpasswd -d apache tomcat
Removing user user01 from group tomcat
四、用户相关配置文件
每创建一个用户,系统会自动将/etc/skel目录的文件复制到用户家目录
[[email protected] ~]# ls -a /home/user06/
. .. .bash_logout .bash_profile .bashrc
1、 .bash_profile 定义环境变量
该文件中的命令,会在用户登录系统时自动执行
[[email protected] ~]# vim /home/user01/.bash_profile
echo '自动执行!'
[[email protected] ~]# su - user01
Last login: Thu Dec 17 23:02:59 CST 2020 on pts/6
自动执行!
[[email protected] ~]$ exit
全局文件 /etc/profile
针对系统中所有用户生效
2、 .bashrc 定义命令别名
该文件的命令,会在用户打开新终端时自动执行
全局文件 /etc/bashrc
//查看别名
[[email protected] ~]# alias
删除别名
[[email protected] ~]# unalias cs
为用户user01定义命令别名
[[email protected] ~]# vim /home/user01/.bashrc
alias lc='ls -ldh /etc/'
[[email protected] ~]# su - user01
Last login: Thu Dec 17 23:07:23 CST 2020 on pts/5
自动执行!
[[email protected] ~]$ lc
drwxr-xr-x. 142 root root 8.0K Dec 17 23:15 /etc/
3、 .bash_history
用户登录过系统后,才会出现,记录对应用户执行过的历史命令
history //查看历史命令
history -c //清空历史命令
四、重置root密码
1、重启系统
2、启动到grub菜单界面,按上下键,暂停启动
3、选择gruab菜单中第一项,按字母e编辑系统启动配置文件
4、找到linux16开头的行,在行尾添加rd.break
5、按ctrl + x再次启动,进入救援模式
6、以读写的方式重新挂载根目录
switch_root# mount -o rw,remount /sysroot
7、切换到救援模式根目录
switch_root# chroot /sysroot
8、使用passwd命令重置密码
sh-4.2# passwd root
9、创建隐藏文件
sh-4.2# touch /.autorelabel
10、敲两次exit退出救援模式,系统会正常启动
边栏推荐
猜你喜欢
随机推荐
Building wheel for hdbscan (pyproject.toml) did not run successfully.
BeanShell 内置变量 ctx
UNIX实现IO多路复用之使用select函数实现网络socket服务端
Contains方法,查看序列中是否包含某个元素
Conditional statement of shell
Jmeter上传和下载文件
常用测试用例方法
About the problem of re creation after deleting the module in idea:
UNIX编程—网络socket
配置yum源遇到的问题
Selenium基础知识 调试方法
测试案例:水杯,保温杯,黑板,抽纸,电梯,签到
使用pip使用报错:pip is configured with locations that require TLS/SSL
zstuAcm学生信息库的建立(用链表完成)
Apply collections Sort() implements list sorting
Amber tutorial 3.2: GPU viewing and running MD with pmemd engine
jmeter常见问题
爬取网页动态加载的评论
Other tests: regression test, smoking test, random test
学习amber教程A17:伞形采样,绘制丙氨酸三肽的势能面









