Is it possible to set Gnome Terminal's title to "user@host" for whatever host I'm connected to?

30,517

Solution 1

Yes. Here's an example for bash using PS1 that should be distro-agnostic:

Specifically, the escape sequence \[\e]0; __SOME_STUFF_HERE__ \a\] is of interest. I've edited this to be set in a separate variable for more clarity.

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
force_color_prompt=yes

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
        # We have color support; assume it's compliant with Ecma-48
        # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
        # a case would tend to support setf rather than setaf.)
        color_prompt=yes
    else
        color_prompt=
    fi
fi

TITLEBAR='\[\e]0;\u@\h\a\]'
# Same thing.. but with octal ASCII escape chars
#TITLEBAR='\[\033]2;\u@\h\007\]'

if [ "$color_prompt" = yes ]; then
    PS1="${TITLEBAR}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\W\[\033[00m\]\$ "
else
    PS1="${TITLEBAR}\u@\h:\W\$ "
fi
unset color_prompt force_color_prompt

Also note that there can be many ways of setting an xterm's title, depending on which terminal program you are using, and which shell. For example, if you're using KDE's Konsole, you can override the title setting by going to Settings->Configure Profiles->Edit Profile->Tabs and setting the Tab title format and Remote tab title format settings.

Konsole titlebar settings dialog

Additionally, you may want to check out:

Solution 2

Here's a version of the SSH bash script that I use which sets the remote server's title and command prompt without making any changes to the remote server.

my_ssh.sh:

#!/bin/bash
SETTP='MY_PROMPT="$HOSTNAME:$PWD\$ "'
SETTP="$SETTP;"'MY_TITLE="\[\e]0;$HOSTNAME:$PWD\a\]"'
SETTP="$SETTP;"'PS1="$MY_TITLE$MY_PROMPT"'
ssh -t $1@$2 "export PROMPT_COMMAND='eval '\\''$SETTP'\\'; bash --login"

You can invoke it by calling ./my_ssh.sh username hostname

Solution 3

The following works for me (probably only on gnome-terminal):

comp@home$ cat /usr/bin/ssh
#!/bin/bash    
echo -ne "\033]0;${1}\007"
ssh_bkup "$@"

Where ssh_bkup command is just basic 'ssh' with a changed name, which is called right after the echo command changes the current terminal's title.

Share:
30,517
Naftuli Kay
Author by

Naftuli Kay

Updated on September 18, 2022

Comments

  • Naftuli Kay
    Naftuli Kay over 1 year

    I'd like to set the terminal title to user@host so I can easily tell which machine I'm connected to from the window title. Is there a way to do this from SSH or from GNOME Terminal?

  • Naftuli Kay
    Naftuli Kay almost 13 years
    So do I need to set this on all of the servers I am connecting to or on my own local machine?
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' almost 13 years
    @TKKocheran: You need to do this on every machine where you're running a shell. If you only want to do it for remote logins, put \h in that prompt only if $SSH_CLIENT is non-empty.
  • TrinitronX
    TrinitronX almost 13 years
    @TK : Yes, you will need to set this on all the servers you connect to. The PS1 variable is local to your current shell (even on a remote host), not the terminal program (ie: gnome-terminal).
  • Naftuli Kay
    Naftuli Kay almost 13 years
    I assume that this will apply differently on servers not running Debian. Can you edit your answer to provide for servers running, let's say, Fedora/Red Hat derivatives?
  • Naftuli Kay
    Naftuli Kay almost 13 years
    Awesome. So is this supposed to change the window title or just the bash prompt?
  • TrinitronX
    TrinitronX almost 13 years
    @TK : Edited my answer to make it distro-agnostic, plus some other helpful resources.
  • TrinitronX
    TrinitronX almost 13 years
    @TK : I tested this using SSH to various hosts from an Ubuntu 11.04 machine... So the title bar was set correctly there... If it doesn't work, try adding this sequence to the front: \[\e]2;\u@\h\a. (Also edited my response with that info)
  • Naftuli Kay
    Naftuli Kay almost 13 years
    \m/ it was the \[\e]2;\u@\h\a that made the difference. You. Win. :)
  • TrinitronX
    TrinitronX almost 13 years
    @TK : Also, Some terminal programs (like Konsole) have their own setting for the title bar that can override that set in the prompt. I'd forgotten to turn mine off initially to test the PS1. My first answer included this in the debian_chroot part, but I removed it haphazardly, and didn't even notice the mistake ;-)
  • Laurens Rietveld
    Laurens Rietveld over 9 years
    I doubt this works when starting another bash session after ssh login though (e.g. when using screen)
  • X Tian
    X Tian almost 9 years
    wouldn't an alias solutions be better than renaming commands to something non-standard ?
  • Gauthier
    Gauthier about 8 years
    This works fine for me, also gnome-terminal. ~/bin has priority in my path, so I placed your script in my ~/bin/ssh. The last row explicitly calls to /usr/bin/ssh. This way, other users still use the standard ssh when logged in on that machine, and (since our home directories are on server, LDAP accounts) I get the functionality on whichever machine I am logged in on.
  • Yuriy Mankovskiy
    Yuriy Mankovskiy over 7 years
    Good call, @Gauthier. Seems a better solution.
  • alper
    alper almost 4 years
    Can this work for zsh?