Ubuntu Persistent LIVE-USB Automation Scripts

Awesome — here’s a single, idempotent bash automation script that:

Adds the Kali Rolling repo (safely pinned so Ubuntu core won’t be replaced)

Imports the Kali signing key the right way (signed-by=…)

Installs selected Kali meta-packages (customizable)

Hardens with UFW + optional security tools

Installs Ollama and sets a handy ai alias


> Works great on your Ubuntu persistent Live-USB. It won’t touch Parrot OS (not included).




---

1) Save the script (on Ubuntu persistent session)

Create a file named setup-persistent-ubuntu-kali-ollama.sh and paste everything below:

#!/usr/bin/env bash
# Persistent Ubuntu Live-USB setup with Kali tools (pinned) + Ollama
# Safe-by-default: pins Kali to low priority; installs only explicitly requested packages.
# Tested on Ubuntu 22.04/24.04 live sessions.
set -euo pipefail

### ─────────────────────────────────────────────────────────────────────────────
### Configurable toggles (edit as needed)
### ─────────────────────────────────────────────────────────────────────────────
INSTALL_KALI_TOP10=true
INSTALL_KALI_INFO_GATHERING=true
INSTALL_KALI_VULN=true
INSTALL_KALI_PASSWORDS=true
INSTALL_KALI_EXPLOITATION=true
INSTALL_SECURITY_TOOLS=true # rkhunter, chkrootkit, lynis
ENABLE_UFW=true
INSTALL_OLLAMA=true

### ─────────────────────────────────────────────────────────────────────────────
### Helpers
### ─────────────────────────────────────────────────────────────────────────────
need_root() {
  if [[ $EUID -ne 0 ]]; then
    echo "Please run as root: sudo $0"
    exit 1
  fi
}

apt_ok() {
  apt-get update -y
}

pkg_install() {
  # usage: pkg_install <pkg1> <pkg2> ...
  DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends "$@"
}

ensure_pkg() {
  dpkg -s "$1" >/dev/null 2>&1 || pkg_install "$1"
}

### ─────────────────────────────────────────────────────────────────────────────
### Add Kali repo (safely pinned) + keyring
### ─────────────────────────────────────────────────────────────────────────────
add_kali_repo() {
  echo "[*] Adding Kali Rolling repository (with keyring + apt pinning)…"

  ensure_pkg wget
  ensure_pkg gpg
  ensure_pkg apt-transport-https
  ensure_pkg ca-certificates

  install -d -m 0755 /usr/share/keyrings
  # Keyring
  if [[ ! -f /usr/share/keyrings/kali-archive-keyring.gpg ]]; then
    wget -qO- https://archive.kali.org/archive-key.asc | gpg --dearmor | tee /usr/share/keyrings/kali-archive-keyring.gpg >/dev/null
  fi

  # Sources list entry (avoid duplicates)
  local src='/etc/apt/sources.list.d/kali.list'
  if [[ ! -f "$src" ]] || ! grep -q 'http.kali.org/kali' "$src"; then
    cat >"$src" <<'EOF'
# Kali Rolling (read-only, explicit installs only)
deb [arch=amd64 signed-by=/usr/share/keyrings/kali-archive-keyring.gpg] http://http.kali.org/kali kali-rolling main non-free contrib
EOF
  fi

  # Pin Kali low so it never replaces Ubuntu automatically
  local pin='/etc/apt/preferences.d/kali.pref'
  if [[ ! -f "$pin" ]]; then
    cat >"$pin" <<'EOF'
Package: *
Pin: release o=Kali
Pin-Priority: 100
EOF
  fi

  apt_ok
}

### ─────────────────────────────────────────────────────────────────────────────
### Install Kali meta-packages (explicit)
### ─────────────────────────────────────────────────────────────────────────────
install_kali_tools() {
  echo "[*] Installing selected Kali meta-packages…"
  # Hint: list with `apt-cache search ^kali-tools-`
  local list=()
  [[ "${INSTALL_KALI_TOP10}" == "true" ]] && list+=("kali-tools-top10")
  [[ "${INSTALL_KALI_INFO_GATHERING}" == "true" ]] && list+=("kali-tools-information-gathering")
  [[ "${INSTALL_KALI_VULN}" == "true" ]] && list+=("kali-tools-vulnerability")
  [[ "${INSTALL_KALI_PASSWORDS}" == "true" ]] && list+=("kali-tools-passwords")
  [[ "${INSTALL_KALI_EXPLOITATION}" == "true" ]] && list+=("kali-tools-exploitation")

  if ((${#list[@]})); then
    # Explicitly set target release to Kali to avoid cross-distro dependency resolution weirdness
    DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confnew" -t kali-rolling install --no-install-recommends "${list[@]}"
  else
    echo " (No Kali tool groups selected.)"
  fi
}

### ─────────────────────────────────────────────────────────────────────────────
### Security hardening: UFW + security scanners (optional)
### ─────────────────────────────────────────────────────────────────────────────
secure_system() {
  if [[ "${ENABLE_UFW}" == "true" ]]; then
    echo "[*] Enabling UFW (deny incoming, allow outgoing)…"
    ensure_pkg ufw
    ufw --force reset
    ufw default deny incoming
    ufw default allow outgoing
    # Common allowances (comment out any you don't need)
    # ufw allow ssh
    # ufw allow 80/tcp
    # ufw allow 443/tcp
    ufw --force enable
  fi

  if [[ "${INSTALL_SECURITY_TOOLS}" == "true" ]]; then
    echo "[*] Installing security tools: rkhunter, chkrootkit, lynis…"
    pkg_install rkhunter chkrootkit lynis
  fi
}

### ─────────────────────────────────────────────────────────────────────────────
### Install Ollama + handy alias
### ─────────────────────────────────────────────────────────────────────────────
install_ollama() {
  if [[ "${INSTALL_OLLAMA}" != "true" ]]; then
    echo "[*] Skipping Ollama install."
    return
  fi
  echo "[*] Installing Ollama…"
  # Official installer (requires internet)
  curl -fsSL https://ollama.com/install.sh | sh

  # Create a global alias 'ai' for all users that maps to a small model by default
  local profile='/etc/profile.d/ai-alias.sh'
  if [[ ! -f "$profile" ]]; then
    cat >"$profile" <<'EOF'
# Simple terminal AI alias (customize the default model as you like)
alias ai='ollama run llama3'
EOF
  fi

  # Pre-pull a compact model so it works immediately (adjust to your hardware)
  # Comment out if you want to choose manually.
  ollama pull llama3 || true

  systemctl enable ollama || true
  systemctl restart ollama || true
}

### ─────────────────────────────────────────────────────────────────────────────
### Main
### ─────────────────────────────────────────────────────────────────────────────
need_root
apt_ok
add_kali_repo
install_kali_tools
secure_system
install_ollama

echo
echo "✔ All done."
echo " - Kali repo added (pinned @ priority 100)."
echo " - Selected Kali toolsets installed (explicit)."
echo " - UFW ${ENABLE_UFW:+enabled} and security tools ${INSTALL_SECURITY_TOOLS:+installed}."
echo " - Ollama ${INSTALL_OLLAMA:+installed}. Use: ai (runs ollama with llama3)"
echo
echo "Tips:"
echo " • To search more Kali bundles: apt-cache search ^kali-tools-"
echo " • To install specific tools later (example): sudo apt-get -t kali-rolling install sqlmap"
echo " • Avoid: apt full-upgrade (to prevent cross-distro replacement)."


---

2) Run it

chmod +x setup-persistent-ubuntu-kali-ollama.sh
sudo ./setup-persistent-ubuntu-kali-ollama.sh

That’s it. Reboot (still in persistent mode), open a terminal and try:

ai "List my network interfaces and suggest basic recon commands."


---

Notes & safety checks

Apt pinning (Pin-Priority: 100) ensures Ubuntu core packages won’t be replaced; only explicitly requested Kali packages install.

Use sudo apt-get -t kali-rolling install <tool> for anything else later.

Avoid apt full-upgrade while Kali repo is enabled.

To temporarily disable Kali, sudo mv /etc/apt/sources.list.d/kali.list{,.off} && sudo apt update.


If you want this to also set up an encrypted directory on your persistent drive for AI models and loot (with cryptsetup), I can extend the script to create and auto-mount it on boot.


Comments

Popular posts from this blog

Set Up Dos Games on websites or App

Smart Monitor

Building games for Amazon Fire TV