2

I have an MOTD script installed in /etc/update-motd.d that works properly, however it seems that SSH bleeds out the colour.

SSHD Look:

enter image description here

Output when running the script once logged in:

enter image description here

Actual script:

cat /etc/update-motd.d/20-logo 
#! /usr/bin/env bash
echo "";
echo "$(tput setaf 2)    .::::::::::::::::::::::::::::::::::::::::...
   :::::::::::::::::::::::::::::::::::::::::::::::::::.
   .::::::::::::::::::::::::::::::::::::::::::::::::::::::.
                                      .....::::::::::::::::::
                  $(tput setaf 7):@@@@@O                          $(tput setaf 2).:::::::::::
                  $(tput setaf 7)@@@@@@@o                             $(tput setaf 2).::::::::
                  $(tput setaf 7)@@@@@@@o                                $(tput setaf 2):::::::
                  $(tput setaf 7)@@@@@@@o                                $(tput setaf 2).::::::
        $(tput setaf 7):@@@@@O   @@@@@@@o   O@@@@O                       $(tput setaf 2):::::::
      $(tput setaf 7):@@@@@@@@o  @@@@@@@o  o@@@@@@@O                    $(tput setaf 2):::::::.
     $(tput setaf 7)o@@@@@@@@O   @@@@@@@o  :@@@@@@@@@                $(tput setaf 2).::::::::.
    $(tput setaf 7)o@@@@@@@O     @@@@@@@o    o@@@@@@@O        $(tput setaf 2)..:::::::::::::
    $(tput setaf 7)@@@@@@@O      @@@@@@@o     :@@@@@@@o  $(tput setaf 2)::::::::::::::::::
   $(tput setaf 7)o@@@@@@@       @@@@@@@:      o@@@@@@@ $(tput setaf 2)::::::::::::::::.
   $(tput setaf 7)o@@@@@@o        o@@@O:        @@@@@@@ $(tput setaf 2).:::::::::..
   $(tput setaf 7)o@@@@@@@                     o@@@@@@@   $(tput setaf 2).:::::::::
   $(tput setaf 7)o@@@@@@@                     O@@@@@@O     $(tput setaf 2).:::::::::.
    $(tput setaf 7)@@@@@@@@                   O@@@@@@@:       $(tput setaf 2).::::::::::.
     $(tput setaf 7)@@@@@@@@O               :@@@@@@@@o           $(tput setaf 2).:::::::::::.
      $(tput setaf 7)@@@@@@@@@O:          o@@@@@@@@@o               $(tput setaf 2)::::::::::::::.
       $(tput setaf 7)O@@@@@@@@@@@@@@@@@@@@@@@@@@@@:                   $(tput setaf 2).:::::::::::::::..
         $(tput setaf 7)O@@@@@@@@@@@@@@@@@@@@@@@@:                         $(tput setaf 2).:::::::::::::::.
           $(tput setaf 7):@@@@@@@@@@@@@@@@@@@O                                $(tput setaf 2).::::::::::::
               $(tput setaf 7)oO@@@@@@@@@@o:                                        $(tput setaf 2)..::::.$(tput sgr0)
";
echo "================================================================================";

I've ensured that force_color_prompt=yes is enabled in ~/.bashrc and that my $TERM variable looks right on both sides. Both terminal-emulator and server show xterm-256color

What could be causing the colour to fail on SSH login?

5
  • 2
    Did you try to use ANSI color codes instead ? Green \u001b[32m as an example.
    – BANJOSA
    Commented Dec 20, 2017 at 8:18
  • No, I will give it a try however that would leave the question as is, because tput should and will work as evidenced by other questions and various howto's on the internet. The tput command outputs the ANSI colour codes. Examples linked. unix.stackexchange.com/questions/223921/no-colour-in-motd - ownyourbits.com/2017/04/05/… Commented Dec 20, 2017 at 16:13
  • ANSI codes do not make a difference, result is the same. Commented Dec 30, 2017 at 16:53
  • Gaaaaah! 40 plus processes forked for nothing. What a waste of CPU cycles...
    – Jens
    Commented Jan 15, 2018 at 22:10
  • I've ended up going back and getting ansible to pregen the motd ansi as zeppelin suggested, while storing the original script as is in the source role for readability. So no wasted CPU cycles @Jens Commented Jan 30, 2018 at 21:03

1 Answer 1

5
+50

Dynamic MOTD is generated by the pam_motd module (/etc/pam.d/login), which uses a piece of code like that to do it:

if (!system("/usr/bin/env -i PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin run-parts --lsbsysinit /etc/update-motd.d > /run/motd.dynamic.new"))
rename("/run/motd.dynamic.new", "/run/motd.dynamic");

As env is invoked with -i

-i, --ignore-environment start with an empty environment

no TERM variable is available by the time tput is run, so no color codes are included in the output:

%env -i /etc/update-motd.d/20-logoenv -i /etc/update-motd.d/20-logo
tput: No value for $TERM and no -T specified
tput: No value for $TERM and no -T specified
tput: No value for $TERM and no -T specified
...

As a workaround you can pre-generate your logo with the escape codes embedded into it, like that:

%/etc/update-motd.d/20-logo > /tmp/color.logo
%(echo "#! /usr/bin/env bash"; 
  echo "cat <<EOF"; cat /tmp/color.logo; 
  echo "EOF") > /etc/update-motd.d/20-logo

or just set an explicit TERM variable at the top of your script:

 export TERM=xterm-256color

Both options are not perfect (as TERM type will be set fixed by the time MOTD is generated), but AFAIK that is the best you can do (unless you want to patch the pam_motd itself).

1
  • Exporting the variable did the trick without need for me to pregenerate anything, which is what I wanted to achieve. Thanks! Commented Jan 15, 2018 at 16:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .