brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: ssh SetEnv sends a variable to the server, if AcceptEnv allows it

Network and access
~/tips/tip-ssh-setenv-acceptenv

SetEnv PROJECT_ENV=staging

Sets an environment variable in the remote session straight from your ssh config.

I like config that follows me to the machine I log in to, and a variable that says “this is staging” is a good example. SSH has two halves for this, and both have to agree before anything arrives.

On the client, in ~/.ssh/config:

Host staging
    SetEnv PROJECT_ENV=staging

Or for one connection, using the config syntax with -o:

ssh -o "SetEnv PROJECT_ENV=staging" host

On the server, in sshd_config:

AcceptEnv PROJECT_ENV

After reloading sshd, ssh staging 'echo $PROJECT_ENV' should print staging.

That is the catch. SetEnv gives a variable and its value in NAME=VALUE form. The server must be prepared to accept it, with the exception of TERM. By default sshd accepts no variables at all, so without an AcceptEnv line the variable never arrives.

Its sibling SendEnv copies variables from your local environment instead of setting a value in the config. It takes names, including wildcards, and the default is to send nothing.

A caveat from the sshd docs: some environment variables can be used to bypass restricted user environments, so they ask for care with AcceptEnv. I’d list exact names and avoid a bare *.

Further reading: ssh_config(5), sshd_config(5)

More Network and access tips scp -3 alice@server-a:/var/backups/db.sql.gz bob@server-b:/srv/restore/ tailscale exit-node suggest tailscale lock sign nodekey:1abddef1 tlpub:abcdef12 wg genpsk > peer1.psk Table = off All Network and access tips (163) All topics

Link