Linux:用户和组总结
从创建文件说起:useradd xiaomi 这里是创建了xiaomi用户
默认系统还会创建:/home/xiaomi /var/mail/xiaomi 即家目录和邮箱账户,同是以下文件也会增加相关用户和密码信息:
1: /etc/passwd 新增加一行 xiaomi:x:1004:1004::/home/xiaomi:/bin/bash 分别表示:username:passwd:uid:gid:comment:dirctory:shell
2: /etc/shadow 新增加一行 xiaomi:!!:17948:0:99999:7::: 分别表示:username:passwd locked:最近一次修改时间:最短过期时间:最长过期时间:提前7天提醒密码过期:密码到期依然可以用:密码永不过期:保留
3: /etc/group 新增加一行 xiaomi:x:1004: 分别表示:groupname:加密:gid:附加组
4: /etc/gshadow 新增加一行 xiaomi:!:: 分别表示:groupname:加密口令:主组:附加组
一些默认行为:
1,密码的显示问题,可以用pwunconv和pwconv进行切换,默认是pwconv的,显示为x如下图:
2,uid,gid默认60000以内递增(配置文件在/etc/login.defs),默认家目录/home/xiaomi ,默认shell为bash
3,shadow文件存放用户的密码
4,group文件存放了用户的主组和附加组相关信息
5,gshadow存放了组的密码
下面执行一系列操作:
1,创建dalao组密码为dalao 命令:`groupadd dalao -p dalao`
2,创建这些用户属于dalao组:mayun,mahuateng,leijun,liyanhong, 他们的密码都是"big4" 命令:useradd -G dalao mayun echo "big4" |passwd --stdin mayun
3,检查这几个用户的组信息 命令: id, getent group dalao, getent gshadow dalao, groups mayun等等都可以查看
======================================================分割线==================================================================
几个重要的文件:
1,/etc/login.defs 用户登录的默认的配置文件
2,/etc/passwd 用户信息存放位置
3,/etc/shadow 用户密码存放位置
4,/etc/group 组信息存放位置
5,/etc/gshadow 组密码存放位置
6,/etc/shells 系统支持的shells
7,/etc/default/useradd 创建用户名的默认设置
8,/etc/skel 目录包含的默认文件
====================================================分割线====================================================================
主要命令用法:
useradd [options] LOGIN
-u uid
-g gid
-G 指定附加组
-p 设置密码
-s 指定shell
-c 添加备注
-r 表明创建的的是系统用户
-d 指定家目录
usermod 修改用户信息
语法:usermod [options] LOGIN
-a 只能和-G一起使用,将用户追加到一个附加组 例如: usermod -aG alibaba mayun 将mayun添加到alibaba组
其他一些用法与useradd类似 -g -c -d -e:设置过期日期 -f:设置账号到期,几天停用
userdel 删除用户
-f 强制删除用户,即使用户正在登陆状态
-r 只删除家目录,其他该用户的数据需要手工删除
=====================================================分割线==============================================================
groupadd groupmod groupdel 不想写了,直接查看man手册
=====================================================分割线==============================================================
其他相关命令
chown 修改文件所属者
chgrp 修改文件所属组
chsh -s 修改用户shell
chfn 修改用户描述信息
chage 修改用户密码设置
chmem 配置内存
getent 可以查看用户,密码,组和组密码 例子:getent passwd xiaomi
pwconv 密码显示为x
pwunconv 密码显示为加密过的数据展现
grpconv
grpunconv 和上面一个意思
vipw 等于 vi /etc/passwd
vigr 等于 vi /etc/group
pwck 检查密码文件
grpck 检查group文件
groupmems -a mayun -g alibaba 将马云添加到alibaba组
==================
最后来看看/etc/login.defs这个文件的内容,可以看到里面设置了一些默认配置。
1 # 2 # Please note that the parameters in this configuration file control the 3 # behavior of the tools from the shadow-utils component. None of these 4 # tools uses the PAM mechanism, and the utilities that use PAM (such as the 5 # passwd command) should therefore be configured elsewhere. Refer to 6 # /etc/pam.d/system-auth for more information. 7 # 8 9 # *REQUIRED* 10 # Directory where mailboxes reside, _or_ name of file, relative to the 11 # home directory. If you _do_ define both, MAIL_DIR takes precedence. 12 # QMAIL_DIR is for Qmail 13 # 14 #QMAIL_DIR Maildir 15 MAIL_DIR /var/spool/mail 16 #MAIL_FILE .mail 17 18 # Password aging controls: 19 # 20 # PASS_MAX_DAYS Maximum number of days a password may be used. 21 # PASS_MIN_DAYS Minimum number of days allowed between password changes. 22 # PASS_MIN_LEN Minimum acceptable password length. 23 # PASS_WARN_AGE Number of days warning given before a password expires. 24 # 25 PASS_MAX_DAYS 99999 26 PASS_MIN_DAYS 0 27 PASS_MIN_LEN 5 28 PASS_WARN_AGE 7 29 30 # 31 # Min/max values for automatic uid selection in useradd 32 # 33 UID_MIN 1000 34 UID_MAX 60000 35 # System accounts 36 SYS_UID_MIN 201 37 SYS_UID_MAX 999 38 39 # 40 # Min/max values for automatic gid selection in groupadd 41 # 42 GID_MIN 1000 43 GID_MAX 60000 44 # System accounts 45 SYS_GID_MIN 201 46 SYS_GID_MAX 999 47 48 # 49 # If defined, this command is run when removing a user. 50 # It should remove any at/cron/print jobs etc. owned by 51 # the user to be removed (passed as the first argument). 52 # 53 #USERDEL_CMD /usr/sbin/userdel_local 54 55 # 56 # If useradd should create home directories for users by default 57 # On RH systems, we do. This option is overridden with the -m flag on 58 # useradd command line. 59 # 60 CREATE_HOME yes 61 62 # The permission mask is initialized to this value. If not specified, 63 # the permission mask will be initialized to 022. 64 UMASK 077 65 66 # This enables userdel to remove user groups if no members exist. 67 # 68 USERGROUPS_ENAB yes 69 70 # Use SHA512 to encrypt password. 71 ENCRYPT_METHOD SHA512 72