Compare commits

..

1 Commits

Author SHA1 Message Date
Nick Hepler
cd5aef79ff Remove docker_installed variable, add prompt for time zone. 2024-10-14 21:42:15 -04:00

126
setup.sh
View File

@ -1,30 +1,24 @@
#!/bin/bash #!/bin/bash
#
# Script to perform system maintenance on a RHEL-based Linux system. # Script to update a RHEL-based Linux system, set the timezone, change the hostname,
# # add a limited user account, modify sshd_config settings, and optionally install Docker CE
# This script:
# - Updates package manager and installed packages
# - Sets the system timezone based on geolocation or user input
# - Changes the system hostname and updates /etc/hosts
# - Creates a limited user account and modifies SSH settings
# - Optionally installs Docker CE
#
# Prerequisites:
# - The script must be run as root or with sudo privileges
#
# Usage:
# - Run the script directly on the system or via SSH.
# - Respond to prompts as required during the script execution.
# Variables # Variables
sshd_config="/etc/ssh/sshd_config" sshd_config="/etc/ssh/sshd_config"
docker_installed=false
# Function to prompt for hostname # Function to prompt for hostname
prompt_for_hostname() { prompt_for_hostname() {
read -p "Please enter the desired hostname: " hostname read -p "Please enter the desired hostname: " hostname
} }
# Function to prompt for timezone selection
prompt_for_timezone() {
echo "Available timezones:"
timedatectl list-timezones
read -p "Please enter the desired timezone (default: America/New_York): " timezone
timezone=${timezone:-"America/New_York"} # Default to America/New_York if no input
}
# Function to add hostname and IP to /etc/hosts # Function to add hostname and IP to /etc/hosts
update_hosts_file() { update_hosts_file() {
local ip_address local ip_address
@ -64,9 +58,6 @@ install_docker() {
usermod -aG docker "$username" usermod -aG docker "$username"
echo "User $username added to the docker group." echo "User $username added to the docker group."
# Clean up get-docker.sh
rm -f get-docker.sh
} }
# Function to create a limited user account # Function to create a limited user account
@ -74,13 +65,6 @@ create_user_account() {
read -p "Please enter the username for the new user account: " username read -p "Please enter the username for the new user account: " username
read -sp "Please enter the password for the new user account: " password read -sp "Please enter the password for the new user account: " password
echo echo
read -sp "Please confirm the password: " password_confirm
echo
if [ "$password" != "$password_confirm" ]; then
echo "Passwords do not match. Exiting."
exit 1
fi
# Create the user and add to the wheel group # Create the user and add to the wheel group
if id "$username" &>/dev/null; then if id "$username" &>/dev/null; then
@ -117,82 +101,12 @@ elif command -v yum &> /dev/null; then
yum -y update yum -y update
fi fi
# Prompt for timezone selection
prompt_for_timezone
# Change the timezone # Change the timezone
echo "Setting timezone to $timezone..."
# Function to set the timezone to UTC in case of an error timedatectl set-timezone "$timezone"
set_utc_timezone() {
echo "Error occurred while determining the timezone. Falling back to UTC."
timedatectl set-timezone UTC
}
# Function to get the timezone from the ipinfo.io API
get_timezone_from_api() {
# Fetch geolocation information using the ipinfo.io API
response=$(curl -s https://ipinfo.io)
# Check if the curl command succeeded and the response contains the 'timezone' field
if [ $? -eq 0 ] && echo "$response" | grep -q "timezone"; then
# Extract the timezone directly from the JSON response
timezone=$(echo "$response" | jq -r '.timezone')
echo "Detected timezone: $timezone"
return 0
else
set_utc_timezone
return 1
fi
}
# Function to prompt the user to choose a timezone
prompt_for_timezone() {
echo ""
echo "Choose a timezone option:"
echo "1) Use the detected timezone ($1)"
echo "2) Use UTC"
echo "3) Enter a custom timezone"
# Read user choice
read -p "Enter the number corresponding to your choice: " choice
case "$choice" in
1)
echo "You chose to use the detected timezone: $1"
timedatectl set-timezone "$1"
;;
2)
echo "You chose to use UTC."
timedatectl set-timezone UTC
;;
3)
# Ask for a custom timezone
read -p "Enter your preferred timezone (e.g., Europe/London, America/New_York): " custom_timezone
if timedatectl list-timezones | grep -q "$custom_timezone"; then
timedatectl set-timezone "$custom_timezone"
else
echo "Invalid timezone. Falling back to UTC."
timedatectl set-timezone UTC
fi
;;
*)
echo "Invalid choice. Falling back to UTC."
timedatectl set-timezone UTC
;;
esac
}
# Main script execution starts here
echo "Attempting to detect and set the timezone..."
# Try to get the detected timezone from the API
if get_timezone_from_api; then
# If the timezone was successfully detected, prompt the user for their choice
prompt_for_timezone "$timezone"
else
# If no timezone was detected, ask the user to fall back to UTC
echo "Unable to detect timezone. Falling back to UTC."
timedatectl set-timezone UTC
fi
# Change the hostname # Change the hostname
prompt_for_hostname prompt_for_hostname
@ -209,11 +123,7 @@ create_user_account
echo "Modifying SSH configuration..." echo "Modifying SSH configuration..."
if [ -f "$sshd_config" ]; then if [ -f "$sshd_config" ]; then
# Set PermitRootLogin to no # Set PermitRootLogin to no
if ! grep -q "^PermitRootLogin" "$sshd_config"; then sed -i 's/^PermitRootLogin .*/PermitRootLogin no/' "$sshd_config" || echo "PermitRootLogin no" >> "$sshd_config"
echo "PermitRootLogin no" >> "$sshd_config"
else
sed -i 's/^PermitRootLogin.*/PermitRootLogin no/' "$sshd_config"
fi
# Set PasswordAuthentication to no # Set PasswordAuthentication to no
if grep -q '^#PasswordAuthentication' "$sshd_config"; then if grep -q '^#PasswordAuthentication' "$sshd_config"; then
@ -265,4 +175,4 @@ elif command -v yum &> /dev/null; then
yum -y autoremove yum -y autoremove
fi fi
echo "System update complete! Timezone set to ${timezone:-UTC}, hostname set to $hostname, limited user created, sshd_config modified, and Docker installation completed if selected." echo "System update complete! Timezone set to $timezone, hostname set to $hostname, limited user created, sshd_config modified, and Docker installation completed if selected."