Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Change bash prompt color when SSH
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo
View previous topic :: View next topic  
Author Message
tuggbuss
Apprentice
Apprentice


Joined: 20 Mar 2017
Posts: 222

PostPosted: Sun Jun 03, 2018 8:46 am    Post subject: Change bash prompt color when SSH Reply with quote

Running a couple of Gentoo workstations i sometimes use the "wrong" terminal session not aware being on a remote host (even thou my username is different).

Is there a way to make bash prompt another color in the remote session?

Link to imgur below:

https://i.imgur.com/2yCsA7j.png
Back to top
View user's profile Send private message
Zucca
Moderator
Moderator


Joined: 14 Jun 2007
Posts: 3342
Location: Rasi, Finland

PostPosted: Sun Jun 03, 2018 8:59 am    Post subject: Reply with quote

You can check if ssh environment variables are set. Then set PS1 accordingly via .bashrc for example.

This, however, is more complicated if you have an ongoing screen/tmux session on the machine. I know tmux has the status line which one can alter. So using it as an indicator whether the current attached client is via ssh or not might work.

I think I'm going to implement this on my PCs too. I just need to RTFM about tmux status bar.
_________________
..: Zucca :..
Gentoo IRC channels reside on Libera.Chat.
--
Quote:
I am NaN! I am a man!
Back to top
View user's profile Send private message
tuggbuss
Apprentice
Apprentice


Joined: 20 Mar 2017
Posts: 222

PostPosted: Sun Jun 03, 2018 9:05 am    Post subject: Reply with quote

Thanks, i did find a quick fix, changing my host name on the other machine to is't IP. and created a user named remotessh

remotessh@192.168.0.123 ~ $

Now it's at least easier to determine

Edit: Actually 1921680123 but anyway...
Back to top
View user's profile Send private message
cboldt
Veteran
Veteran


Joined: 24 Aug 2005
Posts: 1046

PostPosted: Sun Jun 03, 2018 9:50 am    Post subject: Reply with quote

Here is fodder for consideration ...

Code:
# Set colorful and informative prompt
# ===================================

# Prompt Colors
# Foreground                            Background
# ==========                            ==========
# 30 = Black, Grey                      40 = Black
# 31 = Red, BrightRed                   41 = Red
# 32 = Green, BrightGreen               42 = Green
# 33 = Brown, Yellow                    43 = Yellow
# 34 = Blue, BrightBlue                 44 = Blue
# 35 = Magenta, BrightMagenta           45 = Magenta
# 36 = Cyan, BrightCyan                 46 = Cyan
# 37 = White, BrightWhite               47 = White
#
# Attribute codes:
# 00=none 01=bold 04=underscore 05=blink 07=reverse 08=concealed

NORMAL_COLOR='\[\033[00m\]'
PROMPT_COLOR='\[\033[00;32m\]'
[ "$HOSTNAME" == "hypoid" ]   && PROMPT_COLOR='\[\033[00;33m\]'
[ "$HOSTNAME" == "hypoid-3" ] && PROMPT_COLOR='\[\033[00;35m\]'
[ "$HOSTNAME" == "involute" ] && PROMPT_COLOR='\[\033[00;36m\]'

case $TERM in
        xterm*|rxvt*)   TITLEBAR='\[\033]0;`pwd|rev|cut -b-14|rev`\007\]'
                        PROMPT_INFO1='[\u@\h] '
                        PROMPT_INFO2='\# $(pwd) > '     ;;

        screen*)        PROMPT_COMMAND='echo -n -e "\033k\033\\"'
                        TITLEBAR='\[\033k\033\\\]'
                        PROMPT_INFO1='\u@\h \[\033[01;33m\][${WINDOW:-$SSH_TTY}] '
                        PROMPT_INFO2='\# $(pwd) > '     ;;

        *)              TITLEBAR=''
                        PROMPT_INFO1='[\A \u@\h] '
                        PROMPT_INFO2='\# $(pwd) > '     ;;
esac

PS1="${TITLEBAR}\
${PROMPT_COLOR}${PROMPT_INFO1}\
${NORMAL_COLOR}${PROMPT_INFO2}"

unset TITLEBAR PROMPT_COLOR NORMAL_COLOR PROMPT_INFO1 PROMPT_INFO2


Use a slightly different $PS1 for user "root", namely color RED for the \u@\h part (regardless of $HOSTNAME), and \$ instead of > at the end of the prompt.
Back to top
View user's profile Send private message
khayyam
Watchman
Watchman


Joined: 07 Jun 2012
Posts: 6227
Location: Room 101

PostPosted: Sun Jun 03, 2018 11:43 am    Post subject: Reply with quote

tuggbuss wrote:
i did find a quick fix, changing my host name on the other machine to is't IP.

tuggbuss ... there is a reason it's called a "name" ... domain names are seperated by periods, eg, host.domain.tld ... so, what do you think something requiring "host" information about your FQDN will think is the 'host', 'domain', 'top limited domain' parts, if the 'host' part is period seperated? ... see restrictions on valid hostnames.

Anyhow ...
Code:
reset='\e[0m'       # reset
black='\e[0;30m'    # black
red='\e[0;31m'      # red
green='\e[0;32m'    # green
yellow='\e[0;33m'   # yellow
blue='\e[0;34m'     # blue
purple='\e[0;35m'   # purple
cyan='\e[0;36m'     # cyan
white='\e[0;37m'    # white

if [[ -z $SSH_CLIENT && $UID -gt 0 ]] ; then
    PS1="[\[$yellow\]\u\[$reset\]@\[$yellow\]\h\[$reset\]:\[$blue\] \W\[$reset]$ "
elif [[ -z $SSH_CLIENT && $UID -eq 0 ]] ; then
    PS1="[\[$red\]\u\[$reset\]@\[$red\]\h\[$reset\]:\[$blue\] \W\[$reset]$ "
elif [[ -n $SSH_CLIENT && $UID -gt 0 ]] ; then
    PS1="[\[$yellow\]\u\[$reset\]@\[$green\]\h\[$reset\]:\[$blue\] \W\[$reset]$ "
elif [[ -n $SSH_CLIENT && $UID -eq 0 ]]; then
    PS1="[\[$red\]\u\[$reset\]@\[$green\]\h\[$reset\]:\[$blue\] \W\[$reset]$ "
fi

HTH & best ... khay
Back to top
View user's profile Send private message
mv
Watchman
Watchman


Joined: 20 Apr 2005
Posts: 6747

PostPosted: Sun Jun 03, 2018 2:14 pm    Post subject: Reply with quote

You might also try set_prompt (provided by the mv overlay).
Back to top
View user's profile Send private message
tuggbuss
Apprentice
Apprentice


Joined: 20 Mar 2017
Posts: 222

PostPosted: Sun Jun 03, 2018 2:36 pm    Post subject: Reply with quote

khayyam wrote:
tuggbuss wrote:
i did find a quick fix, changing my host name on the other machine to is't IP.

tuggbuss ... there is a reason it's called a "name" ... domain names are seperated by periods, eg, host.domain.tld ... so, what do you think something requiring "host" information about your FQDN will think is the 'host', 'domain', 'top limited domain' parts, if the 'host' part is period seperated? ... see restrictions on valid hostnames.

Anyhow ...
Code:
reset='\e[0m'       # reset
black='\e[0;30m'    # black
red='\e[0;31m'      # red
green='\e[0;32m'    # green
yellow='\e[0;33m'   # yellow
blue='\e[0;34m'     # blue
purple='\e[0;35m'   # purple
cyan='\e[0;36m'     # cyan
white='\e[0;37m'    # white

if [[ -z $SSH_CLIENT && $UID -gt 0 ]] ; then
    PS1="[\[$yellow\]\u\[$reset\]@\[$yellow\]\h\[$reset\]:\[$blue\] \W\[$reset]$ "
elif [[ -z $SSH_CLIENT && $UID -eq 0 ]] ; then
    PS1="[\[$red\]\u\[$reset\]@\[$red\]\h\[$reset\]:\[$blue\] \W\[$reset]$ "
elif [[ -n $SSH_CLIENT && $UID -gt 0 ]] ; then
    PS1="[\[$yellow\]\u\[$reset\]@\[$green\]\h\[$reset\]:\[$blue\] \W\[$reset]$ "
elif [[ -n $SSH_CLIENT && $UID -eq 0 ]]; then
    PS1="[\[$red\]\u\[$reset\]@\[$green\]\h\[$reset\]:\[$blue\] \W\[$reset]$ "
fi

HTH & best ... khay


Thank you, haven't rebooted yet, i'll fix that
Back to top
View user's profile Send private message
khayyam
Watchman
Watchman


Joined: 07 Jun 2012
Posts: 6227
Location: Room 101

PostPosted: Sun Jun 03, 2018 8:40 pm    Post subject: Reply with quote

Zucca wrote:
This, however, is more complicated if you have an ongoing screen/tmux session on the machine. I know tmux has the status line which one can alter. So using it as an indicator whether the current attached client is via ssh or not might work.

Zucca ... well the '#h:#S' (host:session normally on 'status-left') is set from the 'hostname' on which tmux is run, so a ssh from a window of that session will not reflect the remote host that ssh is connected to. If tmux is run remotely then you get this for free on the status line, but it doesn't indicate the ssh session, only the tmux session. For this reason it's best set in PS1, as either way you have a clear indication of what terminal is on what host.

There are other indicators that could be used, like setting the window title, but these need to be set on execution (otherwise they remain the same regardless of what happens within the session/window) ... zsh provides 'precmd' for such a purpose, and if used it'll update the status line on command execution. For bash I can't say what the standard method of doing this is, I expect via PROMPT_COMMAND and using Xterm escape sequences.

best ... khay
Back to top
View user's profile Send private message
mv
Watchman
Watchman


Joined: 20 Apr 2005
Posts: 6747

PostPosted: Mon Jun 04, 2018 6:25 am    Post subject: Reply with quote

khayyam wrote:
Zucca wrote:
This, however, is more complicated if you have an ongoing screen/tmux session on the machine. I know tmux has the status line which one can alter. So using it as an indicator whether the current attached client is via ssh or not might work.

There are other indicators that could be used, like setting the window title

You an do this from PS1; in fact, the prompt generated by set_prompt I mentioned earlier does this.
However, there is the problem that if you run screen/tmux from ssh then either all inherited sessions/windows will also inherit the SSH_CONNECTION environment variable, so using that variable is not completely reliable as an indicator whether you are remote. The just released version of set_promp tests this variable "live" (i.e. if you set/unset it, the indication color will change), although I can hardly imagine setups where this improves the situation automatically (of course, you can fix it now manually by setting/unsetting SSH_CONNECTION appropriately).
However, the displayed host name is always correct (only the colored "remote indicator" might be false) because it is not possible to "transfer" a running screen/tmux to a different host.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum