用户相关文件:
/etc/passwd 用户所在文件
/etc/shadow 密码所在文件
useradd oldboy 添加用户实际上就是修改上述两个文件
passwd oldboy 改密码实际上就是修改密码所在文件
用户组相关文件:
/etc/group 用户组所在文件
/etc/gshadow 用户组密码所在文件(废弃状态)
useradd oldboy 添加用户实际上也会修改上述两个文件(因为要创建同名的用户组)
groupadd sa 添加用户组就是修改上述两个文件
md5sum 给文件设置指纹(计算和检查MD5数字信息)
[root@oldboyedu ~]# md5sum /etc/passwd /etc/shadow /etc/group /etc/gshadow
2fe9f002726ed0a138d67cd44722f1a6 /etc/passwd
c2ca41415dca17f1a3dc3c286a9b9bff /etc/shadow
c475144a13d87a400b5e16fe6bd70baf /etc/group
659ef4f533df0fa7e457f87755fb1c27 /etc/gshadow
[root@oldboyedu ~]# useradd bingbing
文件发生变化
[root@oldboyedu ~]# md5sum /etc/passwd /etc/shadow /etc/group /etc/gshadow
39d60eff90ecd0326fe59e6464b464ad /etc/passwd
0aa8d3b157f042876ec0190a2808a377 /etc/shadow
cee8bc6e85f9dfb9dc0a04e452e834f4 /etc/group
cdfd26351f409ee32fca53a22ad15912 /etc/gshadow
[root@oldboyedu ~]# grep bingbing /etc/passwd /etc/shadow /etc/group /etc/gshadow
/etc/passwd:bingbing:x:1004:1006::/home/bingbing:/bin/bash
/etc/shadow:bingbing:!!:17980:0:99999:7:::
/etc/group:bingbing:x:1006:
/etc/gshadow:bingbing:!::
/etc/passwd文件:
Linux是命令行管理,平时输入ls,cp,谁识别,帮我们把我们想要的输出呢?
这个工具就是bash(命令行解释器)。 * ? [abc]
用户登录:输入命令,希望哪个解释器解释(结尾列决定)
/etc/passwd:bingbing:x:1004:1006::/home/bingbing:/bin/bash
CentOS7默认解释器是bash。
/etc/passwd里的解释器
[root@oldboyedu ~]# awk -F ":" '{print $NF}' /etc/passwd|sort|uniq -c
6 /bin/bash
1 /bin/sync
1 /sbin/halt
18 /sbin/nologin
1 /sbin/shutdown
[root@oldboyedu ~]# cat /etc/shells
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash
bash是sh的扩展,sh是bash的软连接。
[root@oldboyedu ~]# ls -l /bin/sh
lrwxrwxrwx. 1 root root 4 Mar 4 11:15 /bin/sh -> bash
/bin/bash /server/scripts/bak.sh
[root@oldboyedu ~]# tail -1 /etc/shadow
bingbing 用户
:!! 密码
:17980 最近更改密码的时间
:0 禁止修改密码的天数
:99999 用户必须更改口令的天数
:7 警告更改密码的期限
: 在用户密码过期之后到禁用账户的天数
: 从1970年1月1日起,到用户被禁用的天数
: 保留
/etc/group
/etc/gshadow
[root@oldboyedu ~]# useradd gongli -u 888 -s /sbin/nologin -M
[root@oldboyedu ~]# tail -1 /etc/passwd
gongli:x:888:1007::/home/gongli:/sbin/nologin
bingbing:x:1004:1006::/home/bingbing:/bin/bash
[root@oldboyedu ~]# useradd gongli1 -c "beautify woman" -d /tmp -e '2020/10/20'
useradd: warning: the home directory already exists.
Not copying any file from skel directory into it.
[root@oldboyedu ~]# tail -1 /etc/passwd
gongli1:x:1005:1008:beautify woman:/tmp:/bin/bash
[root@oldboyedu ~]# chage -l gongli1
Last password change : Mar 25, 2019
Password expires : never
Password inactive : never
Account expires : Oct 20, 2020 =================
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
给开发等人员添加用户,尽量给截止时间。
[root@oldboyedu ~]# useradd gongli2 -g incahome
[root@oldboyedu ~]# id gongli2
uid=1006(gongli2) gid=1004(incahome) groups=1004(incahome)
/etc/default/useradd文件 useradd命令的配置文件
默认shell就是/bin/bash
为什么默认的家目录在home下
为什么默认的家目录/home/用户名下面有很多隐藏文件,从哪来的。
就是/etc/default/useradd文件配置的。
[root@oldboyedu ~]# cat /etc/default/useradd
# useradd defaults file
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes
修改方法:
useradd -D -s /bin/sh 相当于vim编辑/etc/default/useradd文件。
[root@oldboyedu ~]# useradd -D -s /bin/sh
You have new mail in /var/spool/mail/root
[root@oldboyedu ~]# cat /etc/default/useradd
# useradd defaults file
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/sh
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes
[root@oldboyedu ~]# useradd chenglong1
You have new mail in /var/spool/mail/root
[root@oldboyedu ~]# tail -1 /etc/passwd
chenglong1:x:1007:1009::/home/chenglong1:/bin/sh
[root@oldboyedu ~]# chage -l chenglong1
Last password change : Mar 25, 2019
Password expires : never
Password inactive : never
Account expires : Oct 21, 2020
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
/etc/skel目录,创建用户默认就会把此目录下的文件拷贝到/home/用户名下。
/etc/skel 为每个用户提供用户环境变量的目录。
[root@oldboyedu ~]# ls /etc/skel -A
.bash_logout .bash_profile .bashrc
===================================================
.bash_logout .bash_profile .bashrc 用户环境变量
登录系统后,命令行出现如下提示:为什么?
[root@oldboyedu /home/chenglong1]# su - chenglong1
-sh-4.2$
-sh-4.2$
解答:用户的环境变量缺失导致的。
执行如下命令解决
-sh-4.2$ cp /etc/skel/.bash* .
-sh-4.2$ source ./.bash_profile ./.bashrc
[chenglong1@oldboyedu ~]$
[chenglong1@oldboyedu ~]$
-sh-4.2$ export PS1='[\[\e[34;1m\]\u@\[\e[0m\]\[\e[32;1m\]\H\[\e[0m\]\[\e[31;1m\] \w\[\e[0m\]]\$'
[chenglong1@oldboyedu ~]$
添加用户要用的文件
/etc/login.defs 了解
添加用户要用的文件
/etc/login.defs /etc/skel /etc/default/useradd
编辑/etc/passwd,通过注释来删除。
#chenglong1:x:1007:1009::/home/chenglong1:/bin/sh
"/etc/passwd" 30L, 1362C written
[root@oldboyedu ~]# su - chenglong1
su: user chenglong1 does not exist
[root@oldboyedu ~]# useradd -u 9999 -s /bin/sh -M -g sa -c "老男孩" -e "2019/5/1" zongsheng
[root@oldboyedu ~]# tail -1 /etc/passwd
zongsheng:x:9999:1003:老男孩:/home/zongsheng:/bin/sh
[root@oldboyedu ~]# id zongsheng
uid=9999(zongsheng) gid=1003(sa) groups=1003(sa)
[root@oldboyedu ~]# chage -l zongsheng
Last password change : Mar 25, 2019
Password expires : never
Password inactive : never
Account expires : May 01, 2019
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
usermod -u 8888 -s /bin/bash -m /home/zongsheng -g incahome -c "男孩" -e "2020/5/1"
[root@oldboyedu ~]# usermod -u 8888 -s /bin/bash -g incahome -c "男孩" -e "2020/5/1" zongsheng
[root@oldboyedu ~]# chage -l zongsheng
Last password change : Mar 25, 2019
Password expires : never
Password inactive : never
Account expires : May 01, 2020
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
[root@oldboyedu ~]# id zongsheng
uid=8888(zongsheng) gid=1004(incahome) groups=1004(incahome)
[root@oldboyedu ~]# tail -1 /etc/passwd
zongsheng:x:8888:1004:男孩:/home/zongsheng:/bin/bash
You have new mail in /var/spool/mail/root
usermod -u 8888 -s /bin/bash -g incahome -c "男孩" -e "2020/5/1" zongsheng
usermod -m /home/zongsheng zongsheng
不交互设置密码:
方法1:
[root@oldboyedu ~]# echo 123456|passwd --stdin oldgirl
Changing password for user oldgirl.
passwd: all authentication tokens updated successfully.
方法2:
[root@oldboyedu ~]# echo 123456 >pass
[root@oldboyedu ~]# cat pass
123456
[root@oldboyedu ~]# passwd --stdin oldgirl <pass
Changing password for user oldgirl.
passwd: all authentication tokens updated successfully.
[root@oldboyedu ~]# tail -4 /etc/passwd|awk -F ":" '{print $1":oldboy"}' >user.log
[root@oldboyedu ~]# cat user.log
gongli1:oldboy
gongli2:oldboy
chenglong1:oldboy
zongsheng:oldboy
chpasswd对密码文件的要求是上述user.log
方法1:
[root@oldboyedu ~]# chpasswd<user.log
方法2:
[root@oldboyedu ~]# tail -4 /etc/passwd|awk -F ":" '{print $1":oldboy"}'|chpasswd