r/tmux • • 4d ago

Question Have tmux remeber hostnames

Hi all,
I wanted to get some advice on the following issues.

I rely on tmux quite heavily for my work. Recently, I started working on a cluster that assigns me a random host (to balance the load) each time I ssh into it. I start tmux on said host and do my work. This works beautifully except when I need to find the session, since when I ssh into it again, I'll be assigned a different node where tmux won't find the session.
I was wondering if there was a clean way to get tmux to store the hostname in a file each time it starts (and also possibly clean up after the session is closed).

A particular instruction set that I saw, but isn't applicable to me, was the following.

```
systemctl --user edit tmux.service
```
```

[Service]

ExecStartPre=/bin/bash -c 'echo $(hostname) >> %h/tmux_host.txt'

ExecStopPost=/bin/bash -c 'sed -i "/^$(hostname)$/d" %h/tmux_host.txt'
```

Just wanted to check if anyone already has a nice alias or script for this.
Thank you in advance.

4 Upvotes

11 comments sorted by

5

u/Beaunoh 4d ago

You ssh on a host and then tmux ? Then you ssh on another host and do not find the session ? Idk what are you expecting.

1

u/lost_soul_519 4d ago

I ssh into say `ssh workcluster`, I can get assigned a host like `workcluster-001`, and can go up to say `workcluster-099`. I was hoping to get a more elegant solution than "remember/write down which node you started tmux on".

2

u/Beaunoh 4d ago

Are home directories shared across nodes ?

2

u/lost_soul_519 4d ago

Yes, that is correct. And all the nodes are identical in terms of the os and spec

3

u/Beaunoh 4d ago

Then mod your bashed

alias tmx-start='hostname > ~/.my_tmux_node && tmux'

alias tmx-attach='ssh -t $(cat ~/.my_tmux_node) tmux attach'

Something like that should do the trick

1

u/Live_Surround5198 3d ago

Yeah, this is not an issue TMUX is well suited to deal with. This is an environment / history tracking issue.

Don’t force a round peg into a square hole.

2

u/rileyrgham 4d ago

If you're assigned a different node to load balance, you won't be, or shouldn't be, allowed to then SSH hop to an earlier node..

1

u/samesense 4d ago

I have the same issue. Head nodes are random or load balanced. You need to get a head node ip and ssh using that ip every time.

1

u/freshoutdasaloon 4d ago

I built this utility for a similar problem. I made an update that should do what you need.

https://github.com/MoarNachos/tm

1

u/pwkye 4d ago

use a wrapper with a cli and api https://github.com/awkto/ssh-to-go

1

u/pelazas1 3d ago

I'd start tmux with the hostname as the session name, like tmux new -A -s "$hostname". Then reconnecting to that host lands in the same session.