> For the complete documentation index, see [llms.txt](https://developer.harness.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.harness.io/resilience-testing/chaos-testing/chaos-faults/ssh/ssh-chaos.md). Agents can also search and read this documentation through its [MCP server](https://developer.harness.io/~gitbook/mcp).

# SSH chaos

Run your own chaos and abort scripts on a remote host over SSH to build a host-level fault that the fault library does not cover.

SSH chaos opens an SSH session to `HOST` as `USERNAME` (authenticating with `SSH_PASSWORD` or `SSH_KEY` from Harness Secret Manager), runs the user-supplied chaos script at `CHAOS_SCRIPT_PATH` with the parameters in `CHAOS_PARAMETER`, waits, and then runs the abort script at `ABORT_SCRIPT_PATH` with the parameters in `ABORT_PARAMETER` to roll back. Both scripts are mounted into the fault pod from ConfigMaps (`chaos-script`, `abort-script` by default).

Use SSH chaos to add a host-level chaos experiment that the gold-standard fault library does not cover natively (custom service restart, custom network manipulation, custom storage manipulation, and so on) without writing a new fault from scratch.

| Property                   | Value                                                                                                                            |
| -------------------------- | -------------------------------------------------------------------------------------------------------------------------------- |
| **Fault ID**               | `ssh-chaos`                                                                                                                      |
| **Target**                 | Linux hosts reachable over SSH                                                                                                   |
| **Failure type**           | State                                                                                                                            |
| **Chaos infrastructure**   | Kubernetes                                                                                                                       |
| **Supported environments** | All [SSH environments](https://developer.harness.io/resilience-testing/chaos-testing/chaos-faults/ssh.md#supported-environments) |
| **Reversible**             | Partial. The fault runs your abort script when it ends, so recovery is only as complete as that script.                          |

***

## Use cases

Run the fault when you want to answer questions such as:

* **Custom chaos:** Inject a fault on a target VM (any TCP block, any service kill, any custom workload script) where the script does the injection and the abort script reverses it.
* **Composite experiments:** Chain multiple host-level manipulations in a single script (for example, hold a network port, restart a process, mutate a config file) so the fault has one rollback step.
* **Rollback rehearsal:** Validate that your abort script returns the host to its pre-fault state cleanly after every run.
* **Bridge to non-Harness automation:** Re-use existing chaos scripts (Bash, Python, Ansible) without porting them to the Harness fault SDK.

***

## Before you begin

* **Chaos infrastructure:** A Kubernetes chaos infrastructure on Kubernetes 1.21 or later. Go to [Get started](https://developer.harness.io/resilience-testing/chaos-testing/get-started.md) to install one and run an experiment end to end.
* **SSH reachability:** The chaos infrastructure pod can reach `HOST:22` (or the SSH port the script connects to). Adjust network policies, security groups, and routes as needed.
* **Credentials provisioned:** The SSH password or private key is stored in [Harness Secret Manager](https://developer.harness.io/harness-platform/use-harness-platform/secrets/add-use-text-secrets) (as a Text Secret for `SSH_PASSWORD`, as a File Secret for `SSH_KEY`) and referenced by identifier in the experiment.
* **Scripts uploaded as ConfigMaps:** The chaos script and the abort script are stored in Kubernetes ConfigMaps mounted into the fault pod. The defaults are `chaos-script` mounted at `/tmp/chaos-script/` (script file `script.sh`) and `abort-script` mounted at `/tmp/abort-script/` (script file `abort-script.sh`). Create them in the chaos infrastructure namespace:

  ```bash
  kubectl create configmap chaos-script --from-file=script.sh -n <chaos-namespace>
  kubectl create configmap abort-script --from-file=abort-script.sh -n <chaos-namespace>
  ```
* **Both scripts are idempotent:** The abort script must succeed even if the chaos script never ran, and the chaos script must tolerate a partially-applied previous run.

### Permissions

The fault needs no container privileges. The fault runs with the [core permissions](https://developer.harness.io/resilience-testing/chaos-testing/chaos-faults/kubernetes/permissions.md#core-permissions) of the chaos runner, which already allow it to read the script ConfigMaps and the SSH secret in the chaos runner namespace.

On the target host, `USERNAME` needs the privileges to run everything the chaos and abort scripts do. For commands such as `tc`, `iptables`, and `systemctl`, grant `sudo` access with `NOPASSWD`, or use the root account. The fault does not elevate privileges on its own.

Go to [SSH fault permissions](https://developer.harness.io/resilience-testing/chaos-testing/chaos-faults/ssh/permissions.md) for the combined permissions that cover every SSH fault.

***

## Fault tunables

Configure the following tunables when you add SSH chaos to an experiment in Chaos Studio.

**Target host**

| Tunable        | Description                                                                                    | Default                         |
| -------------- | ---------------------------------------------------------------------------------------------- | ------------------------------- |
| `HOST`         | DNS name or IP address of the target VM.                                                       | Required                        |
| `USERNAME`     | SSH user used to log in to `HOST`.                                                             | Required                        |
| `SSH_PASSWORD` | Identifier of the **Text Secret in Harness Secret Manager** that contains the SSH password.    | (required if no `SSH_KEY`)      |
| `SSH_KEY`      | Identifier of the **File Secret in Harness Secret Manager** that contains the SSH private key. | (required if no `SSH_PASSWORD`) |

**Scripts and parameters**

| Tunable             | Description                                                                                                                                         | Default                             |
| ------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------- |
| `CHAOS_SCRIPT_PATH` | Absolute path inside the fault pod where the chaos script is mounted.                                                                               | `/tmp/chaos-script/script.sh`       |
| `ABORT_SCRIPT_PATH` | Absolute path inside the fault pod where the abort (rollback) script is mounted.                                                                    | `/tmp/abort-script/abort-script.sh` |
| `CHAOS_PARAMETER`   | JSON-encoded parameters passed to the chaos script. Use indicator types (`raw:`, `env:`, `$`) to mix literals, env vars, and references.            | `""`                                |
| `ABORT_PARAMETER`   | JSON-encoded parameters passed to the abort script. Same format as `CHAOS_PARAMETER`.                                                               | `""`                                |
| `INDICATOR_TYPES`   | Comma-separated list overriding the default indicator markers when your scripts use non-standard placeholders (for example `string,environment,&`). | (uses defaults)                     |

The fault also supports the [common tunables](https://developer.harness.io/resilience-testing/chaos-testing/chaos-faults/common-tunables.md) shared by every fault.

***

## Sample chaos and abort scripts

A minimal pair of scripts that blocks a port and then unblocks it on rollback:

{% code title="/tmp/chaos-script/script.sh" %}

```bash
#!/usr/bin/env bash
set -euo pipefail
DEST_IP="$1"
PORT="$2"
DURATION=60 # seconds to keep the port blocked
sudo iptables -I OUTPUT -d "$DEST_IP" -p tcp --dport "$PORT" -j DROP
sleep "$DURATION"
```

{% endcode %}

{% code title="/tmp/abort-script/abort-script.sh" %}

```bash
#!/usr/bin/env bash
set -euo pipefail
DEST_IP="$1"
PORT="$2"
sudo iptables -D OUTPUT -d "$DEST_IP" -p tcp --dport "$PORT" -j DROP || true
```

{% endcode %}

Matching `CHAOS_PARAMETER` and `ABORT_PARAMETER`:

```json
{"parameters":[{"placeholder":"destination_ip","data_type":"string","value":"10.0.0.10"},{"placeholder":"port","data_type":"int","value":"3258"}]}
```

The same JSON is passed to both scripts so the abort script knows which rule to remove.

***

## Script parameters

`CHAOS_PARAMETER` and `ABORT_PARAMETER` take a JSON string that lists the parameters to pass to each script. Write the parameters in one of two ways.

### Declarative parameters

List each parameter with a placeholder, a data type, and a value. The fault validates each value against its data type before it runs the script, so a script never runs with an invalid parameter.

```json
{
  "parameters": [
    {"placeholder": "destination_ip", "data_type": "string", "value": "10.0.0.10"},
    {"placeholder": "port", "data_type": "int", "value": "3258"}
  ]
}
```

Escape the JSON into a single string before you set it on the fault, for example with `jq -c '@json' input.json`.

### One environment variable

Put every value in one environment variable, such as `PARAM_VALS` set to `ip:{127.0.0.1};port:{3245}`, and pass that variable as a single parameter with the `env` data type:

```json
{
  "parameters": [
    {"placeholder": "destination_target", "data_type": "env", "value": "PARAM_VALS"}
  ]
}
```

The `CHAOS_PARAMETER` and `ABORT_PARAMETER` values stay the same for every experiment that uses `PARAM_VALS`. The fault does not validate the values, so your script must parse and check them.

***

## How the fault works

The fault opens an SSH connection to `HOST` as `USERNAME`, copies the chaos script to the target, runs it with the parameters from `CHAOS_PARAMETER`, waits for the chaos duration declared inside the script, then runs the abort script with the parameters from `ABORT_PARAMETER` and closes the SSH connection.

* The target VM runs the chaos script with the privileges of `USERNAME`. Effects on the VM depend entirely on the script.
* The abort script is invoked at the end of the run (or when the experiment is aborted) to roll back the changes.
* The chaos pod streams the chaos script's stdout/stderr into its own logs for traceability.
* After the abort script returns, the SSH connection is closed and the fault exits.

### Signals to watch

Attach [resilience probes](https://developer.harness.io/resilience-testing/chaos-testing/probes.md) to assert each layer:

* **Outcome on the target:** Use a [command probe](https://developer.harness.io/resilience-testing/chaos-testing/probes/command-probe.md) that runs a check on the target (for example `ssh user@host 'systemctl is-active <unit>'`) and asserts the expected state during the chaos window.
* **Application impact:** Use an [HTTP probe](https://developer.harness.io/resilience-testing/chaos-testing/probes/http-probe.md) on an endpoint served by the workload affected by the script.
* **Monitoring fidelity:** Use a [Prometheus probe](https://developer.harness.io/resilience-testing/chaos-testing/probes/apm-probes.md) on whatever metric the script is meant to move.

***

## Verify the fault effect

While the experiment is running, confirm the fault takes effect and then clears:

1. Watch the fault pod logs to see script output.

   ```bash
   kubectl logs -n <chaos-infra-namespace> -l name=ssh-chaos -f
   ```
2. Inspect the target VM during the chaos window.

   ```bash
   ssh <USERNAME>@<HOST> "<check command, for example 'sudo iptables -S OUTPUT | head'>"
   ```
3. Confirm the abort script ran. The fault pod logs should contain the abort script's stdout/stderr at the end of the run.

***

## Recovery and cleanup

The fault reverts its own change when it ends, but not everything it disrupted. Observe the following cases:

* **End of duration:** The chaos pod runs the abort script when the chaos script returns or when the experiment duration ends.
* **Abort the experiment early:** Stopping the experiment from Chaos Studio also triggers the abort script.
* **Manual recovery:** If the abort script fails (logged in the fault pod), SSH into the target VM and run it manually with the same parameters: `bash /path/to/abort-script.sh <args>`.
* **Workload recovery:** Recovery on the target VM depends entirely on the abort script.

***

## Limitations

The fault is not appropriate in the following scenarios:

* **Idempotency is on you:** Both scripts must be safe to re-run; the fault does not guarantee an exclusive lock on the target VM.
* **Single host per run:** Each fault run targets one `HOST`. Use multiple experiments to fan out to many VMs.
* **No built-in observability:** The chaos pod streams script output to its logs but does not parse it; encode pass/fail signals in the script's exit code.
* **No mid-flight reconfigure:** Changing the script requires updating the ConfigMap and re-running the experiment.
* **SSH connectivity:** A network outage on `HOST` mid-run can leave the chaos script partially applied; ensure the abort script can recover from that state on a subsequent run.

***

## Troubleshooting

<details>

<summary>SSH chaos fails with permission denied (publickey)</summary>

Verify `USERNAME` exists on the target VM and that the supplied `SSH_KEY` (or `SSH_PASSWORD`) is correct. For `SSH_KEY`, the File Secret must contain the private key file (not the public key) and the corresponding public key must be in `~/.ssh/authorized_keys` on the target VM. Test manually with `ssh -i <private-key-file> <USERNAME>@<HOST>`.

</details>

<details>

<summary>SSH chaos runs the chaos script but the target VM shows no effect</summary>

`USERNAME` may lack the privileges the script needs (for example `sudo`, capability to write to a system directory). Either grant `sudo` access to `USERNAME` on the target VM, use a privileged account, or simplify the script.

</details>

<details>

<summary>SSH chaos abort script does not run or does not fully revert the changes</summary>

Check the fault pod logs for the abort script's stdout/stderr. If the abort script did not run, the SSH connection likely failed mid-experiment; re-run the abort script manually on the target VM with the same parameters. If the abort script ran but did not revert, make the script idempotent and re-run it.

</details>

<details>

<summary>SSH chaos reports an indicator parsing error in CHAOS_PARAMETER</summary>

Validate the JSON in `CHAOS_PARAMETER` with `jq` before running. The indicator types (`raw:`, `env:`, `$`) must match `INDICATOR_TYPES` if you overrode the defaults. The default format is `raw:{value},env:{$ENV},raw:{para}`.

</details>

***

## Related faults

The following faults test related failure paths:

* [Linux fault library](https://developer.harness.io/resilience-testing/chaos-testing/chaos-faults/linux.md): Use the supported Linux faults instead of writing custom scripts where possible.
* [Custom faults](https://developer.harness.io/resilience-testing/chaos-testing/chaos-faults/custom-faults.md): Promote a custom script into a first-class fault for repeated use.

{% @harness-feedback/feedback %}
