Skip to content

Instantly share code, notes, and snippets.

@fcakyon
Created September 6, 2025 11:28
Show Gist options
  • Select an option

  • Save fcakyon/0834d1ca7ee26dfa86ddcb09a63693c0 to your computer and use it in GitHub Desktop.

Select an option

Save fcakyon/0834d1ca7ee26dfa86ddcb09a63693c0 to your computer and use it in GitHub Desktop.
VSCode Remote Tunnel Setup Script
#!/bin/bash
# Configuration
TUNNEL_NAME="my-server-tunnel"
# Colors for output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'
print_info() { echo -e "${GREEN}[INFO]${NC} $1"; }
print_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
print_error() { echo -e "${RED}[ERROR]${NC} $1"; }
# Detect architecture
detect_arch() {
case $(uname -m) in
x86_64) echo "cli-linux-x64" ;;
aarch64|arm64) echo "cli-linux-arm64" ;;
armv7l|armhf) echo "cli-linux-armhf" ;;
*) print_error "Unsupported architecture: $(uname -m)"; exit 1 ;;
esac
}
print_info "Setting up VS Code Tunnel: $TUNNEL_NAME"
# Setup working directory
WORK_DIR="$HOME/vscode-tunnel"
mkdir -p "$WORK_DIR" && cd "$WORK_DIR"
# Download VS Code CLI
ARCH=$(detect_arch)
VSCODE_URL="https://update.code.visualstudio.com/latest/$ARCH/stable"
print_info "Downloading VS Code CLI for $ARCH..."
if command -v wget >/dev/null 2>&1; then
wget -q -O vscode-cli.tar.gz "$VSCODE_URL"
elif command -v curl >/dev/null 2>&1; then
curl -sL -o vscode-cli.tar.gz "$VSCODE_URL"
else
print_error "Need wget or curl to download. Install with: sudo apt install wget"
exit 1
fi
# Extract and setup
print_info "Extracting VS Code CLI..."
tar -xzf vscode-cli.tar.gz && rm vscode-cli.tar.gz
chmod +x code
# Try to install as persistent service first
print_info "Attempting to install as persistent systemd service..."
if ./code tunnel service install --disable-telemetry --accept-server-license-terms --name "$TUNNEL_NAME" 2>/dev/null; then
print_info "✓ Successfully installed as systemd service!"
# Enable lingering if possible
if command -v loginctl >/dev/null 2>&1 && loginctl enable-linger "$USER" 2>/dev/null; then
print_info "✓ Enabled user lingering for auto-start on boot"
else
print_warn "Could not enable lingering. Service may not start on boot without login."
fi
print_info "Service commands:"
echo " Status: systemctl --user status code-tunnel.service"
echo " Stop: systemctl --user stop code-tunnel.service"
echo " Start: systemctl --user start code-tunnel.service"
echo " Logs: journalctl --user -u code-tunnel.service -f"
print_warn "The service will prompt for GitHub authentication on first run."
print_info "Check logs to get the authentication URL if needed."
else
print_warn "Failed to install as systemd service. Installing tmux for fallback..."
# Now install tmux since service failed
if ! command -v tmux >/dev/null 2>&1; then
print_info "Installing tmux..."
# Try without sudo first (might be root)
if apt update >/dev/null 2>&1 && apt install -y tmux >/dev/null 2>&1; then
print_info "tmux installed successfully"
elif sudo apt update >/dev/null 2>&1 && sudo apt install -y tmux >/dev/null 2>&1; then
print_info "tmux installed with sudo"
else
print_error "Failed to install tmux. Please install manually: sudo apt install tmux"
exit 1
fi
else
print_info "tmux already installed"
fi
# Kill existing tmux session if exists
tmux kill-session -t vscode-tunnel 2>/dev/null
# Start tunnel in tmux
print_info "Starting tunnel in tmux session 'vscode-tunnel'..."
tmux new-session -d -s vscode-tunnel -c "$WORK_DIR"
tmux send-keys -t vscode-tunnel "./code tunnel --disable-telemetry --name \"$TUNNEL_NAME\" --accept-server-license-terms" Enter
sleep 2
print_info "✓ Tunnel running in tmux session 'vscode-tunnel'"
echo "Commands:"
echo " Attach: tmux attach -t vscode-tunnel"
echo " Detach: Ctrl+B then d"
echo " Kill: tmux kill-session -t vscode-tunnel"
echo
print_warn "Attaching to session for GitHub authentication..."
tmux attach -t vscode-tunnel
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment