Linux系统管理——用户操作

一、用户相关介绍

多用户多任务操作系统

1、用户类型

  • 管理员 root , uid=0
  • 普通用户
  • 系统用户, 保证某个软件/应用可正常运行; uid=1–999

2、用户相关文件

  • /etc/passwd 用户信息
// 统计用户数
[root@martin-host ~]# wc -l /etc/passwd | awk '{print $1}'
48
// 文件格式 
用户名:x:uid:gid:说明信息:家目录:shell

操作系统使用uid区分不同的用户
用户组:基本组(一个)、附加组(多个)
shell:
/bin/bash:默认shell, 可正常登录系统、执行操作
/sbin/nologin: 不允许与系统进行交互 
  • /etc/shadow 用户密码信息

  • /etc/group 用户组信息

二、用户操作

1、创建用户

# useradd [选项] 用户名 
  • -s 指定用户shell, 默认/bin/bash
[root@martin-host ~]# useradd -s /sbin/nologin www 
[root@martin-host ~]# 
[root@martin-host ~]# grep "www" /etc/passwd 
www:x:1004:1004::/home/www:/sbin/nologin
  • -M 不创建家目录, 系统用户
[root@martin-host ~]# useradd -s /sbin/nologin -M docker
  • -r 创建系统用户
[root@martin-host ~]# useradd -r -s /sbin/nologin -M redis
[root@martin-host ~]# 
[root@martin-host ~]# tail -n 1 /etc/passwd 
redis:x:986:980::/home/redis:/sbin/nologin
[root@martin-host ~]# 
[root@martin-host ~]# ls /home/
AAA  BBB  demon  martin  www
[root@martin-host ~]# 
  • -G 指定用户的附加组
[root@martin-host ~]# groupadd IT
[root@martin-host ~]# useradd -G IT tome 
[root@martin-host ~]# 
[root@martin-host ~]# id tome 
uid=1006(tome) gid=1007(tome)=1007(tome),1006(IT)
[root@martin-host ~]# 
[root@martin-host ~]# grep -i "it" /etc/group 
polkitd:x:998:
rtkit:x:172:
gnome-initial-setup:x:982:
IT:x:1006:tome
[root@martin-host ~]# 
[root@martin-host ~]# useradd demon

[root@martin-host ~]# tail -n 1 /etc/passwd 
demon:x:1003:1003::/home/demon:/bin/bash
[root@martin-host ~]# 
[root@martin-host ~]# tail -n 1 /etc/shadow
demon:!!:20004:0:99999:7:::
[root@martin-host ~]# 
[root@martin-host ~]# tail -n 1 /etc/group 
demon:x:1003:
[root@martin-host ~]# ls /home/
AAA  BBB  demon  martin
[root@martin-host ~]# 
[root@martin-host ~]# ls /var/spool/mail/
AAA  BBB  demon  martin  root  rpc

切换用户

[root@martin-host ~]# su - demon
[demon@martin-host ~]$ 
[demon@martin-host ~]$ 
[demon@martin-host ~]$ exit
登出

查看用户的登录信息

[root@martin-host ~]# who 
root     :0           2024-10-08 09:05 (:0)
root     pts/0        2024-10-08 09:06 (192.168.140.1)
root     pts/1        2024-10-08 10:48 (192.168.140.1)

查看用户ID

[root@martin-host ~]# id martin 
uid=1000(martin) gid=1000(martin)=1000(martin)

2、修改用户密码

# passwd 用户名 

注意: 普通用户只能修改自己的密码
[root@martin-host ~]# whoami 
root
[root@martin-host ~]# 
[root@martin-host ~]# passwd
更改用户 root 的密码 。
新的 密码:
无效的密码: 密码少于 8 个字符
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
[root@martin-host ~]# 
[root@martin-host ~]# passwd demon
更改用户 demon 的密码 。
新的 密码:
无效的密码: 密码少于 8 个字符
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
[root@martin-host ~]# su - martin
上一次登录:六 928 15:10:47 CST 2024pts/0 上
[martin@martin-host ~]$ 
[martin@martin-host ~]$ 
[martin@martin-host ~]$ passwd 
更改用户 martin 的密码 。
为 martin 更改 STRESS 密码。
(当前)UNIX 密码:
新的 密码:
无效的密码: 密码少于 8 个字符
新的 密码:
无效的密码: 密码少于 8 个字符
新的 密码:
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
[martin@martin-host ~]$ 
  • -l 锁定用户
[root@martin-host ~]# passwd -l demon
锁定用户 demon 的密码 。
passwd: 操作成功
  • -u 解锁用户
[root@martin-host ~]# passwd -u demon
解锁用户 demon 的密码。
passwd: 操作成功

3、修改用户信息

# usermod [选项] 用户名
  • -s 修改用户shell
[root@martin-host ~]# grep "redis" /etc/passwd
redis:x:986:980::/home/redis:/sbin/nologin
[root@martin-host ~]# 
[root@martin-host ~]# usermod -s /bin/bash redis
[root@martin-host ~]# 
[root@martin-host ~]# grep "redis" /etc/passwd 
redis:x:986:980::/home/redis:/bin/bash
[root@martin-host ~]# 
  • -G 组名, 修改附加组
[root@martin-host ~]# id tome
uid=1006(tome) gid=1007(tome)=1007(tome),1006(IT)
[root@martin-host ~]# 
[root@martin-host ~]# groupadd jishu
[root@martin-host ~]# usermod -G jishu tome
[root@martin-host ~]# 
[root@martin-host ~]# id tome 
uid=1006(tome) gid=1007(tome)=1007(tome),3001(jishu)
// -aG, 加入多个组
[root@martin-host ~]# id tome 
uid=1006(tome) gid=1007(tome)=1007(tome),1006(IT)
[root@martin-host ~]# 
[root@martin-host ~]# usermod -aG jishu tome 
[root@martin-host ~]# 
[root@martin-host ~]# id tome 
uid=1006(tome) gid=1007(tome)=1007(tome),1006(IT),3001(jishu)

4、删除用户

[root@martin-host ~]# userdel -r pg 

-r: 同时删除用户相关的文件 

三、用户组操作

1、创建用户组

[root@martin-host ~]# groupadd caiwu

2、删除用户组

[root@martin-host ~]# groupdel caiwu

3、将用户加入组

[root@martin-host ~]# grep "IT" /etc/group
IT:x:1006:tome

[root@martin-host ~]# gpasswd -a martin IT
正在将用户“martin”加入到“IT”组中
[root@martin-host ~]# gpasswd -a demon IT
正在将用户“demon”加入到“IT”组中

[root@martin-host ~]# grep "IT" /etc/group
IT:x:1006:tome,martin,demon
[root@martin-host ~]# 

4、从组中踢出用户

[root@martin-host ~]# grep "IT" /etc/group
IT:x:1006:tome,martin,demon
[root@martin-host ~]# 
[root@martin-host ~]# gpasswd -d tome IT
正在将用户“tome”从“IT”组中删除
[root@martin-host ~]# 
[root@martin-host ~]# grep "IT" /etc/group
IT:x:1006:martin,demon
[root@martin-host ~]# 

四、用户配置文件

1、配置文件

[root@martin-host ~]# ls -a /home/martin/
.  ..  .bash_history  .bash_logout  .bash_profile  .bashrc 

新建用户时,系统会自动从/etc/skel目录下复制相关的配置文件到用户的家目录

[root@martin-host ~]# touch /etc/skel/README
[root@martin-host ~]# ls /etc/skel/ -a
.  ..  .bash_logout  .bash_profile  .bashrc  .mozilla  README

[root@martin-host ~]# useradd a2
[root@martin-host ~]# ls -a /home/a2/
.  ..  .bash_logout  .bash_profile  .bashrc  .mozilla  README

2、定义命令别名

  • /etc/bashrc
  • ~/.bashrc
    本质上就是脚本文件,会在打开终端时,自动执行

3、定义环境变量

[martin@martin-host ~]$ tail -n 2 .bash_profile 
export HISTSIZE=5
export HISTTIMEFORMAT="%F_%T   "

[martin@martin-host ~]$ source .bash_profile

~/.bash_profile:针对当前用户生效
/etc/profile:针对所有用户生效

五、重置root密码

1、重启系统,在启动菜单项按上、下键停住
2、选择菜单中的第一项,按字母e进入编辑模式
3、找到linux16开头的行,在行尾添加rd.break
4、按ctrl + x重新启动,进入救援模式
5、以读写的方式重新挂载根目录

switch_root:# mount -o remount,rw /sysroot
switch_root:# chroot /sysroot

6、重置root密码

sh-4.2# echo "redhat" | passwd --stdin root

// selinux没有关闭的情况下,需要创建.autorelabel隐藏文件;selinux关闭后,可忽略 
sh-4.2# touch /.autorelabel

7、敲两次exit退出,重启测试使用新密码登录系统

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值