删除账号或用户组我们一般会用到deluser、addgroup命令,这两个命令比userdel和groupdel更好用一些,一般有以下三种用法:
删除一个普通账号
deluser 后接账号名
你甚至可以删除root账号,使用—force参数,如果有权限的话
删除一个用户组deluser –group 后接用户组名
或 delgroup 后接用户组名
注意,如果该用户组是一个仍然存在的账号的初始用户组的话,这样并不能删除该用户组。另一种情况是,该用户组中还存在一些其他成员,但并不是这些成员的初始用户组,那么为了避免这个用户组被误删除,我们可以使用—only-if-empty参数。
从用户组内删除账号
deluser 账号 用户组
deluser、delgroup命令还有以下部分常用参数:
–conf后接文件名,以替换/etc/deluser.conf(这个配置文件后面详细说明)
–groupdeluser命令使用这个参数时,相当于delgroup命令
–quiet安静模式
–system仅删除系统账号或用户组,用来避免意外删除非系统账号或用户组的
–only-if-empty只有用户组内没有任何成员,才删除该用户组
–backup在删除前备份该账号家目录及邮件目录到根目录下,备份文件常命名为/user.tar.gz
–backup-to指定备份该账号家目录及邮件目录的地址,使用这个参数相当于也使用了—backup参数
–remove-home删除该账号的家目录及邮件目录
–remove-all-files删除系统内所有属于该账号的文件,范围大于—remove-home
/etc/deluser.conf
deluser及delgroup命令删除账号的设置文件就是这个,文件的内容如下:
1代表是,0代表否
第4行,是否删除账号家目录
第7行,是否删除系统内所有属于该账号的文件
第11行,是否备份账号的家目录及邮件目录
第14行,备份地址(图中表示当前目录)
第17行,如果一个用户组内仍然有成员,是否删除该用户组
第20行,备份该账号文件时,需要排除的文件类型
附一个删除账号的脚本用例
#!/bin/bash
function get_answer()
{
unset ANSWER
ASK_COUNT=0
while [ -z "$ANSWER" ]
do
ASK_COUNT=$[ $ASK_COUNT+1 ]
case $ASK_COUNT in
2)
echo
echo "Please answer the question."
echo
;;
3)
echo
echo "One last try...please answer the question."
echo
;;
4)
echo
echo "Since you refuse to answer the question..."
echo "Exiting program."
echo
exit
;;
esac
if [ -n "$LINE2" ]
then
echo "$LINE1"
echo -e $LINE2" \c"
else
echo -e $LINE1" \c"
fi
read -t 60 ANSWER
done
unset LINE1
unset LINE2
}
function process_answer()
{
case $ANSWER in
y|Y|YES|yes|Yes|yEs|yeS|YEs|yES)
;;
*)
echo
echo $EXIT_LINE1
echo $EXIT_LINE2
echo
exit
;;
esac
unset EXIT_LINE1
unset EXIT_LINE2
}
echo "Step #1 - Determine user account name to delete."
echo
LINE1="Please enter the username of the user "
LINE2="account you wish to delete fron the system:"
get_answer
USER_ACCOUNT=$ANSWER
LINE1="Is $USER_ACCOUNT the user account "
LINE2="you wish to delete fron the system? [Y/N]"
get_answer
EXIT_LINE1="Because the account, $USER_ACCOUNT is not "
EXIT_LINE2="the one you wish to delete, we are leaving the scrit..."
process_answer
USER_ACCOUNT_RECORD=`cat /etc/passwd|grep -w $USER_ACCOUNT`
if [ $? -eq 1 ]
then
echo
echo "Account,$USER_ACCOUNT,not found. "
echo "Leaving the script."
echo
exit
fi
echo "I found the record: $USER_ACCOUNT_RECORD"
echo
LINE1="Is this the correct User Account? [y/n]"
get_answer
EXIT_LINE1="Because the account,$USER_ACCOUNT is not "
EXIT_LINE2="the one you wish to delete, wo are leaving the script..."
process_answer
echo "Step #2 - find processes on system belonging to user account."
echo
ps -u $USER_ACCOUNT >/dev/null
case $? in
1)
echo "There are no processes for this account currently running."
echo
;;
0)
echo "$USER_ACCOUNT has the following processes running: "
echo
ps -u $USER_ACCOUNT
LINE1="Would you like me to kill the processer? [y/n]"
get_answer
case $ANSWER in
y|Y|YES|yes|Yes|yEs|yeS|YEs|yES)
COMMAND_1="ps -u $USER_ACCOUNT --no-heading"
COMMAND_3="xargs -d \\n /usr/bin/sudo /bin/kill -9"
echo
echo "Killing off the processes..."
$COMMAND_1|gawk '{print $1}'|$COMMAND_3
echo
echo "Processed killed."
;;
*)
echo
echo "Will not kill the processes."
echo
exit
;;
esac
;;
esac
echo "Step #3 - find files on system belonging to user account."
echo
echo "Creating a report of all files owned by $USER_ACCOUNT."
echo
echo "It is recommended that you backup/archive these files, "
echo "and then do one of two things:"
echo " 1) delete the files."
echo " 2) chang the files'ownership to a current user account."
echo
echo "Please wait, this may take a while..."
REPORT_DATE=$(date +%y%m%d)
REPORT_FILE=$USER_ACOUNT"_Files_"$REPORT_DATE".rpt"
find / -user $USER_ACCOUNT > $REPORT_FILE 2>/dev/null
echo
echo "Report is complete."
echo "Name of report : $REPORT_FILE."
echo "Location of report: `pwd`"
echo
echo
echo "Step #4 - remove user account"
LINE1="Remove $USER_ACCOUNT's account from system? [y/n]"
get_answer
EXIT_LINE1="Since you do not wish to remove the user account "
EXIT_LINE2="$USER_ACCOUNT at this time, exiting the script..."
process_answer
userdel $USER_ACCOUNT
echo
echo "User account $USER_ACCOUNT has been removed."
echo
exit