Public repo to distribute scripts and config's
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

110 lignes
3.6 KiB

  1. #!/bin/bash
  2. set -euo pipefail
  3. REPO_BASE="https://docs.grid.tf/threefold_public/public/raw/branch/master"
  4. echo "=== Grid VM Preparation ==="
  5. if [ "$EUID" -ne 0 ]; then
  6. echo "Please run as root"
  7. exit 1
  8. fi
  9. # --- System update and base tools ---
  10. echo ""
  11. echo "=== Updating system and installing base tools ==="
  12. apt-get update -y
  13. DEBIAN_FRONTEND=noninteractive apt-get install -y \
  14. sudo nmon tmux restic tcpdump nano iputils-ping net-tools curl wget
  15. # --- TF Users ---
  16. echo ""
  17. echo "=== Setting up TF Users ==="
  18. wget -q "${REPO_BASE}/add-tf-users.sh" -O /tmp/add-tf-users.sh
  19. bash /tmp/add-tf-users.sh
  20. rm -f /tmp/add-tf-users.sh
  21. # --- SSH hardening ---
  22. echo ""
  23. echo "=== Configuring SSH ==="
  24. ubuntu_version=$(lsb_release -rs 2>/dev/null || (. /etc/os-release && echo "$VERSION_ID"))
  25. major_version=$(echo "$ubuntu_version" | cut -d. -f1)
  26. echo "Detected Ubuntu $ubuntu_version (major: $major_version)"
  27. # Backup original if not already backed up
  28. if [ ! -f "/etc/ssh/sshd_config.original" ]; then
  29. cp /etc/ssh/sshd_config /etc/ssh/sshd_config.original
  30. echo "[OK] Backed up original sshd_config"
  31. fi
  32. wget -q "${REPO_BASE}/sshd_config" -O /etc/ssh/sshd_config
  33. echo "[OK] Installed hardened sshd_config (port 34022, no root, no password)"
  34. if [ "$major_version" -ge 24 ]; then
  35. # Ubuntu 24.04+ uses ssh.socket — override listen port
  36. echo "Configuring ssh.socket for port 34022..."
  37. mkdir -p /etc/systemd/system/ssh.socket.d
  38. cat > /etc/systemd/system/ssh.socket.d/port.conf <<EOF
  39. [Socket]
  40. ListenStream=
  41. ListenStream=34022
  42. EOF
  43. systemctl daemon-reload
  44. systemctl restart ssh.socket
  45. systemctl enable ssh.socket
  46. echo "[OK] ssh.socket configured for port 34022"
  47. else
  48. # Ubuntu 22.04 and older — restart ssh service
  49. systemctl restart ssh
  50. echo "[OK] ssh service restarted"
  51. fi
  52. # --- Optional flags ---
  53. while getopts ":dc" opt; do
  54. case ${opt} in
  55. d )
  56. echo ""
  57. echo "=== Installing Docker ==="
  58. if command -v docker &>/dev/null; then
  59. echo "[OK] Docker already installed"
  60. else
  61. DEBIAN_FRONTEND=noninteractive apt-get install -y \
  62. ca-certificates curl gnupg lsb-release
  63. curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
  64. echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
  65. apt-get update
  66. DEBIAN_FRONTEND=noninteractive apt-get install -y docker-ce docker-ce-cli containerd.io
  67. systemctl enable --now docker
  68. echo "[OK] Docker installed"
  69. fi
  70. ;;
  71. c )
  72. echo ""
  73. echo "=== Installing Caddy ==="
  74. if command -v caddy &>/dev/null; then
  75. echo "[OK] Caddy already installed"
  76. else
  77. apt-get install -y debian-keyring debian-archive-keyring apt-transport-https
  78. curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
  79. curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | tee /etc/apt/sources.list.d/caddy-stable.list
  80. apt-get update
  81. DEBIAN_FRONTEND=noninteractive apt-get install -y caddy
  82. echo "[OK] Caddy installed"
  83. fi
  84. ;;
  85. \? )
  86. echo "Invalid option: $OPTARG" 1>&2
  87. ;;
  88. esac
  89. done
  90. shift $((OPTIND -1))
  91. echo ""
  92. echo "=== VM Preparation Complete ==="
  93. echo ""
  94. echo "SSH is now on port 34022. Connect with:"
  95. echo " ssh -p 34022 <username>@<server-ip>"
  96. echo ""
  97. echo "Root login is disabled. Use sudo from a TF user account."