自学内容网 自学内容网

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 ~]# 

原文地址:https://blog.csdn.net/u010198709/article/details/142751803

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!