How to automatically start tmux on SSH session?

Alex Ryan

I have ten or so servers that I connect to with SSH on a regular basis. Each has an entry in my local computer's ~/.ssh/config file.

To avoid losing control of my running process when my Internet connection inevitably drops, I always work inside a tmux session. I would like a way to have tmux automatically connect every time an SSH connection is started, so I don't have to always type tmux attach || tmux new after I SSH in.

Unfortunately this isn't turning out to be as simple as I originally hoped.

  • I don't want to add any commands to the ~/.bashrc on the servers because I only want it for SSH sessions, not local sessions.
  • Adding tmux attach || tmux new to the ~/.ssh/rc on the servers simply results in the error not a terminal being thrown after connection, even when the RequestTTY force option is added to the line for that server in my local SSH config file.
Alex Ryan

Alright, I found a mostly satisfactory solution. In my local ~/.bashrc, I wrote a function:

function ssh () {/usr/bin/ssh -t $@ "tmux attach || tmux new";}

which basically overwrites the ssh terminal function to call the built-in ssh program with the given arguments, followed by "tmux attach || tmux new".

(The $@ denotes all arguments provided on the command line, so ssh -p 123 user@hostname will be expanded to ssh -t -p 123 user@hostname "tmux attach || tmux new")

(The -t argument is equivalent to RequestTTY Force and is necessary for the tmux command.)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to start tmux with attach if a session exists

From Dev

How to access tmux session created in an ssh session locally?

From Dev

How to pass command keys to a remote tmux session when you SSH from a local tmux session into a remove tmux session

From Dev

How to launch tmux –automatically– when konsole/yakuake start?

From Dev

How to launch tmux –automatically– when konsole/yakuake start?

From Dev

How to automatically start tmux when opening gnome-terminal

From Dev

Automatically kill tmux session on session end?

From Dev

Is it possible to start tmux visible session

From Dev

Start SSH automatically on boot

From Dev

How to close a tmux session

From Dev

tmux reattach to session automatically after kill and restart

From Dev

How can I start new window in the same screen session automatically?

From Dev

How to start tmux with no configuration?

From Dev

tmux session closes when ssh terminal closes

From Dev

tmux session killed when disconnecting from ssh

From Dev

tmux session closes when ssh terminal closes

From Dev

bash script to start another script in a tmux session

From Dev

Start shell with tmux and attach only to a session if it is unattached

From Dev

How to detach a tmux session that itself already in a tmux?

From Dev

How can I start an ssh session with a script without redirecting stdin?

From Dev

How to write in bash to ssh to different machine and create tmux session then run some command in it

From Dev

How to write in bash to ssh to different machine and create tmux session then run some command in it

From Dev

How to configure conky in a tmux session?

From Dev

Detach from tmux session and close ssh session with 1 command

From Dev

Bash script to start a detached tmux session, run a script, and keep the tmux session alive

From Dev

How to start sshd automatically

From Dev

Run an existent tmux session at SSH login or create if not exist

From Dev

In tmux session, cannot connect to X server after ssh'ing

From Dev

Does losing an ssh session kills commands running in tmux?

Related Related

  1. 1

    How to start tmux with attach if a session exists

  2. 2

    How to access tmux session created in an ssh session locally?

  3. 3

    How to pass command keys to a remote tmux session when you SSH from a local tmux session into a remove tmux session

  4. 4

    How to launch tmux –automatically– when konsole/yakuake start?

  5. 5

    How to launch tmux –automatically– when konsole/yakuake start?

  6. 6

    How to automatically start tmux when opening gnome-terminal

  7. 7

    Automatically kill tmux session on session end?

  8. 8

    Is it possible to start tmux visible session

  9. 9

    Start SSH automatically on boot

  10. 10

    How to close a tmux session

  11. 11

    tmux reattach to session automatically after kill and restart

  12. 12

    How can I start new window in the same screen session automatically?

  13. 13

    How to start tmux with no configuration?

  14. 14

    tmux session closes when ssh terminal closes

  15. 15

    tmux session killed when disconnecting from ssh

  16. 16

    tmux session closes when ssh terminal closes

  17. 17

    bash script to start another script in a tmux session

  18. 18

    Start shell with tmux and attach only to a session if it is unattached

  19. 19

    How to detach a tmux session that itself already in a tmux?

  20. 20

    How can I start an ssh session with a script without redirecting stdin?

  21. 21

    How to write in bash to ssh to different machine and create tmux session then run some command in it

  22. 22

    How to write in bash to ssh to different machine and create tmux session then run some command in it

  23. 23

    How to configure conky in a tmux session?

  24. 24

    Detach from tmux session and close ssh session with 1 command

  25. 25

    Bash script to start a detached tmux session, run a script, and keep the tmux session alive

  26. 26

    How to start sshd automatically

  27. 27

    Run an existent tmux session at SSH login or create if not exist

  28. 28

    In tmux session, cannot connect to X server after ssh'ing

  29. 29

    Does losing an ssh session kills commands running in tmux?

HotTag

Archive