Centos或Linux编写一键式Shell脚本删除用户、组指导手册

在这里插入图片描述

文章目录

  • 一、目的
  • 二、操作步骤

一、目的

本指导手册为了更加方便使用Centos或者Linux,并在里面删除用户、用户组。

注意点1:userdel命令删除该用户时,并不能删除该用户的所有信息,只是删除了/etc/passwd、/etc/shadow、/etc/group/、/etc/gshadow四个文件里的该账户和组的信息。默认情况下创建一个用户账号,会创建一个家目录(在/home/目录以用户名命名)和一个用户邮箱(在/var/spool/mail目录以用户名命名)
因此彻底删除用户,还需额外执行命令:

rm -rf /home/用户名/
rm -rf /var/spool/mail/用户名

注意点2:删除组参数介绍

[root@usertest ~]# groupdel -h
Usage: groupdel [options] GROUP

Options:
  -h, --help                    display this help message and exit
  -R, --root CHROOT_DIR         directory to chroot into
  -P, --prefix PREFIX_DIR       prefix directory where are located the /etc/* files
  -f, --force                   delete group even if it is the primary group of a user

二、操作步骤

configure_delete_oss.sh

#!/bin/bash

# 设置用户名和密码
username1="oss_cm"
username2="oss_pm"
username3="oss_hw"
groupname1="oss_cm_group"
groupname2="oss_pm_group"
groupname3="oss_hw_group"
echo "Set userName and groupName completed."

# 检查用户是否存在
if id "$username1" >/dev/null 2>&1; then
    # 删除用户
    userdel -r "$username1"
    rm -rf /home/"$username1"/
    rm -rf /var/spool/mail/"$username1"
    echo "user $username1 deleted successfully"
else
    echo "user $username1 inexistence"
fi

if id "$username2" >/dev/null 2>&1; then
    # 删除用户
    userdel -r "$username2"
    rm -rf /home/"$username2"/
    rm -rf /var/spool/mail/"$username2"
    echo "user $username2 deleted successfully"
else
    echo "user $username2 inexistence"
fi

if id "$username3" >/dev/null 2>&1; then
    # 删除用户
    userdel -r "$username3"
    rm -rf /home/"$username3"/
    rm -rf /var/spool/mail/"$username3"
    echo "user $username3 deleted successfully"
else
    echo "user $username3 inexistence"
fi

# 检查组是否存在
if grep -q "^$groupname1:" /etc/group; then
    # 删除组
    groupdel "$groupname1"
    echo "group $groupname1 deleted successfully"
else
    echo "group $groupname1 inexistence"
fi

if grep -q "^$groupname2:" /etc/group; then
    # 删除组
    groupdel "$groupname2"
    echo "group $groupname2 deleted successfully"
else
    echo "group $groupname2 inexistence"
fi

if grep -q "^$groupname3:" /etc/group; then
    # 删除组
    groupdel "$groupname3"
    echo "group $groupname3 deleted successfully"
else
    echo "group $groupname3 inexistence"
fi
echo "OSS configuration completed."

你可能感兴趣的:(Linux相关操作,linux,centos,Shell脚本删除用户和组,脚本删除用户,组指导手册)