Changing the title of an ssh connection without modifying remote machines

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?


Solution 1:

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.

Solution 2:

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.

Solution 3:

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```