Last active
October 27, 2025 12:50
-
-
Save margani/724a08e14b3a4c41f13985b7a0711a3c to your computer and use it in GitHub Desktop.
Shell commands to enable Zsh and plugins on a minimum Ubuntu server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Configuration | |
| readonly VM_PREP_USER="${SUDO_USER:-$(whoami)}" | |
| readonly ZSH_PLUGINS_DIR="/home/$VM_PREP_USER/.oh-my-zsh/custom/plugins" | |
| readonly ZSHRC_PATH="/home/$VM_PREP_USER/.zshrc" | |
| # Function to check for errors and exit | |
| check_error() { | |
| if [ $? -ne 0 ]; then | |
| echo "๐จ ERROR: $1 failed. Exiting." >&2 | |
| exit 1 | |
| fi | |
| } | |
| echo "=====================================================" | |
| echo "=== PROXMOX UBUNTU VM TEMPLATE PREPARATION SCRIPT ===" | |
| echo "=====================================================" | |
| echo "Running as user: $VM_PREP_USER" | |
| # --- 1. System Update and Essential Package Installation --- | |
| # ... (Steps 1 & 2 remain the same) ... | |
| echo "--- 1. Updating and installing essential packages ---" | |
| sudo apt update | |
| check_error "apt update" | |
| sudo apt install -y \ | |
| htop ncdu tmux ufw curl wget git \ | |
| unattended-upgrades net-tools nano \ | |
| qemu-guest-agent cloud-init zsh | |
| check_error "package installation" | |
| echo "--- Reconfiguring Unattended Upgrades ---" | |
| sudo dpkg-reconfigure -plow unattended-upgrades | |
| # --- 2. QEMU Guest Agent Configuration --- | |
| echo "--- 2. Enabling and starting the QEMU Guest Agent ---" | |
| sudo systemctl start qemu-guest-agent | |
| sudo systemctl enable qemu-guest-agent | |
| check_error "QEMU Guest Agent setup" | |
| # --- 3. Installing Zsh and Oh My Zsh for the current user --- | |
| echo "--- 3. Installing Zsh and Oh My Zsh for user '$VM_PREP_USER' ---" | |
| if [ "$VM_PREP_USER" != "root" ]; then | |
| # Change to the user's home directory to run git clone as the user | |
| sudo -u "$VM_PREP_USER" sh -c " | |
| # Check if Oh My Zsh is already installed | |
| if [ -d /home/$VM_PREP_USER/.oh-my-zsh ]; then | |
| echo 'โ ๏ธ Oh My Zsh directory already exists. Skipping installation.' | |
| else | |
| # Install Oh My Zsh | |
| git clone https://github.com/ohmyzsh/ohmyzsh.git /home/$VM_PREP_USER/.oh-my-zsh | |
| check_error 'Oh My Zsh clone' | |
| fi | |
| # --- .zshrc handling --- | |
| if [ -f $ZSHRC_PATH ]; then | |
| echo 'โ ๏ธ The .zshrc file already exists. Will NOT overwrite it.' | |
| echo ' Please manually add Zsh plugins and configurations.' | |
| else | |
| # Set up .zshrc | |
| cp /home/$VM_PREP_USER/.oh-my-zsh/templates/zshrc.zsh-template $ZSHRC_PATH | |
| check_error 'Oh My Zsh config copy' | |
| # Install Plugins | |
| git clone https://github.com/zsh-users/zsh-autosuggestions.git $ZSH_PLUGINS_DIR/zsh-autosuggestions | |
| check_error 'zsh-autosuggestions clone' | |
| git clone https://github.com/zsh-users/zsh-syntax-highlighting.git $ZSH_PLUGINS_DIR/zsh-syntax-highlighting | |
| check_error 'zsh-syntax-highlighting clone' | |
| # Add plugins to .zshrc | |
| sed -i 's/^plugins=(git)$/plugins=(git zsh-autosuggestions zsh-syntax-highlighting)/' $ZSHRC_PATH | |
| check_error 'plugin configuration' | |
| echo 'โ .zshrc created and plugins configured.' | |
| fi | |
| # Set Zsh as default shell (only runs if Zsh is installed) | |
| if command -v zsh &> /dev/null; then | |
| chsh -s \$(which zsh) $VM_PREP_USER | |
| check_error 'chsh' | |
| fi | |
| " | |
| echo "โ Zsh setup complete for $VM_PREP_USER." | |
| else | |
| echo "โ ๏ธ Skipping Zsh installation: Detected script running directly as 'root'. Zsh setup should target a regular user." | |
| fi | |
| # --- 4. System Cleanup and Reset for Templating --- | |
| # ... (Steps 4 & 5 remain the same) ... | |
| echo "--- 4. Cleaning up and resetting for template creation ---" | |
| # Cloud-Init cleanup (Includes logs and machine-id reset) | |
| echo "--- Cleaning all cloud-init state and machine-id ---" | |
| sudo cloud-init clean --logs --machine-id | |
| check_error "cloud-init clean" | |
| # Forcing a new machine-id on next boot (Good for non-Cloud-Init setups too) | |
| echo "--- Resetting /etc/machine-id for uniqueness ---" | |
| sudo truncate -s 0 /etc/machine-id | |
| sudo rm -f /var/lib/dbus/machine-id | |
| sudo ln -s /etc/machine-id /var/lib/dbus/machine-id | |
| # Disk/Cache Cleanup | |
| echo "--- Cleaning up apt cache and unused packages ---" | |
| sudo apt autoremove -y | |
| sudo apt clean | |
| # --- 5. Final Preparation --- | |
| echo "--- 5. Cleaning shell history ---" | |
| history -c | |
| echo "=====================================================" | |
| echo " ๐ TEMPLATE PREPARATION COMPLETE ๐ " | |
| echo "=====================================================" | |
| echo "The system is now prepared for templating." | |
| echo "You can safely shut down this VM and convert it to a template in Proxmox." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| echo "--- Start applying cloud init" | |
| PRIMARY_IP=$(ip -4 addr show scope global | grep -oP "inet \K[\d.]+" | head -1) | |
| LAST_OCTET=$(echo $PRIMARY_IP | awk -F. "{print \$4}") | |
| NEW_HOSTNAME="umin-$LAST_OCTET" | |
| sudo hostnamectl set-hostname $NEW_HOSTNAME | |
| echo "--- Done!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment