You are not logged in.

#1 2018-12-06 13:14:06

wallace
Member
Registered: 2018-12-06
Posts: 7

[SOLVED] Fixing locale on non-login shell

I've been scratching my head over this one for a couple of hours.

Upgraded my home server from Debian to Arch a while ago. I'm running scripts that execute various commands over ssh. Those commands are executed on a non-login bash shell environment for which according to https://wiki.archlinux.org/index.php/bash sources only ~/.bashrc.
The problem starts when dealing with UTF-8 characters in file names, etc., since apparently non-login shells do not inherit locale settings (aka sourcing /etc/profile.d/locale.sh).
I can confirm a fix for this by simply adding

source /etc/profile.d/locale.sh

or just

export LC_CTYPE=en_US.UTF-8

before running any command, but this is obviously not ideal.
I tried adding the above commands to ~/.bashrc, but it doesn't seem to be executed at all (confirmed also by echoing random text).

I know for a fact that on Debian it worked right out of the box, and I didn't have to change anything to have proper UTF support on non-login shells, question is how it's supposed to be done on Arch.

By the way, the command I'm running to confirm this is:

ssh -t root@server "locale && ls /path/to/files/containing/utf8/names"

which produces gibberish for ls and the following for locale:

LANG=
LC_CTYPE="POSIX"
LC_NUMERIC="POSIX"
LC_TIME="POSIX"
LC_COLLATE="POSIX"
LC_MONETARY="POSIX"
LC_MESSAGES="POSIX"
LC_PAPER="POSIX"
LC_NAME="POSIX"
LC_ADDRESS="POSIX"
LC_TELEPHONE="POSIX"
LC_MEASUREMENT="POSIX"
LC_IDENTIFICATION="POSIX"
LC_ALL=

Thanks in advance!

P.S
My /etc/ssh/sshd_config contains

AcceptEnv LANG LC_*

.

Last edited by wallace (2018-12-09 05:11:10)

Offline

#2 2018-12-06 13:58:18

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,442
Website

Re: [SOLVED] Fixing locale on non-login shell

I can replicate your locale output using the command you shared, however this is no surprise to me.  If you use ssh to start an interactive shell, ~/.bashrc (or your shell's equivalent) will be sourced.  But when you pass commands as you have done, you are not starting an interactive shell, and per the BASH man page, under such conditions ~/.bashrc is not sourced.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3 2018-12-06 14:06:22

wallace
Member
Registered: 2018-12-06
Posts: 7

Re: [SOLVED] Fixing locale on non-login shell

Trilby wrote:

I can replicate your locale output using the command you shared, however this is no surprise to me.  If you use ssh to start an interactive shell, ~/.bashrc (or your shell's equivalent) will be sourced.  But when you pass commands as you have done, you are not starting an interactive shell, and per the BASH man page, under such conditions ~/.bashrc is not sourced.

$ ssh -t root@server "[[ $- == *i* ]] && echo 'Interactive' || echo 'Not interactive' && shopt -q login_shell && echo 'Login shell' || echo 'Not login shell'"
Interactive
Not login shell

Either this or that, I need to find a way to source the locale settings upon login. Any idea how?

Offline

#4 2018-12-06 14:14:13

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,442
Website

Re: [SOLVED] Fixing locale on non-login shell

wallace wrote:

I need to find a way to source the locale settings upon login. Any idea how?

ssh -t root@server "source /etc/profile.d/locale.sh && locale && ls /path/to/files/containing/utf8/names"

Or if you want it more automated, put the source command in ~/.ssh/rc on the remote system.

Last edited by Trilby (2018-12-06 14:15:50)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#5 2018-12-07 07:30:31

wallace
Member
Registered: 2018-12-06
Posts: 7

Re: [SOLVED] Fixing locale on non-login shell

Trilby wrote:
wallace wrote:

I need to find a way to source the locale settings upon login. Any idea how?

ssh -t root@server "source /etc/profile.d/locale.sh && locale && ls /path/to/files/containing/utf8/names"

Or if you want it more automated, put the source command in ~/.ssh/rc on the remote system.

Hi, thanks for your answer.

I actually have also tried that.
I have tried a couple of options, from sourcing /etc/profile.d/locale.sh to exporting variables directly on ~/.ssh/rc, ~/.ssh/sshrc and even /etc/ssh/sshrc. I could confirm that all of them were running by also adding a n echo line echo 'Hello World'.
However, no matter what I do, I cannot get locale to work properly, unless I explicitly specify the command

source /etc/profile.d/locale.sh

like you mentioned.
Any idea on how I could fix the locale globally?
Thanks.

Offline

#6 2018-12-07 10:08:53

nl6720
The Evil Wiki Admin
Registered: 2016-07-02
Posts: 591

Re: [SOLVED] Fixing locale on non-login shell

This also happens with zsh.

I would suggest specifying the shell (started as a login shell) as the command for ssh to run. E.g.:

ssh -t root@server 'bash -lc "locale && ls /path/to/files/containing/utf8/names"'

or

ssh -t root@server '"$SHELL" -lc "locale && ls /path/to/files/containing/utf8/names"'

Offline

#7 2018-12-07 11:16:36

wallace
Member
Registered: 2018-12-06
Posts: 7

Re: [SOLVED] Fixing locale on non-login shell

I didn't mention this before, but I'm trying to solve this globally since I've also got some Python scripts relying on pysftp for non-interactive non-login shells.

In other words, I want to have UTF-8 support on any ssh login mode without having to specify anything special on the client side -> something that worked flawlessly on Debian as I mentioned before, so it SHOULD be possible.

Offline

#8 2018-12-07 13:16:55

seth
Member
Registered: 2012-09-03
Posts: 49,966

Re: [SOLVED] Fixing locale on non-login shell

man 8 sshd_conf, see PermitUserEnvironment

Online

#9 2018-12-08 19:02:30

wallace
Member
Registered: 2018-12-06
Posts: 7

Re: [SOLVED] Fixing locale on non-login shell

seth wrote:

man 8 sshd_conf, see PermitUserEnvironment

That's it!
I went to /etc/ssh/sshd_config and added:

PermitUserEnvironment yes

Then I created a file ~/.ssh/environment with the following line:

LANG=en_US.UTF-8

and everything was working in all four different combinations of login/interactive.
Thank you so much!

I wish I could set an environment file inside /etc/ssh/environment but as far as I can tell it's not possible.

By the way, another way to solve this was to include:

Host <server ip>
    SendEnv LANG LC_*

in the client's /etc/ssh/ssh_config (or ~/.ssh/config).
However, it solves only part of the problem since it has to be configured manually on all clients, and it wouldn't work for non-login, non-interactive sessions.

Offline

#10 2018-12-08 19:47:37

seth
Member
Registered: 2012-09-03
Posts: 49,966

Re: [SOLVED] Fixing locale on non-login shell

I wish I could set an environment file inside /etc/ssh/environment

/etc/ssh/sshd_config

…
SetEnv "LANG=en_US.UTF-8"
…

Please always remember to mark resolved threads by editing your initial posts subject - so others will know that there's no task left, but maybe a solution to find.
Thanks.

Online

#11 2018-12-09 05:10:17

wallace
Member
Registered: 2018-12-06
Posts: 7

Re: [SOLVED] Fixing locale on non-login shell

seth wrote:

I wish I could set an environment file inside /etc/ssh/environment

/etc/ssh/sshd_config

…
SetEnv "LANG=en_US.UTF-8"
…

Please always remember to mark resolved threads by editing your initial posts subject - so others will know that there's no task left, but maybe a solution to find.
Thanks.

Super! Thank you very much! Works like a charm!

Offline

Board footer

Powered by FluxBB