Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I know how to set the GNOME-terminals (or xterms!) prompt to green/red regarding the last exit code:

vi .bashrc

export PROMPT_COMMAND='PS1="`
if [[ \$? = "0" ]];
then echo "\\[\\033[0;32m\\]";
else echo "\\[\\033[0;31m\\]";
fi`[\u@\h \w]\[\e[m\] "'

export PS1

in picture:

enter image description here

but if I log in to a remote server then these color settings doesn't work!

How can I set it to work on remote terminals too? Unfortunately, I can't append the mentioned lines to the remote servers .bashrc.

share|improve this question
add comment

2 Answers 2

up vote 3 down vote accepted

You are trying to change the prompt displayed by the remote shell. This, of course, requires changing the configuration file of the remote shell, i.e. .bashrc.

If you don't want to change the file, and you really have to use that shared account (insert obligatory grumble about shared accounts), and you're logging in over ssh, you can (ab)use the command= option in the ~/.ssh/authorized_keys file. A key with a command option is good only for running the specified command; but the command in the authorized_keys file runs with the environment variable SSH_ORIGINAL_COMMAND set to the command the user specified (empty for interactive sessions). So you can use something like this in ~/.ssh/authorized_keys:

command="HOME=$HOME/.HOME.lance;
         if [ -n \"$SSH_ORIGINAL_COMMAND\" ]; then
           eval \"$SSH_ORIGINAL_COMMAND\";
         else exec \"$SHELL\"; fi" ssh-rsa AAAA…== lance@example.com

Note that I put line breaks above for legibility, but this actually needs to be all on one line in the authorized_keys file.

Then put your favorite configuration files in that .HOME.lance directory.

For occasional use, you can explicitly source a profile file or run any shell command. Pass the -t option to have a terminal if you want to run interactive commands.

ssh shared-account@server "LS_COLORS='$LS_COLORS' ls --color"
ssh -t shared-account@server '. ~/.profile.drew; exec zsh'

If you only want to edit or copy files on the remote machine, you can use a network filesystem such as SSHFS (for unix clients) or Tramp (for Emacs) and work from the comfort of your local environment.

share|improve this answer
add comment

When you login to a remote system, you are given a new shell. Specifically a shell on THE REMOTE system. In order to have your favorite shell preferences on a different machine, you will also have to set your preferences on that machine. This means you will have to setup the prompt command shell prompt in the .bashrc file on the remote site. It's designed this way.

As a side note, it's possible that when you login to a remote machine, the remote machine may assume you have a less capable terminal than you do. If your PS1 is set right, try also setting your TERM variable when you get on the new machine. Remote machines often assume a pretty dumb terminal for all remote logins, but they often actually support decent ones and you can forge it to use reasonable settings but forcing this environment variable to something capable such as xterm-color.

share|improve this answer
    
:O sry, it doesn't work under xterm or gnome-terminal :O but thank you! –  LanceBaynes Jun 13 '11 at 16:22
3  
The question says "no, I don't want to append the mentioned lines to the remote servers .bashrc", so apparently he just wants his local prompt settings to work on a remote machine without actually changing the remote prompt. I'm not sure if that's even possible, but that's why color isn't working –  Michael Mrozek Jun 13 '11 at 16:29
    
@Michael: Thanks I missed that part of the question. I thought he had the stuff set but the color wasn't coming through. @Lance: Michael is right it simply doesn't work that way. See my edited answer for why. –  Caleb Jun 13 '11 at 21:34
    
@Michael: it is, kind of (copied here). –  Gilles Jun 13 '11 at 21:44
add comment

Your Answer

 
discard

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.