|
| 1 | +# SSH Key Handling |
| 2 | + |
| 3 | +This page covers everything you need to know about SSH keys for the Torrust Tracker Deployer: |
| 4 | +why they are required, what format they must be in for automated deployment, and how to |
| 5 | +generate or adjust them. |
| 6 | + |
| 7 | +## Why the Deployer Needs SSH Keys |
| 8 | + |
| 9 | +The deployer uses SSH for every remote operation after infrastructure is provisioned: |
| 10 | + |
| 11 | +- **`provision`** — Waits for the VM to accept SSH connections; verifies cloud-init ran |
| 12 | + successfully. |
| 13 | +- **`configure`** — Uploads configuration files and runs Ansible playbooks over SSH. |
| 14 | +- **`release`** — Pushes Docker images and starts Docker Compose over SSH. |
| 15 | +- **`run`** — Triggers service restarts and smoke tests over SSH. |
| 16 | + |
| 17 | +All steps use the key pair configured in `ssh_credentials.private_key_path` / |
| 18 | +`ssh_credentials.public_key_path`. |
| 19 | + |
| 20 | +## Key Requirements for Unattended Automation |
| 21 | + |
| 22 | +When running in an automated environment (Docker container, CI/CD pipeline, GitHub |
| 23 | +Actions), there is **no interactive terminal and no SSH agent**. This means: |
| 24 | + |
| 25 | +| Scenario | Works? | |
| 26 | +| ---------------------------------- | ------ | |
| 27 | +| Unencrypted (passphrase-free) key | ✅ Yes | |
| 28 | +| Passphrase-protected + SSH agent | ✅ Yes | |
| 29 | +| Passphrase-protected, no SSH agent | ❌ No | |
| 30 | + |
| 31 | +A passphrase-protected key without an accessible SSH agent will cause every `provision` |
| 32 | +(and later) step to fail with: |
| 33 | + |
| 34 | +```text |
| 35 | +Permission denied (publickey,password) |
| 36 | +``` |
| 37 | + |
| 38 | +This error is indistinguishable from a wrong key or an unconfigured `authorized_keys` |
| 39 | +file. The deployer will emit a warning during `create environment` if it detects a |
| 40 | +passphrase-protected key so you can resolve this before reaching the `provision` step. |
| 41 | + |
| 42 | +## Supported Workflows |
| 43 | + |
| 44 | +### Workflow 1 — Passphrase-free dedicated deployment key (recommended) |
| 45 | + |
| 46 | +Generate a dedicated key with no passphrase and use it only for deploying this |
| 47 | +environment. This is the simplest and most portable approach. |
| 48 | + |
| 49 | +```bash |
| 50 | +ssh-keygen -t ed25519 -C "torrust-tracker-deployer" \ |
| 51 | + -f ~/.ssh/torrust_tracker_deployer_ed25519 |
| 52 | +# Leave the passphrase empty (press Enter twice) |
| 53 | +``` |
| 54 | + |
| 55 | +Configure it in your environment JSON: |
| 56 | + |
| 57 | +```json |
| 58 | +"ssh_credentials": { |
| 59 | + "private_key_path": "/home/you/.ssh/torrust_tracker_deployer_ed25519", |
| 60 | + "public_key_path": "/home/you/.ssh/torrust_tracker_deployer_ed25519.pub" |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +### Workflow 2 — Passphrase-protected key with SSH agent forwarding into Docker |
| 65 | + |
| 66 | +If you need to keep the passphrase on the key, you can forward your local SSH agent |
| 67 | +socket into the deployer Docker container: |
| 68 | + |
| 69 | +```bash |
| 70 | +# Make sure your key is loaded into the agent |
| 71 | +ssh-add ~/.ssh/torrust_tracker_deployer_ed25519 |
| 72 | + |
| 73 | +# Pass the agent socket when running the container |
| 74 | +docker run --rm \ |
| 75 | + -v "$SSH_AUTH_SOCK:/tmp/ssh-agent.sock" \ |
| 76 | + -e SSH_AUTH_SOCK=/tmp/ssh-agent.sock \ |
| 77 | + -v $(pwd)/envs:/var/lib/torrust/deployer/envs \ |
| 78 | + -v $(pwd)/build:/var/lib/torrust/deployer/build \ |
| 79 | + torrust/tracker-deployer:latest \ |
| 80 | + provision my-environment |
| 81 | +``` |
| 82 | + |
| 83 | +The deployer will use the agent socket to authenticate without needing the passphrase |
| 84 | +in plaintext. |
| 85 | + |
| 86 | +### Workflow 3 — Native (non-Docker) execution with an SSH agent on the host |
| 87 | + |
| 88 | +When running the deployer binary directly (not in Docker), your desktop SSH agent is |
| 89 | +typically already running and holds the unlocked key. The deployer inherits the |
| 90 | +`SSH_AUTH_SOCK` environment variable automatically. |
| 91 | + |
| 92 | +```bash |
| 93 | +# Load your key once per session |
| 94 | +ssh-add ~/.ssh/torrust_tracker_deployer_ed25519 |
| 95 | + |
| 96 | +# Run the deployer natively — the agent socket is inherited |
| 97 | +torrust-tracker-deployer provision my-environment |
| 98 | +``` |
| 99 | + |
| 100 | +## Removing an Existing Passphrase |
| 101 | + |
| 102 | +If you already created a key with a passphrase and want to remove it: |
| 103 | + |
| 104 | +```bash |
| 105 | +ssh-keygen -p -f ~/.ssh/torrust_tracker_deployer_ed25519 |
| 106 | +# Enter old passphrase, then press Enter twice for the new (empty) passphrase |
| 107 | +``` |
| 108 | + |
| 109 | +## Security Notes |
| 110 | + |
| 111 | +- **Dedicated deployment keys** — Use a separate key pair for each deployer environment; |
| 112 | + never reuse your personal SSH key for automated deployments. |
| 113 | +- **Key rotation** — Replace deployment keys after the deployment is complete or the |
| 114 | + environment is destroyed. |
| 115 | +- **Filesystem permissions** — Private key files must be readable only by the owner: |
| 116 | + |
| 117 | + ```bash |
| 118 | + chmod 600 ~/.ssh/torrust_tracker_deployer_ed25519 |
| 119 | + ``` |
| 120 | + |
| 121 | +- **Never commit private keys** — Add key paths to `.gitignore`; store them outside the |
| 122 | + repository. |
| 123 | + |
| 124 | +## Configuration Reference |
| 125 | + |
| 126 | +The SSH key paths are specified in the `ssh_credentials` section of the environment |
| 127 | +configuration file: |
| 128 | + |
| 129 | +```json |
| 130 | +"ssh_credentials": { |
| 131 | + "private_key_path": "/absolute/path/to/private_key", |
| 132 | + "public_key_path": "/absolute/path/to/private_key.pub", |
| 133 | + "username": "torrust", |
| 134 | + "port": 22 |
| 135 | +} |
| 136 | +``` |
| 137 | + |
| 138 | +See the [environment config JSON schema](../../schemas/environment-config.json) for the |
| 139 | +full `ssh_credentials` field documentation. |
| 140 | + |
| 141 | +## Related Documentation |
| 142 | + |
| 143 | +- [Create Environment Command](commands/create.md) — passphrase warning details |
| 144 | +- [Hetzner Provider Guide](providers/hetzner.md) — SSH key requirements for cloud deployments |
| 145 | +- [Security Guide](security.md) — broader security considerations |
| 146 | +- [ADR: SSH Key Passphrase Detection](../../docs/decisions/ssh-key-passphrase-detection.md) |
0 commit comments