SSH: Behavior of SetEnv for TERM variable

Server Fault Asked by Lazarus535 on December 26, 2020

I want to set the TERM environment variable to a different value for each of my remote machines, so i used SetEnv TERM=myTermForRemoteVar in ~/.ssh/config.
The remote machine still sees TERM=myLocalTermVar.

I added AcceptEnv TERM in /etc/ssh/sshd_config on the remote machine. No luck still.

I tried just for testing purposes SetEnv FOO=smth locally and AcceptEnv FOO. This works perfectly and the remote machine sees FOO=smth.

Is TERM treated specially by ssh? SetEnv works in general but not for TERM. Anyone else seeing this behavior? It is not documented at least. Is this a bug?

One Answer

This thread on the openssh-unix-dev seems to indicate that TERM is indeed special, and you can't set it via SetEnv. Here's my reading of things:

As noted in that thread, when the SSH client requests a new PTY from sshd, the request includes an explicit TERM value (see RFC4254, "The Secure Shell (SSH) Connection Protocol", section 6.2). This is separate from any environment variables the client wishes to send.

In OpenSSH's ssh.c, function ssh_session2_setup, OpenSSH unconditionally reads the PTY request's TERM value from your current environment, without any regard to any SetEnv directives. (You can see environment variables, including those from SetEnv, being sent separately in clientloop.c, function client_session2_setup.)

Then, on the server side, session.c's do_setup_env function sets up the environment for the new shell in the reverse order from how the client sends it: first it sets environment variables from s->env, then it sets TERM from the PTY request's TERM value (s->term), overwriting any value for TERM sent as a normal environment variable.

Because of this order of operations on the server, a SetEnv TERM will always be overridden by the value of TERM in the ssh client's environment, not from any SetEnv.

There seem to be good arguments for changing the behavior of OpenSSH in this regard in the aforementioned thread, but I take it no action came of it. At least one person in that thread felt strongly that the remote host's init files should be the one to modify TERM if needed. (I disagree, personally.)

Correct answer by dsedivec on December 26, 2020

Add your own answers!

Related Questions

Apache reverse proxy from root to context

1  Asked on December 13, 2021 by jzonthemtn


Barman: rsync wal archival stalls on some files

1  Asked on December 13, 2021 by thoro


Unable to port forward via ssh on windows

1  Asked on December 13, 2021 by justtryingnottofail


git clone does not work in (sshfs) mapped drive in windows

0  Asked on December 13, 2021 by dhanabal-m


OpenSSL – error in SSLv2/v3 read client hello A

1  Asked on December 13, 2021 by tuyen-nguyen


Prevent port change on redirect in nginx

5  Asked on December 11, 2021 by arbales


PHP-FPM does not automatically start after reboot

3  Asked on December 11, 2021 by steveedson


Postgres roles best practice implementation

1  Asked on December 11, 2021 by jp-beaudry


Rinetd failing to run

1  Asked on December 11, 2021 by opad


Nagios service check interval based on host type

2  Asked on December 11, 2021 by sebastien-prudhomme


Cannot connect to Linux Samba share from Windows 10

15  Asked on December 11, 2021 by pngl


Ask a Question

Get help from others!

© 2022 All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP