A little useful addition for ssh login (from putty) to my .bashrc / .bash_profile


A while ago I was getting increasingly annoyed with encoding problems through ssh - especially when using putty from windows to access my box with filenames,.. in de_AT.UTF-8 - so I was constantly setting locale back to 'C' or something similar.
And well since I am lazy I did look for a way to make that happen automatically -- unfortunately I didn't find a -reasonably easy- way to see if I am using putty. But at least you can find out if you are logged in through ssh or not. So here's a little code snippet I added to my .bashrc to save myself from garbled console output:

if [[ -n "$SSH_TTY" ]]
        echo 'Logged in remotely changing LANG and LC_ALL to C'
        export LANG=C
        export LC_ALL=C

While writing the above text I had some other idea so I tried this:
Ssh has a way to send environment variables to be set to the server - and putty supports that too:

Putty settings screenshot

So I think .. neat .. I try that .. to be greeted by the server with this:

Server refused to set environment variables

A quick look in the sshd_config man page tells me there is a config setting called AcceptEnv. The default (on Gentoo) seems to be this:

# Allow client to pass locale environment variables #367017
AcceptEnv LANG LC_*

Makes sense from a security point of view I guess so for my use case just setting those instead of my 'own' variable to detect the client works just fine. -- And I can always still modify my sshd_config to accept my custom variable if I should need it for something else. So yeah I could've saved myself writing my little code snippet, but it might still come in handy if I need something else done automatically only through ssh login too and also that little bit of shell code is once per user on the server ... the putty settings would have to be on every machine i connect from, so I will keep that snippet in my .bashrc anyway ^^

Currently unrated


There are currently no comments

New Comment


required (not published)