Linux - 用户与组 - 重要文件-密码复杂度验证脚本

目录

密码复杂度验证脚本

用户与组练习:

/etc/passwd文件

各个字段的含义:

密码占位符 x 的 作用

用户的shell类型:

passwd备份文件 passwd- 

/etc/shadow文件

各个字段的含义:

备份文件 shadow-

/etc/group

有效组

gpasswd命令

用户与组相关的文件:

/etc/login.defs文件

/etc/skel/*文件

批量新建用户和设置密码脚本


练习:

密码复杂度验证脚本

 脚本实现功能:

        批量新建用户,

        验证密码复杂度

难点:

1.如何知道密码长度?

wc -L

统计一行的最大长度

[root@localhost lianxi]# echo abc1234 |wc -L

7

2.密码里面必须包含大小写,数字,特殊字符

使用正则表达式:

[0-9] 表示数字0-9

[a-z] 表示小写字母 a-z

[A-Z] 表示大写字母A-Z

[0-Z] 表示数字和字母

[^0-Z] 表示特殊字符

[root@localhost lianxi]# echo abc123_@#15/>=+ | grep -E [^0-Z]

abc123_@#15/>=+

实现:

if echo $user_passwd | egrep [0-9] | egrep [a-z] |egrep [A-Z] | egrep [^0-Z]

先判断有没有数字,然后判断有没小写字母,然后判断是否有大写字母,最后判断是不是有特殊字符

#! /bin/bash


# 判断密码复杂度,
passwd_chick(){

	if echo $user_passwd | egrep "[0-9]" | egrep "[a-z]" |egrep "[A-Z]" | egrep "[^0-Z]" 
	then
		echo "密码设置正确,包含大小写,数字,特殊字符!"
	else
		echo "密码复杂度不够!"
		echo "密码必须同时包含大小写,数字,特殊字符!"
		exit
	fi
}

# 密码长度检测
passwd_lenth(){

	if (( $(echo $user_passwd | wc -L) >8 ))
	then
		echo "密码长度符合要求!"		
	else
		echo "您的密码长度不足8位!"
		exit
	fi

}

echo "###########start##############"
echo "欢迎进入用户新建密码检测系统!"

read -p "请输入用户名:" username
if id $username &>/dev/null
then
	echo "此用户已经存在!"
	exit
else
	
	read -p "请输入您的密码:" user_passwd
	passwd_lenth
	passwd_chick
	read -p "请输入您要新建用户的数量:" user_num
	# 如果用户数量为1,直接新建
	if [ $user_num -eq 1 ]
	then 
		useradd $username
		echo $user_passwd | passwd $username --stdin &>/dev/null
		echo "用户$username 新建成功!"
		echo "密码设置成功!"	
	else
		# 批量新建用户
		for i in $(seq $user_num)
		do
			useradd $username$i
			echo $user_passwd | passwd $username$i --stdin &>/dev/null
			echo "用户$username$i新建成功!"
			echo "密码设置成功!"
				
		done
	fi	
fi
echo "###########end##############"

###################################

用户与组练习:

建立用户,删除用户,新建组,修改用户信息

1.新建三个组nongfa,gongda,wenli

# group只能一个个的新建,不能一次建多个

groupadd nongda

group gongda

group wenli

2.新建用户chenli 指定家目录在/home/yueyang shell 为/sbin/nologin uid为7788 属于wenli组 注释为 sanchuangstudent

[root@localhost lianxi2]# useradd -d /home/yueyang -s /sbin/nologin -u 7788 -g wenli -c sanchuang student chenli

3.新建用户huangtao,指定家目录在/home/changde shell为/bin/bash uid为7790 属于nongda组,注释为 sanchuang student

[root@localhost lianxi2]# useradd -d /home/changde -s /sbin/nologin -u 7789 -g nongda -c sanchuang student huangtao

4.新建用户zhangcz 指定家目录在/home/zhuhai shell为/bin/bash uid为7790 属于gongda 注释为 sanchuang student

[root@localhost lianxi2]# useradd -d /home/zhuhai -s /bin/sh -u 7790 -g gongda -c sanchuang student zhangcz

5.给chenli设置密码为123456abc

[root@localhost lianxi2]# echo 123456abc | passwd chenli --stdin

更改用户 chenli 的密码 。

passwd:所有的身份验证令牌已经成功更新。

6.查看chenli用户的信息

[root@localhost lianxi2]# cat /etc/passwd | grep chenli

chenli:x:7788:1034:sanchuang student:/home/yueyang:/sbin/nologin

7.修改zhangcz的shell为/sbin/nologin

[root@localhost lianxi2]# usermod -s /sbin/nologin zhangcz

[root@localhost lianxi2]# cat /etc/passwd | grep zhangcz

zhangcz:x:7790:1033:sanchuang student:/home/zhuhai:/sbin/nologin

8.修改huangtao的uid为9900,组为gongda

[root@localhost lianxi2]# usermod -u 9900 huangtao

[root@localhost lianxi2]# id huangtao

uid=9900(huangtao) gid=1032(nongda) 组=1032(nongda)

9.删除用户zhangcz,连家目录和邮箱都删除

[root@localhost lianxi2]# userdel -r zhangcz

10,删除gongda组,验证是否可以成功删除?

不能,group只能删除空的组,如果里面有人的话是不能删除的

如果想删除需要将里面用户的组换到别的组

##################################################

/etc/passwd文件

passwd文件是用来保存用户信息的文件,一行一个用户

passwd文件位置:/etc/passwd

[root@localhost /]# cat /etc/passwd | tail -1
liming:x:1023:1023::/home/liming:/bin/bash

 ##################################################

各个字段的含义:

字段一:liming : 用户账号的名称

字段二:x : 用户密码字串或者密码占位符“x”

字段三:1023: 表示用户的uid

字段四:1023:表示所属基本组账号的GID

字段五: 用户描述信息

字段六:用户账号的家目录所在

字段七: 登录的shell信息 不指定的话,就是bash

##################################################

密码占位符 x 的 作用

/etc/passwd文件里面的 x密码占位符作用:

如果有x就表示去检查 /etc/shadow文件的作用

如果没有x,就直接登陆,不需要密码验证

这种方式只能是本地登录,远程登录不行

##################################################

用户的shell类型:

/sbin/nologin --》 这个用户不能登录系统

/sbin/halt:--》一登录就会关机

/sbin/sync: --》sync是将内存里的缓存刷新到磁盘里面的

/bin/bash:--》默认的用户shell --》正常的shell

##################################################

passwd备份文件 passwd- 

/etc/passwd :用来存放用户信息的

/etc/passwd- 是备份文件

[root@localhost home]# ls /etc/passwd*

/etc/passwd /etc/passwd- --》passwd的备份文件

他们两的备份文件和原文件都有差异,

备份文件比原文件总是少一次操作,也许这次操作是增加用户,也许是删除了用户,

[root@localhost home]# diff /etc/passwd /etc/passwd-

33a34

wangyi:x:1012:1012::/home/wangyi:/bin/bash

##################################################

例:

查找出linux系统里面的用户uid大于1000的用户,显示出它的uid,家目录,shell信息

[root@localhost /]# cat /etc/passwd | awk -F : '$3>1000{print $1,$3,$6,$7}'
chenhang 1001 /home/chenhang /bin/bash
zhnagjian 1002 /home/zhnagjian /bin/bash
zhangjian 1003 /home/zhangjian /bin/bash
wangshenghu 1004 /home/wangshenghu /bin/bash
yalin 1005 /home/yalin /bin/bash
lilanqing 1006 /home/lilanqing /bin/bash
califeng 1007 /home/califeng /bin/bash
cali123 1008 /home/cali123 /bin/bash
shimengmeng 1010 /home/shimengmeng /bin/bash
zhouyiwei 1011 /home/zhouyiwei /bin/bash
zhaojunjie 1013 /home/zhaojunjie /bin/bash
kimi 1014 /home/kimi /bin/bash
linhucong 1015 /huashan /bin/bash
kimi3 1016 /home/kimi3 /bin/bash
feng1 1017 /home/feng1 /bin/bash
feng2 1018 /home/feng2 /bin/bash
feng3 1019 /home/feng3 /bin/bash
feng4 1020 /home/feng4 /bin/bash
feng5 1021 /home/feng5 /bin/bash
liudehua 1022 /home/liudehua /bin/bash
liming 1023 /home/liming /bin/bash

###########################################################

/etc/shadow文件

用于保存密码字串,密码有效期等信息

文件位置:/etc/shadow

每一行对应一个用户的密码记录

[root@localhost lianxi2]# cat /etc/shadow | tail -2
chenli:$6$OvbWT2jI$T3A7XPRj5jxSnDGQQQujuP3NpfpFCpUrv035RwPzlUphhSS/RHsrCTOoBEHCeFRfNBUgpaW8Lf0g0Vnlzi7Ka0:19061:0:99999:7:::
huangtao:!!:19061:0:99999:7:::

################################################## 

各个字段的含义:

字段1:用户账号的名称

字段2:加密的密码字串信息

        密码字段为 * 或者 !! 说明用户没有设置密码

        密码字段为!开头的话,说明用户密码被锁定

        被禁用的用户是不能登录系统的

        usermod禁用用户的时候,在shadow文件里的密码字段加一个!

        passwd禁用用户的时候,在shadow文件里的密码字段加两个!

字段3:上次修改密码的时间

字段4:密码最短有效天数,默认值为0

字段5:密码最长有效天数,默认值为99999

字段6:提前多少天警告用户口令将过期,默认值为7

字段7:在密码过期之后多少天禁用此用户

字段8:账号失效时间,默认值为空

字段9:保留字段(未使用)

##################################################

备份文件 shadow-

/etc/shadow : 用来存放用户密码的

/etc/passwd- 和 /etc/shadow- 是备份文件

[root@localhost home]# ls /etc/shadow*

/etc/shadow /etc/shadow- --》shadow的备份文件

备份文件比原文件总是少一次操作,也许这次操作是增加用户,也许是删除了用户,

[root@localhost home]# diff /etc/shadow /etc/shadow-
33a34
wangyi:!!:19022:0:99999:7:::

#############################################

/etc/group

组账号:

主要组(私有组)

次要组(附属组)

GiD :组标识号

linux里面一切皆文件,我们可以直接编辑/etc/group文件来修改用户的组名

[root@localhost script]# cat /etc/group | tail -2

user19:x:9919:

user20:x:9920:

 ##################################################

有效组

什么叫有效组?

新建文件夹的时候使用的gid

gid使用哪个组,哪个就是有效组

因为一个用户可以同时属于多个组,但是用户只能有一个有效组,--》对应gid的对应组

################################################## 

gpasswd命令

(用的很少,了解一下)

gpasswd是一个可以将用户加入到某个组的命令

-a 添加 add

-d 删除 delete

-M 批量添加

##################################################

用户与组相关的文件:

/etc/login.defs文件

对账号初始的属性设置

设置普通用户的uid 和gid范围等

定义邮箱

密码的日期,长度等

是否创建家目录

密码的加密算法

删除用户的时候,如果对应的组里面没有成员了,将这个组也删除

[root@localhost user01]# cat /etc/login.defs 
#
# Please note that the parameters in this configuration file control the
# behavior of the tools from the shadow-utils component. None of these
# tools uses the PAM mechanism, and the utilities that use PAM (such as the
# passwd command) should therefore be configured elsewhere. Refer to
# /etc/pam.d/system-auth for more information.
#

# *REQUIRED*
#   Directory where mailboxes reside, _or_ name of file, relative to the
#   home directory.  If you _do_ define both, MAIL_DIR takes precedence.
#   QMAIL_DIR is for Qmail
#
#QMAIL_DIR	Maildir
MAIL_DIR	/var/spool/mail
#MAIL_FILE	.mail

# Password aging controls:
#
#	PASS_MAX_DAYS	Maximum number of days a password may be used.
#	PASS_MIN_DAYS	Minimum number of days allowed between password changes.
#	PASS_MIN_LEN	Minimum acceptable password length.
#	PASS_WARN_AGE	Number of days warning given before a password expires.
#
PASS_MAX_DAYS	99999
PASS_MIN_DAYS	0
PASS_MIN_LEN	5
PASS_WARN_AGE	7

#
# Min/max values for automatic uid selection in useradd
#
UID_MIN                  1000
UID_MAX                 60000
# System accounts
SYS_UID_MIN               201
SYS_UID_MAX               999

#
# Min/max values for automatic gid selection in groupadd
#
GID_MIN                  1000
GID_MAX                 60000
# System accounts
SYS_GID_MIN               201
SYS_GID_MAX               999

#
# If defined, this command is run when removing a user.
# It should remove any at/cron/print jobs etc. owned by
# the user to be removed (passed as the first argument).
#
#USERDEL_CMD	/usr/sbin/userdel_local

#
# If useradd should create home directories for users by default
# On RH systems, we do. This option is overridden with the -m flag on
# useradd command line.
#
CREATE_HOME	yes

# The permission mask is initialized to this value. If not specified, 
# the permission mask will be initialized to 022.
UMASK           077

# This enables userdel to remove user groups if no members exist.
#
USERGROUPS_ENAB yes

# Use SHA512 to encrypt password.
ENCRYPT_METHOD SHA512

 ##################################################

/etc/skel/*文件

新建用户账号时,复制到用户宿主目录中

进入一个新建用户的家目录就可以看到这些隐藏文件

主要控制用户初始配置文件

.bash_profile:用户每次登陆时候执行

.bashrc:每次进入新的bash环境时执行

.bash_logout :用户每次退出登陆时候执行

.bash_history:记录存放上次注销前使用的历史命令

[root@localhost home]# cd user01

[root@localhost user01]# ls -a

. .. .bash_logout .bash_profile .bashrc

################################################## 

批量新建用户和设置密码脚本

写一个脚本,实现批量添加20个用户,用户名为user01-20,密码为user后面跟5个随机字符,

将用户名和密码保存到一个文件里面,文件为/lianxi/username_passwd.txt

需求分析:

1.如何在shell里面产生5个随机的字符串

md5sum cut -c 1-5

2.如何保存用户名和密码到文件里面

拼接,重定向

#!/bin/bash
#新建用户函数
user_add(){
useradd user$i
echo 完成新建用户user$i
}
#给用户设置密码
passwd_add(){
# 使用md5sum 和 cut 随机生成5个字符作为密码
a=$(echo user$i | md5sum | cut -c 1-5 )
echo user$i$a | passwd user$i --stdin &>/dev/null
echo 完成设置密码 
}
echo  "####################"
for i in $(seq -w 20)
do
# 判断用户是否存在
if id user$i &>/dev/null;then
echo user$i exits!
else
user_add
passwd_add
echo user$i:user$i$a >> /lianxi/username_passwd.txt
fi
done
echo  "####################"

实现效果:

####################
完成新建用户user01
完成设置密码 
完成新建用户user02
完成设置密码 
完成新建用户user03
完成设置密码 
完成新建用户user04
完成设置密码 
完成新建用户user05
完成设置密码 
完成新建用户user06
完成设置密码 
完成新建用户user07
完成设置密码 
完成新建用户user08
完成设置密码 
完成新建用户user09
完成设置密码 
完成新建用户user10
完成设置密码 
完成新建用户user11
完成设置密码 
完成新建用户user12
完成设置密码 
完成新建用户user13
完成设置密码 
完成新建用户user14
完成设置密码 
完成新建用户user15
完成设置密码 
完成新建用户user16
完成设置密码 
完成新建用户user17
完成设置密码 
完成新建用户user18
完成设置密码 
完成新建用户user19
完成设置密码 
完成新建用户user20
完成设置密码 
####################
[root@localhost script]# cat /lianxi/username_passwd.txt 
user01:user01e3d44
user02:user02cdc4b
user03:user03e90e8
user04:user047f000
user05:user051f687
user06:user060c6ef
user07:user073035f
user08:user08816ad
user09:user0950556
user10:user101110d
user11:user111c0b7
user12:user12cf91d
user13:user13baef6
user14:user14a2a72
user15:user15b93c5
user16:user16cf2eb
user17:user177aa24
user18:user1845732
user19:user19d9ef9
user20:user207b161

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值