Introducing a new way to review your PRs.

Read the announcement

basecamp / kamal

Deploy web apps anywhere.

View on GitHub

17 documents · Updated Nov 16, 2026

docs / remote-execution

Remote execution with SSHKit

Remote execution is the shared path Kamal uses to reach deployment hosts and run work on them. SSH configuration defines how a connection is made. Kamal selects targets while SSHKit manages connections, runs commands, transfers data, and reports results to the command layer. This page is part of the operational integrations area.

Connection configuration

SSH connection settings describe the identity and route used for every remote operation. The ssh section supplies the user, port, keys, OpenSSH configuration, proxy path, agent forwarding, and connection log level. Kamal passes these settings to SSHKit when the command configuration is loaded. (lib/…/ssh.rb• L1-76, lib/…/commander.rb• L192-200)

The default connection targets root on port 22. A proxy can be a jump host or a custom proxy command. Private key material can come from named secrets. Inline key material remains supported for compatibility but is deprecated. (lib/…/ssh.rb• L14-61, lib/…/ssh.yml• L15-81)

SettingPurpose
user, portSelect the remote login and SSH port.
keys, key_data, keys_onlySelect private keys and control use of other agent identities.
proxy, proxy_commandRoute the connection through another host or command.
configLoad the default OpenSSH files or named files.
forward_agentControl forwarding of the local SSH agent.
log_levelIncrease SSH detail when a connection needs diagnosis.

References: lib/…/ssh.rb• L1-76 lib/…/ssh.yml• L1-81 lib/…/commander.rb• L192-200

SSHKit runtime

SSHKit provides the common runtime for remote commands. Kamal configures its Net::SSH backend with connection options, a connection pool, output handling, and a Docker command map before command work starts. This keeps application, accessory, proxy, build, and host operations on the same remote execution path. (lib/…/commander.rb• L31-35, lib/…/commander.rb• L192-213)

The sshkit section controls how that runtime behaves under load. Connection starts are limited to avoid a burst against many hosts. Idle connections remain available for later stages so a long local task does not cause a reconnection storm. DNS retries absorb short resolver failures during connection bursts. (lib/…/sshkit.rb• L1-25, lib/…/sshkit.yml• L1-31)

SettingDefaultSystem effect
max_concurrent_starts30Limits simultaneous new SSH connections.
pool_idle_timeout900 secondsKeeps idle pooled connections available for later work.
dns_retries3Retries eligible DNS failures after the first lookup or connect attempt.

References: lib/…/sshkit.rb• L1-25 lib/…/sshkit.yml• L1-31 lib/…/commander.rb• L192-201

Command execution extensions

Kamal extends SSHKit with helpers that fit its command and output model. Commands can capture output at an explicit log level or parse JSON into readable output. Host-labeled output can be printed as human-readable text or as raw bytes when another tool must consume the result. (lib/…/sshkit_with_ext.rb• L9-32)

Kamal also merges execution options supplied by both the command and the CLI. This lets a command add options such as environment values without losing the options chosen by the caller. The resulting SSHKit command carries the current host and working directory into the remote operation. (lib/…/sshkit_with_ext.rb• L34-66)

These extensions are the boundary between higher-level operations and SSHKit. Command objects describe the work. SSHKit turns that description into a remote process, captures its result, and sends output through the configured logging path. See telemetry and logging for the output side of that path.

References: lib/…/sshkit_with_ext.rb• L9-66 lib/…/commander.rb• L200-213

DNS retry behavior

DNS retry behavior protects connection setup from temporary resolver failures. Kamal wraps the Net::SSH connection attempt and retries only errors that identify a name lookup problem. Other connection failures pass through on the first attempt because retrying them does not address the cause. (lib/…/sshkit_with_ext.rb• L73-112)

The retry delay uses a short exponential backoff with a small random change and a maximum delay. Each retry writes a warning with the host and attempt count. Setting dns_retries to 0 disables retries after the initial attempt. (lib/…/sshkit_with_ext.rb• L74-110, lib/…/sshkit.yml• L25-31)

The connection path applies this behavior at the point where a host is opened. A failed DNS lookup therefore does not create a partially usable pooled connection, while a non-DNS error still reaches the command that owns the operation. (lib/…/sshkit_with_ext.rb• L131-145, test/sshkit_dns_retry_test.rb• L15-50)

References: lib/…/sshkit_with_ext.rb• L73-145 lib/…/sshkit.rb• L19-21 lib/…/sshkit.yml• L25-31 test/sshkit_dns_retry_test.rb• L15-50

Host targeting

Host targeting converts deployment topology into the set of remote hosts for one command. Roles define application host groups. Accessories add their own hosts. A command may then narrow that set by role or by host. The selected hosts determine which SSHKit backends receive the operation. (lib/…/configuration.rb• L120-178, lib/…/commander.rb• L46-71)

The target set preserves a primary host and gives hosts in the primary role priority. This ordering matters to operations that need one leading host while still allowing work across the rest of the selected set. Role and host filters are checked against configured names, so an unmatched filter fails before remote execution starts. (lib/…/specifics.rb• L5-61, lib/…/commander.rb• L46-71)

Target choiceResult
No filterUses all configured application and accessory hosts.
Role filterUses hosts assigned to the selected roles.
Host filterUses the named hosts that exist in the configuration.
Role and host filtersUses their intersection.

See application topology configuration for how roles and hosts are declared, and host operations for commands that act on selected hosts.

References: lib/…/configuration.rb• L120-178 lib/…/commander.rb• L46-75 lib/…/specifics.rb• L5-61 lib/…/role.rb• L30-36

Remote operation coordination

SSHKit coordinates work across hosts with parallel runners and pooled connections. The normal host runner removes duplicate hosts. Kamal adds role-aware coordination for cases where the same host belongs to more than one role. This preserves role context instead of silently collapsing those role operations into one host action. (lib/…/sshkit_with_ext.rb• L220-273)

Role work has two scheduling modes:

  • In role-parallel mode, each active role runs in its own thread. Hosts for that role run through the normal host runner.
  • In host-first mode, hosts run in parallel and each host processes its matching roles in sequence.

The parallel runner waits for every host thread before it returns. If several hosts fail, it reports all collected host errors instead of stopping at the first joined error. This gives the command layer a complete result for a multi-host operation. (lib/…/sshkit_with_ext.rb• L175-210)

The coordination flow moves from configuration to selected targets and then to bounded connection starts and remote work:

flowchart TD
    A[Load SSH and SSHKit settings] --> B[Configure SSHKit backend]
    B --> C[Select roles and hosts]
    C --> D[Start pooled SSH connections]
    D --> E{DNS failure?}
    E -- Yes --> F[Back off and retry within limit]
    F --> D
    E -- No --> G[Run remote command or transfer]
    G --> H[Collect output and host errors]

References: lib/…/sshkit_with_ext.rb• L114-172 lib/…/sshkit_with_ext.rb• L175-273 lib/…/specifics.rb• L5-61 lib/…/commander.rb• L192-203

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep ssh option names aligned with the Net::SSH options passed from Commander.
  • Keep sshkit defaults and runtime configuration aligned so connection limits and retry counts take effect for every command.
  • Preserve the distinction between deduplicated host execution and role-aware execution when changing SSHKit runners.
  • Keep host and role filtering consistent with primary-host ordering so single-host and multi-host commands select the same targets.
  • Keep DNS retry logging and error classification aligned with the connection wrapper so only resolver failures are retried.

Important failure modes

TriggerConsequenceGuard
A role or host filter matches nothing.The command fails before it can run on a remote host.Validate filters against configured roles and hosts.
Too many SSH connections start at once.The resolver or SSH service can reject a burst of connections.Set max_concurrent_starts to match the host count and network capacity.
A resolver returns a temporary DNS error.Connection setup fails unless the retry limit allows recovery.Keep dns_retries enabled for unstable resolvers and inspect retry warnings.
A non-DNS connection error occurs.The operation fails without repeated connection attempts.Fix the route, credentials, port, or host instead of increasing DNS retries.
Several hosts fail in one parallel operation.A partial result is returned with failures from multiple hosts.Read the combined host error and inspect each named host.
The same host is used by multiple roles.Normal host execution can merge the host and lose role-specific work.Use role-aware coordination when role identity must remain distinct.

Key files

FileWhy to read it
lib/…/commander.rbShows when Kamal configures SSHKit and how connection settings enter the runtime.
lib/…/ssh.rbDefines the SSH connection contract.
lib/…/sshkit.rbDefines concurrency, pooling, and DNS retry settings.
lib/…/sshkit_with_ext.rbShows Kamal's command helpers, DNS protection, connection limits, and multi-host coordination.
lib/…/specifics.rbShows how role and host filters become ordered targets.
lib/…/role.rbShows how a role exposes its hosts and primary host.
lib/…/ssh.ymlDocuments the user-facing SSH settings and defaults.
lib/…/sshkit.ymlDocuments the user-facing SSHKit settings and their operational purpose.