Skip to content

Instantly share code, notes, and snippets.

@imneonizer
Last active September 27, 2019 10:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imneonizer/7bae0d4c2776f5fc3bd50ea04c734640 to your computer and use it in GitHub Desktop.
Save imneonizer/7bae0d4c2776f5fc3bd50ea04c734640 to your computer and use it in GitHub Desktop.

What is tmux

tmux is a ssh session logging/sharing tool which we can use to reconnect to a ssh session even if we lost the connection in between.

How to install

First we need to install it in the remote machine, to do so ssh into the remote machine and run this command

sudo apt-get install tmux

For Example:

client@client_machine:~$ ssh host@192.168.0.142

Welcome to Ubuntu 18.04.3 LTS (GNU/Linux 4.9.140-tegra aarch64)

Last login: Fri Sep 27 15:42:31 2019 from 192.168.0.163

host@remote_machine:~$ sudo apt-get install tmux

Reading package lists... Done
Building dependency tree       
Reading state information... Done
.
.
host@remote_machine:~$ 

After tmux is installed in the remote machine. we can create new sessions, list running sessions, detach from sessions, attach to different sessions and yes we can close the connection from our client machine and reconnect via a new ssh session and can reconnect back to the old tmux session, so lets do that.

Using tmux

For using tmux sessions we fist need to ssh into the remote machine so that we can create a new session.

Step 1 - Create a new session

first we need to ssh into the remote machine and after that we can create a tmux session.

client@client_machine:~$ ssh host@192.168.0.142

Welcome to Ubuntu 18.04.3 LTS (GNU/Linux 4.9.140-tegra aarch64)

Last login: Fri Sep 27 15:42:31 2019 from 192.168.0.163

host@remote_machine:~$ tmux new -s my_session

This will create a new tmux session as my_session, later we can use this session name to reconnect. so you can do your stuff here and incase if you want to close the ssh connection but retain the session you can simply detach from the current session by pressing ctrl+b+: this will highlight a yellow ribbon at the bottom of the terminal : there you have to type detach and press Enter key and you will get out of the tmux session but you will still be in the ssh session so now you can close the ssh session by pressing ctrl+d on the keyboard or closing the terminal itself.

step 2 - Reconnecting to the old tmux session

For this we need to do a ssh connection to the remote machine first and then we can reconnect to the old session

client@client_machine:~$ ssh host@192.168.0.142

Welcome to Ubuntu 18.04.3 LTS (GNU/Linux 4.9.140-tegra aarch64)

Last login: Fri Sep 27 15:42:31 2019 from 192.168.0.163

host@remote_machine:~$ tmux list-sessions
my_session: 1 windows (created Fri Sep 27 16:02:42 2019) [80x23]
host@remote_machine:~$

tmux list-sessions will show a list of all the active tmux sessions on the remote machine, yes you can create more than one sessions. So, now it's time to connect to our old tmux session my_session.

host@remote_machine:~$ tmux list-sessions
my_session: 1 windows (created Fri Sep 27 16:02:42 2019) [80x23]
host@remote_machine:~$ tmux attach -t my_session

And after this you will be back to where you left off, this comes in really handy when you have to close the connection from client side but still wants the process to be running on the remote side and later you can reconnect to that session again. and yes the tmux sessions will be active as long as the remote machine is running and you have carefully detached from the session instead of closing the entire session.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment