← Back to Features | ← Back to Documentation
WebSSH2 supports passing environment variables through URL parameters, allowing you to customize the SSH session environment. This feature enables scenarios like automatically opening specific files or setting custom environment variables.
Both routes are supported:
/ssh?env=FOO:bar,BAR:baz
/ssh/host/localhost?port=2244&env=FOO:bar,BAR:baz
Tips:
- Use
&env=...when adding to an existing query string; do not add a second? - Your SSH server must also allow each variable name via
AcceptEnvinsshd_config - You can restrict which variables are forwarded using an allowlist
Before using this feature, you must configure your SSH server to accept the environment variables you want to pass.
Edit your /etc/ssh/sshd_config file to include the desired variables in the AcceptEnv directive:
# Allow client to pass locale environment variables and custom vars
AcceptEnv LANG LC_* VIM_FILE CUSTOM_ENVRemember to restart your SSH server after making changes:
# For systemd-based systems
sudo systemctl restart sshd
# For init.d-based systems
sudo service sshd restartPass environment variables using the env query parameter:
http://localhost:2222/ssh/host/example.com?env=VIM_FILE:config.txt
http://localhost:2222/ssh/host/example.com?env=VIM_FILE:config.txt,CUSTOM_ENV:test
http://localhost:2222/ssh?env=VIM_FILE:config.txt,CUSTOM_ENV:test
To maintain security, environment variables must meet strict criteria:
Variable names must:
- Start with a capital letter
- Contain only uppercase letters, numbers, and underscores (
^[A-Z][A-Z0-9_]*$) - Be listed in the SSH server's
AcceptEnvdirective
Variable values cannot contain shell special characters:
;(semicolon)&(ampersand)|(pipe)`(backtick)$(dollar sign)
WebSSH2 enforces the following limits:
- Maximum 50 key/value pairs
- Key length ≤ 32 characters
- Value length ≤ 512 characters
Invalid or disallowed variables are silently ignored.
You can restrict which environment variables are forwarded using an allowlist.
{
"ssh": {
"envAllowlist": ["FOO", "BAR", "VIM_FILE"]
}
}Comma-separated format:
WEBSSH2_SSH_ENV_ALLOWLIST="FOO,BAR,VIM_FILE"JSON array format:
WEBSSH2_SSH_ENV_ALLOWLIST='["FOO","BAR","VIM_FILE"]'When an allowlist is provided, only listed keys are forwarded to the SSH session (after format/value checks).
Add to /etc/ssh/sshd_config:
AcceptEnv VIM_FILE CUSTOM_ENV PROJECT_ENVRestart SSH:
sudo systemctl restart sshdhttp://localhost:2222/ssh/host/example.com?env=VIM_FILE:settings.conf,CUSTOM_ENV:production
In your remote server's .bashrc or shell initialization file:
# Automatically open a file if specified
if [ ! -z "$VIM_FILE" ]; then
vim "$VIM_FILE"
fi
# Show environment information
if [ ! -z "$CUSTOM_ENV" ]; then
echo "Running in $CUSTOM_ENV environment"
fi
# Load project-specific settings
if [ ! -z "$PROJECT_ENV" ]; then
source "/opt/configs/${PROJECT_ENV}.sh"
fiIf variables aren't visible on the remote host after connecting:
-
Check AcceptEnv Configuration
- Ensure
AcceptEnvin/etc/ssh/sshd_configincludes each variable name - Example:
AcceptEnv FOO BAR(notAcceptEnv FOO,BAR)
- Ensure
-
Restart SSH Service
- Always restart or reload SSHD after configuration changes:
sudo systemctl reload sshd # or restart -
Check SSH Server Logs
- Enable debug mode in sshd_config:
LogLevel DEBUG - Look for
req envlines in logs - Denied variables show as:
Ignoring env request BAR: disallowed name
- Enable debug mode in sshd_config:
-
Verify Match Blocks
- Some distributions or
Matchblocks overrideAcceptEnv - Ensure no later directives disable it
- Some distributions or
-
Check URL Format
- Confirm your client URL uses
&env=...(not a second?) - Example:
?port=22&env=FOO:bar✓ - Wrong:
?port=22?env=FOO:bar✗
- Confirm your client URL uses
-
Verify Allowlist
- With an allowlist configured, only listed names are forwarded
- Check
ssh.envAllowlistorWEBSSH2_SSH_ENV_ALLOWLIST
Test with a simple variable first:
- Add to sshd_config:
AcceptEnv TEST_VAR - Restart sshd
- Connect with:
/ssh/host/server?env=TEST_VAR:hello - Check in shell:
echo $TEST_VAR
/ssh/host/dev-server?env=NODE_ENV:development,DEBUG:true
/ssh/host/server?env=VIM_FILE:config.yaml,VIM_LINE:42
/ssh/host/build-server?env=PROJECT:website,BRANCH:feature-123
/ssh/host/server?env=LANG:en_US.UTF-8,LC_ALL:en_US.UTF-8