Linux用户管理


一、用户信息管理

1. 登陆后信息

[root@myserver ~]#
[用户名@主机名 当前位置]用户权限

  • ~ 代表当前用户的家目录,当前为root用户,所以~代表/root目录
  • # 表示当前用户是超级管理员

2. 查询用户信息命令

(1) id 命令

查询当前用户详细信息

  1. [root@myserver ~]# id
  2. uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
  • uid 用户id
  • gid 用户(主)组id
  • groups 用户所属组(用户可能不仅仅属于一个组)

(2) whoami 命令

查看当前用户名

  1. [root@myserver ~]# whoami
  2. root

(3) hostname 命令

查看当前主机名

  1. [root@myserver ~]# hostname
  2. myserver

二、用户管理

1. 查

  • head -2 /etc/passwd 查看/etc/passwd文本的前两行
  1. [root@myserver ~]# cat /etc/passwd
  2. root:x:0:0:root:/root:/bin/bash
  3. bin:x:1:1:bin:/bin:/sbin/nologin
  4. -------------------------------------------------------
  5. · 用户名:用户密码:用户id:组id:对用户的描述:用户家目录:所用shell
  6. · root用户的用户id和组id均为0
  • cat /etc/shells 查看系统所有可用的shell解释器
  1. [root@myserver ~]# cat /etc/shells
  2. /bin/sh
  3. /bin/bash
  4. /usr/bin/sh
  5. /usr/bin/bash
  6. -------------------------------------------------------
  7. · /bin/bash是创建用户时默认分给用户的shell解释器
  8. . /sbin/nologin,如果指定这个shell,表示禁止该用户登入系统

2. 增

useradd 添加用户

  • -u 指定用户id
  • -g 指定组id
  • -d 指定用户家目录
  • -M 不创建用户家目录
  • -s 指定shell
  • 不使用以上参数,默认情况下创建用户,会同时产生一个和用户同名的组

默认情况下创建用户

  1. [root@myserver ~]# useradd usera
  2. [root@myserver ~]# id usera
  3. uid=1001(usera) gid=1001(usera) groups=1001(usera)
  4. [root@myserver home]# cd /home
  5. [root@myserver home]# ls
  6. usera xuhui
  7. [root@myserver home]# tail -2 /etc/passwd
  8. xuhui:x:1000:1000:xuhui:/home/xuhui:/bin/bash
  9. usera:x:1001:1001::/home/usera:/bin/bash
  10. -----------------------------------------------------
  11. · tail -2 /etc/passwd 查看/etc/passwd文本的后两行内容

使用参数选项创建用户

  1. [root@myserver ~]# useradd -u 1002 -g 1001 -M -s /sbin/nologin userb
  2. [root@myserver ~]# tail -5 /etc/passwd
  3. rngd:x:976:974:Random Number Generator Daemon:/var/lib/rngd:/sbin/nologin
  4. tcpdump:x:72:72::/:/sbin/nologin
  5. xuhui:x:1000:1000:xuhui:/home/xuhui:/bin/bash
  6. usera:x:1001:1001::/home/usera:/bin/bash
  7. userb:x:1002:1001::/home/userb:/sbin/nologin
  8. [root@myserver ~]# cd /home
  9. [root@myserver home]# ls
  10. usera xuhui
  11. -----------------------------------------------------------------------------------
  12. · 虽然userb显示家目录在/home/userb,但因为创建时使用了-M参数,所以并没有创建
  13. -----------------------------------------------------------------------------------
  14. [root@myserver home]# useradd -u 1003 -g 1001 -d /opt/userc -s /bin/bash userc
  15. [root@myserver home]# tail -5 /etc/passwd
  16. tcpdump:x:72:72::/:/sbin/nologin
  17. xuhui:x:1000:1000:xuhui:/home/xuhui:/bin/bash
  18. usera:x:1001:1001::/home/usera:/bin/bash
  19. userb:x:1002:1001::/home/userb:/sbin/nologin
  20. userc:x:1003:1001::/opt/userc:/bin/bash
  21. [root@myserver home]# cd /opt
  22. [root@myserver opt]# ls
  23. userc
  24. ----------------------------------------------------------------------------------
  25. · 通过-d参数指定userc家目录为/opt/userc,所以userc目录创建在/opt

3. 切换

  • su 切换用户
  1. [root@myserver ~]# tail -5 /etc/passwd
  2. tcpdump:x:72:72::/:/sbin/nologin
  3. xuhui:x:1000:1000:xuhui:/home/xuhui:/bin/bash
  4. usera:x:1001:1001::/home/usera:/bin/bash
  5. userb:x:1002:1001::/home/userb:/sbin/nologin
  6. userc:x:1003:1001::/opt/userc:/bin/bash
  7. [root@myserver ~]# su usera
  8. [usera@myserver root]$ cd /home
  9. [usera@myserver home]$ ls
  10. usera xuhui
  11. [usera@myserver home]$ exit
  12. exit
  13. [root@myserver ~]#
  14. -------------------------------------------------------------------------
  15. · su usera 切换到usera用户,但并不改变当前环境变量
  16. · exit 退出当前用户模式
  17. · # 表示当前用户是超级管理员
  18. · $ 表示当前用户是普通用户
  • su - 切换用户
  1. [root@myserver ~]# tail -5 /etc/passwd
  2. tcpdump:x:72:72::/:/sbin/nologin
  3. xuhui:x:1000:1000:xuhui:/home/xuhui:/bin/bash
  4. usera:x:1001:1001::/home/usera:/bin/bash
  5. userb:x:1002:1001::/home/userb:/sbin/nologin
  6. userc:x:1003:1001::/opt/userc:/bin/bash
  7. [root@myserver ~]# su - userb
  8. su: warning: cannot change directory to /home/userb: No such file or directory
  9. This account is currently not available.
  10. [root@myserver ~]# su - userc
  11. [userc@myserver ~]$ pwd
  12. /opt/userc
  13. [userc@myserver ~]$ exit
  14. logout
  15. ------------------------------------------------------------------------
  16. · su - userb 无法切换,因为我们给userb设置的shell是/sbin/nologin,禁止userb登入
  17. · su - userc 切换到userc用户,同时切换到userc的环境变量

4. 删

userdel 删除用户

  • userdel usera 仅删除用户,但用户的home目录数据仍存在
  1. [root@myserver ~]# tail -3 /etc/passwd
  2. usera:x:1001:1001::/home/usera:/bin/bash
  3. userb:x:1002:1001::/home/userb:/sbin/nologin
  4. userc:x:1003:1001::/opt/userc:/bin/bash
  5. [root@myserver ~]# userdel userc
  6. [root@myserver ~]# cd /opt
  7. [root@myserver opt]# ls
  8. userc
  9. [root@myserver opt]# tail -3 /etc/passwd
  10. xuhui:x:1000:1000:xuhui:/home/xuhui:/bin/bash
  11. usera:x:1001:1001::/home/usera:/bin/bash
  12. userb:x:1002:1001::/home/userb:/sbin/nologin
  • userdel -r userb 将用户和其对应的家目录全部删掉
  1. [root@myserver ~]# tail -3 /etc/passwd
  2. xuhui:x:1000:1000:xuhui:/home/xuhui:/bin/bash
  3. usera:x:1001:1001::/home/usera:/bin/bash
  4. userb:x:1002:1001::/home/userb:/sbin/nologin
  5. [root@myserver ~]# cd /home
  6. [root@myserver home]# ls
  7. usera xuhui
  8. [root@myserver home]# userdel -r usera
  9. userdel: group usera is the primary group of another user and is not removed.
  10. [root@myserver home]# ls
  11. xuhui
  12. [root@myserver home]# tail -3 /etc/passwd
  13. tcpdump:x:72:72::/:/sbin/nologin
  14. xuhui:x:1000:1000:xuhui:/home/xuhui:/bin/bash
  15. userb:x:1002:1001::/home/userb:/sbin/nologin
  16. [root@myserver home]# id userb
  17. uid=1002(userb) gid=1001(usera) groups=1001(usera)
  18. --------------------------------------------------------------------------------
  19. · 因为userb未删除,且在usera这个组中,所以在删除usera用户后会给出提示信息

5. 改

usermod 修改用户属性

  • -d 修改家目录
  1. [root@myserver ~]# tail -3 /etc/passwd
  2. tcpdump:x:72:72::/:/sbin/nologin
  3. xuhui:x:1000:1000:xuhui:/home/xuhui:/bin/bash
  4. usera:x:1001:1001::/home/usera:/bin/bash
  5. [root@myserver ~]# usermod -d /opt/usera usera
  6. [root@myserver ~]# tail -3 /etc/passwd
  7. tcpdump:x:72:72::/:/sbin/nologin
  8. xuhui:x:1000:1000:xuhui:/home/xuhui:/bin/bash
  9. usera:x:1001:1001::/opt/usera:/bin/bash
  • -g 修改group
  1. [root@myserver opt]# tail -3 /etc/passwd
  2. tcpdump:x:72:72::/:/sbin/nologin
  3. xuhui:x:1000:1000:xuhui:/home/xuhui:/bin/bash
  4. usera:x:1001:1001::/opt/usera:/bin/bash
  5. [root@myserver opt]# usermod -g 1000 usera
  6. [root@myserver opt]# tail -3 /etc/passwd
  7. tcpdump:x:72:72::/:/sbin/nologin
  8. xuhui:x:1000:1000:xuhui:/home/xuhui:/bin/bash
  9. usera:x:1001:1000::/opt/usera:/bin/bash
  10. [root@myserver opt]# id usera
  11. uid=1001(usera) gid=1000(xuhui) groups=1000(xuhui)
  12. -----------------------------------------------------------------------
  13. · 注意:-g 后面加的gid应该存在
  • -s 修改shell
  1. [root@myserver opt]# tail -3 /etc/passwd
  2. xuhui:x:1000:1000:xuhui:/home/xuhui:/bin/bash
  3. usera:x:1001:1000::/opt/usera:/bin/bash
  4. userb:x:1002:1002::/home/userb:/sbin/nologin
  5. [root@myserver opt]# su - userb
  6. This account is currently not available.
  7. [root@myserver opt]# usermod -s /bin/bash userb
  8. [root@myserver opt]# tail -3 /etc/passwd
  9. xuhui:x:1000:1000:xuhui:/home/xuhui:/bin/bash
  10. usera:x:1001:1000::/opt/usera:/bin/bash
  11. userb:x:1002:1002::/home/userb:/bin/bash
  12. [root@myserver opt]# su - userb
  13. [userb@myserver ~]$

三、用户密码设置

passwd 修改用户密码

  1. [root@myserver ~]# passwd
  2. Changing password for user root.
  3. New password:
  4. BAD PASSWORD: The password fails the dictionary check - it is too simplistic/systematic
  5. Retype new password:
  6. passwd: all authentication tokens updated successfully.
  7. [root@myserver ~]#

四、文件归属管理

1. 用户分类

  • 超级管理员
  • 普通用户
  • 系统用户

2. 文件归属更改

chown 修改文件和目录归属

  1. [root@myserver ~]# tail -3 /etc/passwd
  2. tcpdump:x:72:72::/:/sbin/nologin
  3. xuhui:x:1000:1000:xuhui:/home/xuhui:/bin/bash
  4. usera:x:1001:1001::/home/usera:/bin/bash
  5. [root@myserver ~]# ls
  6. anaconda-ks.cfg Documents initial-setup-ks.cfg myTest.txt Public Videos
  7. Desktop Downloads Music Pictures Templates
  8. [root@myserver ~]# ll myTest.txt
  9. -rw-r--r--. 1 root root 123 Jul 25 02:11 myTest.txt
  10. [root@myserver ~]# chown usera:usera myTest.txt
  11. [root@myserver ~]# ll myTest.txt
  12. -rw-r--r--. 1 usera usera 123 Jul 25 02:11 myTest.txt
  13. [root@myserver ~]# chown root myTest.txt
  14. [root@myserver ~]# ll myTest.txt
  15. -rw-r--r--. 1 root usera 123 Jul 25 02:11 myTest.txt
  16. [root@myserver ~]# chown :root myTest.txt
  17. [root@myserver ~]# ll myTest.txt
  18. -rw-r--r--. 1 root root 123 Jul 25 02:11 myTest.txt
  19. [root@myserver ~]#
  20. ---------------------------------------------------------------------------------
  21. · chown usera:usera myTest.txt myTest.txt的用户归属和组归属均改为usera
  22. · chown root myTest.txt myTest.txt的用户归属改为root
  23. · chown :root myTest.txt myTest.txt的组归属改为root

五、group组管理

  • /etc/group 组管理配置文件
  1. [root@myserver ~]# tail -3 /etc/group
  2. tcpdump:x:72:
  3. xuhui:x:1000:
  4. usera:x:1001:
  • groupadd 添加组
  1. [root@myserver ~]# tail -3 /etc/group
  2. tcpdump:x:72:
  3. xuhui:x:1000:
  4. usera:x:1001:
  5. [root@myserver ~]# groupadd userb
  6. [root@myserver ~]# tail -3 /etc/group
  7. xuhui:x:1000:
  8. usera:x:1001:
  9. userb:x:1002:
  10. [root@myserver ~]#
  • groupdel 删除组
  1. [root@myserver ~]# tail -3 /etc/group
  2. xuhui:x:1000:
  3. usera:x:1001:
  4. userb:x:1002:
  5. [root@myserver ~]# groupdel userb
  6. [root@myserver ~]# tail -3 /etc/group
  7. tcpdump:x:72:
  8. xuhui:x:1000:
  9. usera:x:1001:
  10. [root@myserver ~]#