Shell脚本实现编程管理Linux用户和组脚本,编写思路如下:
(1)脚本支持创建普通用户。
(2)支持创建多个用户或者列表用户添加。
(3)支持Linux系统用户删除。
(4)支持Linux系统组删除。
(5)支持对某个用户修改密码。
相关代码如下
bash
#!/bin/bash
#2024年4月30日
#auto manager linux user
########################
# 以下是一个用于自动管理 Linux 用户的 Shell 脚本。
USR="$*"
# 首先检查是否以 root 用户身份运行此脚本
if [ $UID -ne 0 ];then
echo -e "\033[32m-----------------\033[0m"
echo -e "\033[32mThe script must be executed using the root user.\033[0m"
exit 1
fi
# 定义添加用户的函数
add_user(){
read -p "Please enter the user name you need to create? " USR
for USR in $USR
do
# 检查用户是否存在,如果不存在,则创建用户并设置密码
id $USR
if [ $? -ne 0 ];then
useradd -s /bin/bash $USR -d /home/$USR
echo ${USR}_123456|passwd --stdin $USR
if [ $? -eq 0 ];then
echo -e "\033[32m-----------------\033[0m"
echo -e "\033[32mThe $USR user created successfully\033[0m"
echo -e "User,Password"
echo -e "$USR,${USR}_123"
echo
tail -n 5 /etc/passwd
fi
else
echo -e "\033[32m-----------------\033[0m"
echo -e "\033[32mThis $USR user already exists, please exit\033[0m"
exit 1
fi
done
}
# 定义从用户列表添加用户的函数
add_user_list(){
# 从用户列表文件中读取用户名,并逐个添加用户
if [ $# -eq 1 ];then
while read USR
do
useradd -s /bin/bash $USR -d /home/$USR
echo ${USR}_123456|passwd --stdin $USR
if [ $? -eq 0 ];then
echo -e "\033[32m-----------------\033[0m"
echo -e "\033[32mThe $USR user created successfully\033[0m"
echo -e "User,Password"
echo -e "$USR,${USR}_123"
echo
tail -n 5 /etc/passwd
fi
done < $1
else
echo -e "\033[32m-----------------\033[0m"
echo -e "\033[32mThe user list file must be entered. The reference content format is as follows:\033[0m"
echo "cyly1"
echo "cyly2"
echo "cyly3"
echo "cyly4"
echo "......"
fi
}
# 定义删除用户的函数
remove_user(){
for USR in $USR
do
# 逐个删除用户及其相关文件
userdel -r $USR
if [ $? -eq 0 ];then
echo -e "\033[32m-----------------\033[0m"
echo -e "\033[32mThe $USR user delete successfully\033[0m"
echo
tail -n 5 /etc/passwd
fi
done
}
# 定义删除用户组的函数
remove_group(){
for USR in $USR
do
# 逐个删除用户组
groupdel $USR
if [ $? -eq 0 ];then
echo -e "\033[32m-----------------\033[0m"
echo -e "\033[32mThe $USR group delete successfully\033[0m"
echo
tail -n 5 /etc/passwd
else
grep "$USR" /etc/group
if [ $? -eq 0 ];then
echo -e "\033[32m-----------------\033[0m"
echo -e "\033[32mThe $USR group delete failed, cannot remove the primary group of user $USR\033[0m"
read -p "Are you sure you want to delete the $USR user? yes or no " INPUT
if [ $INPUT == "y" -o $INPUT == "Y" -o $INPUT == "yes" -o $INPUT == "YES" ];then
userdel -r $USR
groupdel $USR >>/dev/null 2>&1
echo -e "\033[32m-----------------\033[0m"
echo -e "\033[32mThe $USR user delete successfully\033[0m"
echo -e "\033[32mThe $USR group delete successfully\033[0m"
fi
fi
fi
done
}
# 定义修改用户密码的函数
change_user_passwd(){
read -p "Please enter your user name and new password: username password: " INPUT
if [ $(echo $INPUT|sed 's/ /\n/g'|wc -l) -eq 2 ];then
USR=$(echo $INPUT|awk '{print $1}')
PAS=$(echo $INPUT|awk '{print $2}')
for USR in $USR
do
echo $PAS|passwd --stdin $USR
if [ $? -eq 0 ];then
echo -e "\033[32m-----------------\033[0m"
echo -e "\033[32mThe password of $USR user was modified successfully\033[0m"
echo -e "User,Password"
echo -e "$USR,$PAS"
echo
fi
done
fi
}
# 根据用户输入执行相应的操作
case $1 in
1)
add_user
;;
2)
add_user_list $2
;;
3)
remove_user
;;
4)
remove_group
;;
5)
change_user_passwd
;;
*)
echo "---------------------------------------------"
echo -e "\033[34mWelcome to system user management scripts:\033[0m"
echo -e "\033[32m1) add user\033[0m"
echo -e "\033[32m2) add_user_list\033[0m"
echo -e "\033[32m3) remove_user\033[0m"
echo -e "\033[32m4) remove_group\033[0m"
echo -e "\033[32m5) change_user_passwd\033[0m"
echo -e "\033[32mUsage:{/bin/sh $0 1|2|3|4|5|help}\033[0m"
echo "---------------------------------------------"
esac
使用教程(会的不用看哦)
我用的MobaXterm,先创建一个shell名称
将上面的代码复制进去
给他赋值权限
bash
chmod o+x user.sh
RUN
bash
./user.sh
这里我们输入 1不起作用,我们要用下列命令来执行1-5 或者help
具体意思看顶部需求
bash
./user.sh 1