Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
Tried stuff like this as well: ssh -t vps2 'export TERM="xterm"; export PS1="\s-\v\$"; source /etc/profile ; source .bashrc ; export LS_COLORS="'$LS_COLORS'"; exec /bin/bash --noprofile --norc' but I loose all aliases... –  neuron Aug 10 '12 at 6:13
I can probably use expect to do it, but that's not installed on most of the hosts. I seem to remember some alternative to the expect command but I dont remember what it was called. OR maybe I can use expect client side to do it.. –  neuron Aug 10 '12 at 6:41
Similar question here : superuser.com/questions/221001 –  neuron Aug 22 '12 at 12:44

3 Answers 3

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.

share|improve this answer
That'd require manually running the second line though wouldn't it? As I cant run that after bash initialization and still get a valid shell? –  neuron Aug 10 '12 at 6:01
Hang on. The .bash_profile/.bashrc doesn't leave you with a valid shell? Let me ponder that... –  Henk Langeveld Aug 10 '12 at 6:52

This works (client side, without any server modification): expect -c 'spawn ssh vps2 expect "~#" send "export LS_COLORS=\"no=00:fi=00:ETC_ETC_ETC_ETC:\"\r" interact'

Is is of course quite far from optimal (as it "types" the export command after ssh, causing a delay), any cleaner solution is very welcome! :)

share|improve this answer
Whatever gets the job done. –  Henk Langeveld Aug 10 '12 at 7:10
Yeah, my solution has a slight delay during login though. From all the "typing", since LS_COLORS is quite long. I might combine your answer and mine, and run a shorter eval $(dircolors .dircolors-neuron) or something like that. –  neuron Aug 11 '12 at 6:45
Yup, expect can be slow... –  Henk Langeveld Aug 11 '12 at 11:04
Little update on this, I can make it faster by just skipping the expect and sending it right away. ssh will buffer it and send it as soon as the connection is made. –  neuron Aug 11 '12 at 13:22
This breaks terminal resizing, see askubuntu.com/questions/93848/… . Suspected it was expect for a while but didn't have time to look into it :/ –  neuron Aug 22 '12 at 12:25
up vote 0 down vote accepted

ssh -t vps2 ' cp ~/.bashrc ~/.bashrc.n &>/dev/null ; echo "LS_COLORS=\"no=00:fi=00:ETC:ETC:ETC\";" >> ~/.bashrc.n ; echo "export LS_COLORS" >> ~/.bashrc.n ; echo "alias ls=\"ls --color=auto\";" >> ~/.bashrc.n ; exec bash --rcfile ~/.bashrc.n'

This will work without requiring a extra scp first, the extra connection delay here should be non existant. You wont need any extra command during login, and it'll use the existing shell's setup, except from with my overrides. It wont break terminal resizing like expect does :)

I actually also added PS1 and an alias for grep/egrep with colors, but for ease of readability the example code above doesn't add that.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.