Skip to content

Instantly share code, notes, and snippets.

@nksaraf
Created January 19, 2026 14:01
Show Gist options
  • Select an option

  • Save nksaraf/94420128fa93d2654cd23cd1c9294302 to your computer and use it in GitHub Desktop.

Select an option

Save nksaraf/94420128fa93d2654cd23cd1c9294302 to your computer and use it in GitHub Desktop.
Setup new vm
#!/bin/bash
# Define color codes
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
# Function to print colored messages
print_message() {
local color=$1
local message=$2
local emoji=$3
echo -e "${color}${emoji} ${message}${NC}"
}
# Function to print section headers
print_header() {
local message=$1
local emoji=$2
echo ""
echo -e "${PURPLE}════════════════════════════════════════════════════${NC}"
echo -e "${PURPLE} ${message} ${NC}"
echo -e "${PURPLE}════════════════════════════════════════════════════${NC}"
echo ""
}
# Function to print a separator line
print_separator() {
echo -e "${PURPLE}════════════════════════════════════════════════════${NC}"
echo ""
}
# Function to configure hostname
configure_hostname() {
print_header "Configuring Hostname" "🖥️"
read -p "Enter the new hostname: " new_hostname
# Set hostname using hostnamectl
sudo hostnamectl hostname "$new_hostname"
# Update /etc/hosts - only modify the 127.0.1.1 line if it exists, or add it if it doesn't
if grep -q "^127.0.1.1" /etc/hosts; then
sudo sed -i "s/^127.0.1.1.*/127.0.1.1 $new_hostname/" /etc/hosts
else
echo "127.0.1.1 $new_hostname" | sudo tee -a /etc/hosts
fi
print_message "$GREEN" "Hostname configured successfully!" "✅"
}
# Function to configure network settings
configure_network() {
print_header "Configuring Network Settings" "🌐"
read -p "Enter static IP address (e.g., 192.168.2.75): " static_ip
read -p "Enter gateway IP (e.g., 192.168.2.5): " gateway_ip
read -p "Enter additional DNS server (press Enter to skip): " additional_dns
# Find the existing netplan configuration file
netplan_file=$(ls /etc/netplan/*.yaml | head -n 1)
if [ -z "$netplan_file" ]; then
print_message "$RED" "No netplan configuration file found!" "❌"
exit 1
fi
print_message "$BLUE" "Using existing netplan configuration: $netplan_file" "📄"
# Create netplan configuration
cat << EOF | sudo tee "$netplan_file"
network:
ethernets:
enp6s18:
dhcp4: false
addresses:
- ${static_ip}/24
nameservers:
addresses:
- 8.8.8.8
${additional_dns:+"- $additional_dns"}
search: []
routes:
- to: default
via: ${gateway_ip}
version: 2
EOF
# Apply netplan configuration
sudo netplan apply
print_message "$GREEN" "Network configuration applied successfully!" "✅"
}
# Main execution
main() {
print_header "Server Setup Script" "🚀"
# Check if running with sudo privileges
if [ "$EUID" -ne 0 ]; then
print_message "$RED" "Please run as root (use sudo)" "❌"
exit 1
fi
# Configure hostname
configure_hostname
# Configure network
configure_network
print_header "Setup Complete!" "✨"
print_message "$GREEN" "Server configuration completed successfully!" "🎉"
}
# Run the main function
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment