Ssh – using local LS_COLORS on remote server via ssh without modifying remote server

bashenvironment-variablesssh

I have access to loads of different ssh accounts, several hundred I'd imagine, that I use on different occasions. Some of them are personal, some of them I've gotten from clients and are shared, and loads of them are temporary. Created for one use, that will be automatically deleted after a while.

Now my problem is that I use two .dircolors, one light and one dark themed. I quite like this setup, but it means copying over a new .dircolors every time I ssh into a server, and on shared accounts occasionally annoying other people. I tried modifying my ssh script to allow me to use my local LS_COLORS, but I'm having some problems.

If I use:
ssh -t vps2 'export LS_COLORS="'$LS_COLORS'"; exec /bin/bash --noprofile --norc'
It works, but isn't exactly usable, as it ignores all of that systems default information.

If I use:
ssh -t vps2 'export LS_COLORS="'$LS_COLORS'"; exec /bin/bash'
It does not work, as bash goes through .profile and loads a different LS_COLORS.

Any suggestions on how I can both load the default .bash_profile/.bashrc AND have my own LS_COLORS?

Best Answer

Create a file, say "prefs.rc" with whatever initialisation you like.

$ scp prefs.rc vps2:/tmp/ && ssh vps2
# ssh banner
$ . /tmp/prefs.rc && rm /tmp/prefs.rc

I would probably add some checks to verify that the remote file is indeed mine to write, and does not contain some trojan. Better to err on the side of paranoia.

Alternatively, start the prefs.rc with the sequence

. /etc/profile
. ~/.bash_profile
exec LSCOLORS=...

Use scp to transfer, and then:

ssh -t vps2 'exec bash --rcfile /tmp/prefs.rc'

Ultimately you may need to copy the entire contents of .bash_profile, and just replace the LS_COLORS=... line.