VPS-setup

Bootstrap

read -rp "Enter your userid: " USER
KEY=$(wget -qO- https://github.com/$USER.keys)
wget -https://github.com/RandomBits-top/VPS-setup/raw/refs/heads/main/setup-new-vps.sh
chmod +x setup-new-vps.sh
./setup-new-vps.sh "$USER" "$KEY"
echo "$USER ALL=(ALL) NOPASSWD:ALL" | tee "/etc/sudoers.d/$USER" > /dev/null && chmod 0440 "/etc/sudoers.d/$USER"

Updates

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install python3-full
sudo apt-get install joe

Cloudflare Addons

1. Create the Cloudflare Tunnel & Route SSH

  1. Log in to the Cloudflare Zero Trust Dashboard.
  2. Navigate to Networks > Tunnels and click Create a tunnel.
  3. Select Cloudflared as the connector type and click Next.
  4. Give your tunnel a name (e.g., vps-ssh-tunnel) and click Save tunnel.
  5. Copy your Tunnel Token: Under the Install and run a connector section, locate the installation command (sudo cloudflared service install <YOUR_TOKEN>). Copy only the long alphanumeric token string—this is your <cf_tunnel_token>.
  6. Click Next to go to the Route Traffic tab:
    • Subdomain: Enter a subdomain (e.g., ssh).
    • Domain: Select your domain managed by Cloudflare (e.g., example.com).
    • Service Type: Select SSH.
    • URL: Enter localhost:22 (matches ListenAddress 127.0.0.1 configured by the script).
  7. Click Save tunnel.

2. Create the Cloudflare Access Application & Enable Browser SSH

  1. In the Zero Trust dashboard, navigate to Access > Applications and click Add an application.
  2. Select Self-hosted.
  3. Configure the application settings:
    • Application name: e.g., VPS SSH
    • Session Duration: Choose your preferred session length (e.g., 24 hours).
    • Application domain: Match the exact subdomain and domain from Step 1 (e.g., ssh.example.com).
  4. Click Next and configure an Access Policy:
    • Policy name: e.g., Allow Admin
    • Action: Allow (Note: Browser rendering requires an Allow policy; Bypass and Service Auth policies are not supported).
    • Include rule: Select Emails (or Login Methods) and enter your authorized email address(es).
  5. Click Next to reach the Setup (or Settings) page:
    • Scroll to Browser rendering and enable Allow access through browser-based RDP, SSH, or VNC sessions.
    • Select SSH from the protocol dropdown.
  6. Click Add application (or Save).

3. Generate the Short-Lived SSH CA Key

Browser-based SSH relies on Cloudflare’s short-lived SSH certificates rather than static keys.

  1. In the Zero Trust dashboard, go to Access > Service Auth > SSH (or Access controls > Service credentials > SSH).
  2. Select the SSH application created in Step 2 from the dropdown.
  3. Click Generate certificate.
  4. Copy the generated Public Key (starts with ecdsa-sha2-nistp256...). This is your <cf_ssh_ca_public_key>.
  5. Put the public key in /etc/ssh/ca.pub 6. if ! grep -q '^ListenAddress 127.0.0.1' /etc/ssh/sshd_config.d/99-hardening.conf; then echo "ListenAddress 127.0.0.1" >> /etc/ssh/sshd_config.d/99-hardening.conf fi chmod 644 /etc/ssh/ca.pub if ! grep -q '^TrustedUserCAKeys /etc/ssh/ca.pub' /etc/ssh/sshd_config.d/99-hardening.conf; then echo "TrustedUserCAKeys /etc/ssh/ca.pub" >> /etc/ssh/sshd_config.d/99-hardening.conf fi
  6. ufw delete allow 22/tcp 2>/dev/null   true
  7. ufw reload

4. Verify VPS User & Run the cf-ssh Script

Prerequisite: Match Your Linux Username

Cloudflare’s browser terminal authenticates using the prefix of your Cloudflare Access login email (everything before the @ symbol). For example, alice@example.com connects as the Linux user alice.

Ensure a user with that exact name exists on your VPS before locking down port 22:

# Check if the user already exists
id <your_email_prefix>

# If not, create the user and grant sudo privileges
sudo adduser <your_email_prefix>
sudo usermod -aG sudo <your_email_prefix>

wget -q0 https://github.com/RandomBits-top/VPS-setup/raw/refs/heads/main/cf-ssh | bash -s -- 

Install nodered

bash <(curl -sL https://github.com/node-red/linux-installers/releases/latest/download/update-nodejs-and-nodered-deb)
sudo systemctl enable nodered.service

Allow node to bind to port 443

sudo apt-get install libcap2-bin
sudo setcap 'cap_net_bind_service=+ep' `which node`

Creating Self-signed Certs

Simple script to create a root ca and self-sign certs. Install the myCA.pem in your browser.

mkdir ~/certs
cd ~/certs
wget https://github.com/RandomBits-top/VPS-setup/raw/refs/heads/main/mkcert.sh

Cheap DNS