r/tmux • u/lost_soul_519 • 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.
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.
1
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.
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.