Как сделать так, чтобы заголовок терминала прикреплялся на Mac

1418
Classified

Я не уверен, что это проблема с моим Mac, с которой я нуб, или это настройка сервера, на который я захожу.

Что я делаю, так это:

  1. Откройте новое окно в терминале
  2. SSH на сервер CentOS. Мой логин - это bash.
  3. Нажмите на Shell-> Изменить заголовок
  4. Измените заголовок и заголовок вкладки на abc123. Он отображается в строке заголовка терминала как abc123-abc123-ssh-100x24.
  5. В терминале запустите sudo su, чтобы изменить пользователя на root. Обратите внимание, что название теперь изменилось на root @ blah: / home / myuser-ssh-100x24. Если я выйду из root, название заголовка все равно будет новым.

Как сделать так, чтобы название заголовка оставалось как abc123 и не менялось, когда я su другим пользователям? Заранее спасибо за помощь.

1

1 ответ на вопрос

5
Daniel Beck

The title is set from the server you're connecting to. Chances are, /etc/bashrc or a similar file defines PROMPT_COMMAND to set the title. On CentOS 6, it looks like this:

 if [ -z "$PROMPT_COMMAND" ]; then case $TERM in xterm*) if [ -e /etc/sysconfig/bash-prompt-xterm ]; then PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm else PROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "$" "$" "$"' fi ;; [... more of the same ...] esac fi 

To prevent this, make sure to set PROMPT_COMMAND= in root's and your own bash configuration files on the server you SSH into (e.g. ~/.bashrc).

Alternatively, add PROMPT_COMMAND to /etc/sudoers (env_keep) and /etc/ssh/sshd_config (AcceptEnv) to inherit it from the local system, and set it to a value that does not affect the title bar.

In this specific case, you could also set your Terminal declare a different Terminal, as this is only set for xterm* on CentOS (Terminal » Preferences… » Settings » (Select Profile) » Advanced » Declare Terminal as).


Terminal.app does not seem to allow to lock the title – you're probably expected to change the configuration that sets it as described above. You could always check out a different terminal, such as iTerm. It supports keeping the profile name in the title even the title changes.


And here's a dirty hack to keep your terminal title without changing the configuration of all servers you SSH into:

In the same shell you SSH into your Linux box, before you do that, run the following command:

while true ; do printf "\033]0;%s\007" "My Terminal Title" ; sleep 1 ; done & 

This will execute a loop in the background that changes the terminal title to My Terminal Title once a second, overriding any changes performed in your SSH session.

Once you're done, use job control in your shell to stop it (jobs and kill %1 if it's the first job), or fg to bring it to foreground, and cancel with Ctrl-C.