An OpenSSH client does not send arbitrary environment variables, and a Linux server copies only approved names into the remote session. Send an approved variable with SendEnv or SetEnv on the client and the server's AcceptEnv allowlist to keep a remote command on the same deployment target, locale, proxy, or build mode as the client.

SendEnv names variables from the local environment, while SetEnv sends a fixed NAME=VALUE pair from client config. Either way, the server's AcceptEnv has to allow the name. The steps use DEPLOY_ENV as the example.

Keep the allowlist narrow because environment variables can affect shells, language runtimes, proxies, build tools, and restricted account behavior. Do not use this path for secrets, tokens, or broad patterns such as *.

Steps to send environment variables to a Linux server over SSH:

  1. Open a server-side sshd configuration drop-in with administrator privileges.
    $ sudoedit /etc/ssh/sshd_config.d/90-accept-env.conf

    If /etc/ssh/sshd_config does not include /etc/ssh/sshd_config.d/*.conf, add the directive to the main file instead, above any Match block.

  2. Add an AcceptEnv allowlist entry for the variable name.
    /etc/ssh/sshd_config.d/90-accept-env.conf
    AcceptEnv DEPLOY_ENV

    OpenSSH warns that accepted environment variables can bypass restricted user environments, so list exact names and avoid password, token, cookie, or cloud credential variables.

  3. Test the sshd configuration syntax.
    $ sudo sshd -t

    No output means the daemon configuration parsed successfully.

  4. Reload the SSH service to apply the server allowlist.
    $ sudo systemctl reload ssh

    Use sudo systemctl reload sshd on distributions that name the server unit sshd.

  5. Add SendEnv to the matching Host block in the client configuration.
    ~/.ssh/config
    Host host.example.net
        SendEnv DEPLOY_ENV

    If the host already has a block, add the SendEnv line to it. Use SetEnv DEPLOY_ENV=staging instead when every connection should send the same fixed value.

  6. Send the variable to a new remote command.
    $ DEPLOY_ENV=staging ssh host.example.net 'printenv DEPLOY_ENV'
    staging

    A variable already exported in the local shell is sent the same way, without the prefix.

Share this guide