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.

112 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=0.0.0.0:34022
  42. ListenStream=[::]:34022
  43. EOF
  44. systemctl daemon-reload
  45. systemctl restart ssh.socket
  46. systemctl enable ssh.socket
  47. echo "[OK] ssh.socket configured for port 34022"
  48. else
  49. # Ubuntu 22.04 and older — restart ssh service
  50. systemctl restart ssh
  51. echo "[OK] ssh service restarted"
  52. fi
  53. # --- Optional flags ---
  54. while getopts ":dc" opt; do
  55. case ${opt} in
  56. d )
  57. echo ""
  58. echo "=== Installing Docker ==="
  59. if command -v docker &>/dev/null; then
  60. echo "[OK] Docker already installed"
  61. else
  62. DEBIAN_FRONTEND=noninteractive apt-get install -y \
  63. ca-certificates curl gnupg lsb-release
  64. curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
  65. 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
  66. apt-get update
  67. DEBIAN_FRONTEND=noninteractive apt-get install -y docker-ce docker-ce-cli containerd.io
  68. systemctl enable --now docker
  69. echo "[OK] Docker installed"
  70. fi
  71. ;;
  72. c )
  73. echo ""
  74. echo "=== Installing Caddy ==="
  75. if command -v caddy &>/dev/null; then
  76. echo "[OK] Caddy already installed"
  77. else
  78. apt-get install -y debian-keyring debian-archive-keyring apt-transport-https
  79. curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
  80. curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | tee /etc/apt/sources.list.d/caddy-stable.list
  81. apt-get update
  82. DEBIAN_FRONTEND=noninteractive apt-get install -y caddy
  83. echo "[OK] Caddy installed"
  84. fi
  85. ;;
  86. \? )
  87. echo "Invalid option: $OPTARG" 1>&2
  88. ;;
  89. esac
  90. done
  91. shift $((OPTIND -1))
  92. echo ""
  93. echo "=== VM Preparation Complete ==="
  94. echo ""
  95. echo "SSH is now on port 34022. Connect with:"
  96. echo " ssh -p 34022 <username>@<server-ip>"
  97. echo ""
  98. echo "Root login is disabled. Use sudo from a TF user account."