diff --git a/setup.sh b/setup.sh index 9a863d6..b5355e1 100644 --- a/setup.sh +++ b/setup.sh @@ -12,6 +12,22 @@ prompt_for_hostname() { read -p "Please enter the desired hostname: " hostname } +# Function to add hostname and IP to /etc/hosts +update_hosts_file() { + local ip_address + ip_address=$(hostname -I | awk '{print $1}') + echo "Updating /etc/hosts with IP $ip_address and hostname $hostname..." + + # Check if the entry already exists + if grep -q "$ip_address" /etc/hosts; then + echo "Entry for $ip_address already exists in /etc/hosts." + sed -i "s/.*$hostname/$ip_address $hostname/" /etc/hosts + else + echo "$ip_address $hostname" >> /etc/hosts + echo "Added $ip_address $hostname to /etc/hosts." + fi +} + # Function to prompt for Docker installation prompt_for_docker_install() { read -p "Would you like to install Docker CE? (y/n): " install_docker @@ -90,6 +106,9 @@ prompt_for_hostname echo "Setting hostname to $hostname..." hostnamectl set-hostname "$hostname" +# Update /etc/hosts +update_hosts_file + # Create a limited user account create_user_account @@ -149,4 +168,4 @@ elif command -v yum &> /dev/null; then yum -y autoremove fi -echo "System update complete! Timezone set to $timezone, 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." \ No newline at end of file