AutoStopping VMs over SSH
Use the AutoStopping SSH preflight script to reach a stopped VM with an ssh command.
AutoStopping shuts down a VM when it is not in use and starts it again when traffic arrives. For HTTP workloads this is transparent, but a plain ssh command gives up before a stopped VM can finish starting. The SSH preflight script closes that gap: it starts the VM, waits for it to accept connections, and then allows your ssh command to run without any change.
What you will learn from this topic
Why SSH connections fail against stopped AutoStopping VMs, and how the preflight script fixes it.
How the connection flow works once the script is set up.
How to install, configure, and add the script to the way your users already connect.
How to verify it, troubleshoot common issues, and what the current limitations are.
Why SSH needs a preflight step
AutoStopping reduces your cloud bill by stopping idle VMs and starting them again when they are needed. For web traffic this is seamless: the AutoStopping proxy receives the first request, starts the VM, and holds the request until the VM is ready. The user observes only a slower first page load.
SSH behaves differently. The ssh command expects a response within a few seconds and fails if it does not receive one. A stopped VM needs 30 to 60 seconds to boot and start its SSH service (sshd), which is longer than ssh waits, so the connection fails with Connection timed out during banner exchange before the VM is ready.
Increasing the ssh timeout is not a reliable workaround. The ConnectTimeout setting is often restricted by security policy, and a higher value slows down every failed connection, not only the first start.
The preflight script removes the conflict by doing the waiting before ssh runs. By the time ssh connects, the VM is already up, so no timeout occurs and no SSH configuration changes are required.
How it works
When a user connects to a stopped VM, the script starts the AutoStopping rules that cover the target address and waits until the VM is running and SSH is answering. The script then hands over to ssh, which connects to a VM that is already up. While the session is open, the script reports activity every two minutes, so AutoStopping does not treat the VM as idle and stop it mid-session. When the last session ends, the script stops reporting. The rule's idle timer then runs out as it would for any idle VM, and AutoStopping stops it.
What the script can and cannot do
The script starts VMs and keeps them running while users are connected. When the last session ends, the script stops reporting activity, so the rule's idle timer stops the VM the same way it stops any idle VM. The script does not stop VMs itself, change an AutoStopping rule, or block anyone's connection. Starting a VM does not grant access to it either: as always, only the SSH accounts and keys on the VM decide who can log in.
If anything goes wrong, for example the Harness API is unreachable, the script prints a short message and runs ssh. In the worst case the VM is not started and ssh fails exactly as it would without the script.
Before you begin
A VM-based AutoStopping rule that covers the address your users connect to. An idle time of 10 minutes or more is recommended. The minimum of 5 minutes works but leaves less margin if an activity report is lost.
A shared Linux host you control (bastion, jump box, build agent, or workstation) where your users run
sshand where you have administrator access to install the script. The script needsbashandcurl, so confirm both are installed on the host and install them if they are not.Network access from that host to
app.harness.io, or your Harness cluster URL, on port 443.A Harness API key and account ID. The key's role needs view access to the covering rules; see Granular RBAC for AutoStopping. Use a key with a long or no expiration. Go to Add and manage API keys to create one.
Set up SSH preflight access
Set up the script once on the shared host, then connect it to the way your users already run ssh.
Install the script
Download the current version and verify its checksum:
Install the script so users can run but cannot modify it. The script is plain
bash, so review it first if you want:Confirm the version:
Configure the script
With the script installed, the next step is to configure how it reaches your Harness account. The script reads these details, your account ID, the API endpoint, and an API key, from a configuration file.
Create the configuration file:
This creates
/etc/harness/autostop-preflight.conf. Replace<your Harness account ID>with your account ID, and leavetimeoutandreport_intervalat their defaults.Provide your API key. The script reads it from one of two safe places. Choose the one that fits your host:
Use a key file when a single user runs the script. Store the key in its own file, kept separate from the configuration so it is not exposed:
Replace <your API key> with your Harness API key. If you do not have one yet, go to Add and manage API keys to create a key with the permissions listed in Before you begin. The commands lock the file to its owner, so only that user can run the script.
With either method, do not supply the key as a command-line argument, such as --api-key <key>. Process arguments are visible to other users and processes on the host through the process list (ps), and they can be captured in shell history and system logs. The key file and the environment variable avoid this exposure, because the script reads the key without it ever appearing on the command line.
Navigate to Command-line options for the full list of options, their defaults, and the configuration keys and environment variables that set them.
Add it to how people connect
With the script installed and configured, the remaining step is to make it run automatically before ssh connects. The script does not run on its own; it has to be triggered when a user connects. You add it to your users' existing ssh workflow, so their connection runs it automatically and the VM is up by the time ssh opens the session. Select the tab that matches how your users connect: a shared wrapper script, a direct ssh command, or an automated job.
Add the following line above the existing ssh command in your wrapper:
The || true is a safeguard: the script already succeeds on every failure path, but it makes the intent plain to whoever reads the wrapper next. If the wrapper ends in exec ssh …, activity reporting follows the wrapper's process, and exec keeps the same process, so reporting tracks the SSH session and stops when it ends.
Use OpenSSH's own Match exec to run the script before any connection to the managed hosts. Each user adds this to ~/.ssh/config, or you add it to /etc/ssh/ssh_config:
ssh runs the exec command before connecting, so the VM is started and ready by the time the connection is attempted. Users keep typing ssh 10.0.1.23 exactly as they do today.
Let the script run the command itself, so it tracks that command's lifetime precisely:
This is the most reliable form, because the script knows exactly when the session ends. It is the right choice for cron jobs, CI steps, and backup scripts.
Verify it
Confirm the setup works by connecting to a stopped VM and watching it start.
Stop the VM through its AutoStopping rule and wait for the rule to show as Stopped.
Connect the way your users do, adding
--verboseto watch what happens:You see output similar to this:
The connection then succeeds immediately. In the Harness UI the rule shows as Active, and it stays active for as long as you remain connected.
Command-line options
Every option can also be set in the configuration file or the environment, so the connection command itself needs nothing but --host.
--host <address>
required
--port <port>
22
--account-id <id>
account_id
HARNESS_ACCOUNT_ID
--api-url <url>
api_url
HARNESS_AUTOSTOP_API_URL
https://app.harness.io/gateway/lw/api
--api-key-file <path>
api_key_file
HARNESS_API_KEY_FILE
--config <path>
HARNESS_AUTOSTOP_CONFIG
/etc/harness/autostop-preflight.conf
--timeout <secs>
timeout
300
--report-interval <secs>
report_interval
120
--log-file <path>
log_file
HARNESS_AUTOSTOP_LOG
discard
--no-report
off
--verbose
off
--version
The API key itself is read only from HARNESS_API_KEY or from the key file. It is never accepted as an argument.
Limitations
Addresses translated in transit do not match. The address must be one that Harness can see on the VM. Addresses translated by NAT or PrivateLink never match, and the script falls through to a plain
ssh.Dynamic public IPs are unreliable. An automatically assigned public IP changes on every restart, so a remembered public address does not reach the VM regardless of this script. Use static or private addresses.
A killed reporter process can end a session early. If the reporting process is killed without the session ending, the VM may be stopped mid-session. A generous idle time on the rule is the mitigation.
Coverage is by TCP port, not by protocol. The script starts the VM and waits for the SSH port, so anything carried over SSH, such as SFTP, SCP, rsync,
gitover SSH, and port forwarding, is covered. The script cannot distinguish one SSH-based protocol from another, and it does not detect activity on ports other than the one it checks.
Troubleshooting
Next steps
You have set up SSH preflight access on your shared host, and your users can reach AutoStopping-managed VMs over SSH with little or no change to how they connect and without losing the savings from stopping idle VMs.
AutoStopping idle VMs behind a reverse proxy: Apply the same warm-up behavior to HTTP workloads behind a proxy.
AutoStopping rules: Review and tune the rules that cover your VMs.
AutoStopping proxy: Set up the proxy and its TCP mode for non-HTTP traffic.
Last updated
Was this helpful?