Add command checks to setup script for user management. Implement validation for required commands and enhance error handling for usermod command usage during user creation processes.

This commit is contained in:
2025-09-05 12:36:34 +01:00
parent 420d8deddf
commit fd2be2393e

View File

@@ -47,6 +47,22 @@ check_debian() {
fi
}
# Check for required commands
check_commands() {
local required_commands="useradd passwd sudo"
for cmd in $required_commands; do
if ! command -v "$cmd" >/dev/null 2>&1 && ! [ -x "/usr/sbin/$cmd" ]; then
error "Required command '$cmd' not found. Please install it first."
fi
done
# Special check for usermod which we use later
if ! command -v usermod >/dev/null 2>&1 && ! [ -x /usr/sbin/usermod ]; then
warn "usermod command not found. Will attempt to use full path."
fi
}
# Ask user about creating an additional user
ask_additional_user() {
echo ""
@@ -174,7 +190,13 @@ create_sysadmin_user() {
# Add sysadmin to sudo group
log "Adding $SYSADMIN_USER to sudo group..."
usermod -aG sudo "$SYSADMIN_USER"
if command -v usermod >/dev/null 2>&1; then
usermod -aG sudo "$SYSADMIN_USER"
elif [ -x /usr/sbin/usermod ]; then
/usr/sbin/usermod -aG sudo "$SYSADMIN_USER"
else
error "usermod command not found. Please install the passwd package."
fi
}
create_additional_user() {
@@ -195,7 +217,13 @@ create_additional_user() {
# Add additional user to sudo group
log "Adding $ADDITIONAL_USER to sudo group..."
usermod -aG sudo "$ADDITIONAL_USER"
if command -v usermod >/dev/null 2>&1; then
usermod -aG sudo "$ADDITIONAL_USER"
elif [ -x /usr/sbin/usermod ]; then
/usr/sbin/usermod -aG sudo "$ADDITIONAL_USER"
else
error "usermod command not found. Please install the passwd package."
fi
fi
}
@@ -579,6 +607,7 @@ main() {
# Check prerequisites
check_root
check_debian
check_commands
echo -e "${BLUE}=== Debian 12 Initial Setup ===${NC}"
echo "This script will:"