Refactor user creation logic in setup script to include existence checks and detailed logging for both sysadmin and additional users. Enhance error handling during user addition to the sudo group. Remove self-deletion feature from customization script to maintain script availability post-execution.
This commit is contained in:
@@ -251,11 +251,6 @@ main() {
|
||||
echo ""
|
||||
echo -e "${GREEN}Server customization completed!${NC}"
|
||||
echo -e "${YELLOW}You may need to log out and back in to see the hostname change.${NC}"
|
||||
|
||||
# Self-delete the script after successful completion
|
||||
log "Cleaning up customization script..."
|
||||
rm -f "$0"
|
||||
log "Customization script deleted successfully"
|
||||
}
|
||||
|
||||
main "$@"
|
||||
55
setup.sh
55
setup.sh
@@ -319,9 +319,11 @@ setup_system() {
|
||||
|
||||
create_sysadmin_user() {
|
||||
log "Creating user: $SYSADMIN_USER"
|
||||
log "Checking if user $SYSADMIN_USER already exists..."
|
||||
|
||||
if id "$SYSADMIN_USER" &>/dev/null; then
|
||||
warn "User $SYSADMIN_USER already exists, skipping creation"
|
||||
log "User $SYSADMIN_USER found in system, UID: $(id -u $SYSADMIN_USER)"
|
||||
SYSADMIN_USER_CREATED="no"
|
||||
|
||||
# Check if the existing user has the default password "hhh"
|
||||
@@ -336,9 +338,17 @@ create_sysadmin_user() {
|
||||
fi
|
||||
else
|
||||
# Create user with home directory
|
||||
useradd -m -s /bin/bash "$SYSADMIN_USER"
|
||||
log "User $SYSADMIN_USER created successfully"
|
||||
SYSADMIN_USER_CREATED="yes"
|
||||
log "Attempting to create user $SYSADMIN_USER with home directory..."
|
||||
log "Running command: useradd -m -s /bin/bash $SYSADMIN_USER"
|
||||
|
||||
if useradd -m -s /bin/bash "$SYSADMIN_USER"; then
|
||||
log "User $SYSADMIN_USER created successfully"
|
||||
log "User details: $(getent passwd $SYSADMIN_USER)"
|
||||
log "Home directory: $(getent passwd $SYSADMIN_USER | cut -d: -f6)"
|
||||
SYSADMIN_USER_CREATED="yes"
|
||||
else
|
||||
error "Failed to create user $SYSADMIN_USER. Exit code: $?"
|
||||
fi
|
||||
|
||||
# Set password for sysadmin user with retry logic
|
||||
local max_attempts=3
|
||||
@@ -362,21 +372,38 @@ create_sysadmin_user() {
|
||||
|
||||
# Add sysadmin to sudo group
|
||||
log "Adding $SYSADMIN_USER to sudo group..."
|
||||
usermod -aG sudo "$SYSADMIN_USER"
|
||||
log "Running command: usermod -aG sudo $SYSADMIN_USER"
|
||||
|
||||
if usermod -aG sudo "$SYSADMIN_USER"; then
|
||||
log "Successfully added $SYSADMIN_USER to sudo group"
|
||||
log "User groups: $(groups $SYSADMIN_USER)"
|
||||
else
|
||||
error "Failed to add $SYSADMIN_USER to sudo group. Exit code: $?"
|
||||
fi
|
||||
}
|
||||
|
||||
create_additional_user() {
|
||||
if [[ "$CREATE_ADDITIONAL_USER" == "yes" && -n "$ADDITIONAL_USER" ]]; then
|
||||
log "Creating additional user: $ADDITIONAL_USER"
|
||||
log "Checking if user $ADDITIONAL_USER already exists..."
|
||||
|
||||
if id "$ADDITIONAL_USER" &>/dev/null; then
|
||||
log "Using existing user: $ADDITIONAL_USER"
|
||||
log "User $ADDITIONAL_USER found in system, UID: $(id -u $ADDITIONAL_USER)"
|
||||
ADDITIONAL_USER_CREATED="no"
|
||||
else
|
||||
# Create user with home directory
|
||||
useradd -m -s /bin/bash "$ADDITIONAL_USER"
|
||||
log "User $ADDITIONAL_USER created successfully"
|
||||
ADDITIONAL_USER_CREATED="yes"
|
||||
log "Attempting to create user $ADDITIONAL_USER with home directory..."
|
||||
log "Running command: useradd -m -s /bin/bash $ADDITIONAL_USER"
|
||||
|
||||
if useradd -m -s /bin/bash "$ADDITIONAL_USER"; then
|
||||
log "User $ADDITIONAL_USER created successfully"
|
||||
log "User details: $(getent passwd $ADDITIONAL_USER)"
|
||||
log "Home directory: $(getent passwd $ADDITIONAL_USER | cut -d: -f6)"
|
||||
ADDITIONAL_USER_CREATED="yes"
|
||||
else
|
||||
error "Failed to create user $ADDITIONAL_USER. Exit code: $?"
|
||||
fi
|
||||
|
||||
# Set password for additional user with retry logic
|
||||
local max_attempts=3
|
||||
@@ -400,7 +427,14 @@ create_additional_user() {
|
||||
|
||||
# Add additional user to sudo group (existing or newly created)
|
||||
log "Adding $ADDITIONAL_USER to sudo group..."
|
||||
usermod -aG sudo "$ADDITIONAL_USER"
|
||||
log "Running command: usermod -aG sudo $ADDITIONAL_USER"
|
||||
|
||||
if usermod -aG sudo "$ADDITIONAL_USER"; then
|
||||
log "Successfully added $ADDITIONAL_USER to sudo group"
|
||||
log "User groups: $(groups $ADDITIONAL_USER)"
|
||||
else
|
||||
error "Failed to add $ADDITIONAL_USER to sudo group. Exit code: $?"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
@@ -609,10 +643,11 @@ finalize_setup() {
|
||||
|
||||
log "SSH service restarted successfully"
|
||||
|
||||
# Copy customization script to sysadmin user home folder
|
||||
# Copy customization script to sysadmin user home folder (always replace if exists)
|
||||
log "Copying customization script..."
|
||||
if [[ -f "costumize.sh" ]]; then
|
||||
cp costumize.sh /home/$SYSADMIN_USER/costumize.sh
|
||||
log "Local costumize.sh found, copying to /home/$SYSADMIN_USER/costumize.sh (replacing if exists)"
|
||||
cp -f costumize.sh /home/$SYSADMIN_USER/costumize.sh
|
||||
chmod +x /home/$SYSADMIN_USER/costumize.sh
|
||||
chown $SYSADMIN_USER:$SYSADMIN_USER /home/$SYSADMIN_USER/costumize.sh
|
||||
log "Customization script copied to /home/$SYSADMIN_USER/costumize.sh"
|
||||
|
||||
Reference in New Issue
Block a user