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.

As you might know, by default, the hostname of an AWS instance is something like ip-12-34-56-78.us-west-2.compute.internal, so when I ssh to this host, the tab name is changed to root@ip-12-34-56-78 (a bit of difficult for me to identify which is which).

Since I have the following in the ~/.ssh/config:

Host mail.domain.com
    Hostname 1.2.3.4
    User root

Host web.domain.com
    Hostname 5.6.7.8
    User root

I'm wondering that is there any way to change the tab name to the "alias" hostname of the remote host I'm connected to (instead of the "real" hostname):

iTerm tab title when ssh


My current PS1 settings:

PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '

I have tried to add a ssh wrapper into ~/.bashrc:

ssh() {
    echo -ne "\033]0;${@: -1}\007"
    exec ssh $@
}

then . ~/.bashrc and test by running ssh web.domain.com:

  • while connecting: the tab name is changed to web.domain.com, as expected
  • when connected: it is overwritten with root@ip-5-6-7-8

Why? Is there something related to PS1 here?


UPDATE Fri Jun 6 21:58:04 ICT 2014

There is something quite strange going on: looks like this only happened with AWS instance. With other hosts, the tab name remain after logging in.

PS1 on the AWS instance:

# echo $PS1
[\u@\h \W]\$

PS1 on the other hosts:

~ echo $PS1
\[\]\[\][\[\]\t\[\]] \u\[\]@\[\]\h\[\]\[\]:\[\]\w\[\] \[\]

I also have tried to set the PS1 variable on the AWS to the value of working host but it didn't help.

share|improve this question
    
have you tried setting PROMPT_COMMAND variable in your .bashrc? –  cuonglm Jun 6 at 6:02
    
@Gnouc: set to what? How can I take the first argument when ssh and put it into PROMPT_COMMAND? My current setting: export PROMPT_COMMAND='echo -ne "\033]0;$(basename "$(dirname "$PWD")")/${PWD##*/}\007"'. –  quanta Jun 6 at 6:31
    
It's probably not quite what you want but it might be worth a look: Try curl http://169.254.169.254/latest/meta-data/public-hostname from inside the VM and see if this value is better suited for a tab name then what hostname gets you. (More about metadata) –  sr_ Jun 6 at 6:44
    
@sr_: I know that command, it does not help in this case. The thing is when I opened multiple tabs, I would like to quick switch to a specific tab by pressing <kbd>command</kbd> + tab number. –  quanta Jun 6 at 6:58
    
@quanta: maybe export PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}: ${PWD/#$HOME/~}\007"'. It seems that you want to change HOSTNAME to the alias name which define in .ssh/config? –  cuonglm Jun 6 at 7:22

1 Answer 1

up vote 1 down vote accepted

That should be because upon logging into the remote shell session, that server's PS1 is sending you back the same \033]0;title\007 command sequence which makes your terminal program intercept and display accordingly. You really don't have any effect of editing your PS1 on local workstation.

I had a similar requirement and what I did is on the remote shell's bashrc, I put something like the following

PS1="\033]0;(tools)\007\015[\u@\h \W]# "

The tools is the designated name by which I wanted to identify the title. Effectively, the title shows it correctly in the terminal, and the command prompt is what I have normally.

share|improve this answer
    
+1. I know that I can do this or simply change the hostname on the remote host, but why it does not happen with other hosts (that has PS1 is set to \[\]\[\][\[\]\t\[\]] \u\[\]@\[\]\h\[\]\[\]:\[\]\w\[\] \[\]? See my updated question. –  quanta Jun 6 at 15:13
    
echo $PS1 > ps1.out in your server and check what do you see in that file. Probably that PS1 does not have the ESC sequence embedded into it. You can't see it by just echo $PS1 in console. Did I get your question correct ? –  roby Jun 6 at 15:20
    
It has the same value as I posted \[\]\[\][\[\]\t\[\]] \u\[\]@\[\]\h\[\]\[\]:\[\]\w\[\] \[\]. One more question: with my above ssh wrapper, whenever I press ctrl-D or type exit it close my tab instead of close the session and return the local prompt(quanta@MacBook-Pro). –  quanta Jun 6 at 15:31
    
You should have something in your server's /etc/bashrc or so which does this trick. In my case for xterm like, it sets PROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"' which seems to do this trick. The reason for your tab to exit is because you are exec'ing ssh and that image replaces your bash process. Now when you exit from the shell, ssh client dies along with that since there is no more bash process to hold you back on terminal, it completely exits. –  roby Jun 6 at 15:39
1  
Why don't use it just this way. Don't exec ssh. I tried it myself and works => ssh() { echo -ne "\033]0;${@: -1}\007" /usr/bin/ssh $@ } –  roby Jun 6 at 16:14

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.