5

Is it possible to set the title of an ssh connection by just changing something on my local machine?

I mean I want ubuntu@ip-10-50-10-152 in the following picture to be changed to a name I like.

And I don't want to change any settings on the remote machine.

enter image description here

The IP address is difficult to remember, how can I change it to a more readable name?

I want to map each IP address to a different name.

The remote machines in my case are lots of Amazon EC2 instances. Some of them only exist for a few days. And new ec2s get created frequently.

I use iTerm2 as my macOS terminal. And I couldn't find any plugin that does the thing I want.

Or is there a better ssh management tool that can display custom text on the title of an ssh connection?

3 Answers 3

4

The best way (IMO) is to set the necessary variables in your bash_profile of your remote machine. I understand you don't want to modify anything on your remote but it's important to note that what you are doing is setting the environment variable PROMPT_COMMAND

When logged in to your remote, issue the command echo $HOSTNAME and echo $PROMPT_COMMAND. More than likely, you will get the hostname of your machine and the second will come up blank meaning that it's unset.

Setting the Title

Edit your .bash_profile found in your home directory on the remote. This is your file and will not affect the rest of the system. Add the following line:

PROMPT_COMMAND='echo -ne "\033]0;${HOSTNAME}\007"'

The \033]0; (Esc 0;) is the escape code to set the title. The ${HOSTNAME} is the name of your machine.

Save your profile, log out and log back in and your title bar should now reflect your hostname.

Connecting via hostname vs IP

There are two ways to do this:

  • Set the hostname in DNS
  • modify your local /etc/hosts file

I prefer to set this in DNS so that the name will be available to all computers across your network. Going under the assumption that you have a "consumer grade" router (like Linksys or Netgear), you just have to add the hostname to your DHCP static lease (see your router documentation for specifics)

If you're on a corporate network, the Network Admin can add a record to the DNS server pointing to your host.

If those two options aren't available, you can simply edit your local /etc/hosts file (need root permissions. Below is my /etc/hosts used as an example:

127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost
192.168.10.22   fatjoe

I added the hostname fatjoe to the end of the hosts file. When I save and exit, if I type ping fatjoe it immediately begins to ping the correct IP - the change is immediate.

3
  • I found that editing /etc/hostname on the remote machine works for me. The remote machine is Ubuntu 14.04. It's strange that editing /etc/hosts doesn't work in my case.
    – Brian
    Commented May 25, 2017 at 3:18
  • I still wish there's a method that doesn't require any modifications on the remote machine. Windows ssh client, such as Xshell, is able to do this. I just migrated from windows to mac.
    – Brian
    Commented May 25, 2017 at 3:23
  • When you are on the remote machine, what does the command hostname give you (before you edit the /etc/hostname file)? As far as it being a Windows/Mac thing, it sounds like Xshell was "bodging" this together. In all my years of working with BSD/Solaris/Windows/Mac, I always set this info on the remote - so it was the same everywhere.
    – Allan
    Commented May 25, 2017 at 11:08
1

Add the following to your ~/.ssh/config:

Host yourname
    HostName server.example.com
    Port 22
    User root

Then use ssh yourname and ‘yourname’ will be shown in the title bar.

5
  • 2
    No, this method doesn't work on my machine.
    – Brian
    Commented May 24, 2017 at 9:11
  • 'youname' only appears in the title bar during the establishment of the ssh connection.
    – Brian
    Commented May 24, 2017 at 9:13
  • After the connection is established, it changes back to ubuntu@ip-XX-XX-XX-XX
    – Brian
    Commented May 24, 2017 at 9:14
  • @Brian Strange, it works for me, I have ~10 entries in ssh config like this and it works for me both in Terminal and iTerm.
    – grg
    Commented May 24, 2017 at 9:19
  • There must be something else that affects the title of the tab.
    – Brian
    Commented May 24, 2017 at 9:56
1

I followed all the answer of this post and the official guide, but without success.

Checking up further, I discovered the ~/.bashrc on the target machine was overwriting $PS1, and thus putting back the default name.

If you have the same problem, it might be worth looking for those lines in ~/.bashrc and edit them as to show the title you want ($NICKNAME in my case) to appear on the terminal tab.

 case "$TERM" in
 xterm*|rxvt*)
     PS1="\[\e]0;${debian_chroot:+($debian_chroot)}$NICKNAME: \w\a\]$PS1"
     ;;
 *)
     ;;
 esac```

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .