Getting started with TMUX

      No Comments on Getting started with TMUX

This is one of those ‘I must be living under a rock’ things.  I’m not sure how I’ve never heard of TMUX before but it’s really pretty awesome.  I initially came across it when searching for a way to share a terminal session with another user.  It does that quite well but it’s also a great terminal session manager allowing for pane, window, and session management.  Let’s take a look at a quick example to show you what I mean.

Here we have a server called ‘tmuxtest’.  The server already has TMUX on it by default but if it’s not there you can easily install it (sudo apt-get install tmux, etc).  So let’s say I want to start a new session.  The easiest way to do this is to just type ‘tmux’..

Now we’re in TMUX.  We are in what’s called a ‘session’.  The session can contain multiple panes and multiple windows.  For instance, if I wanted to create a second pane I could do by pressing ‘Ctrl-b + %’…

Notice that the screen on the right, the new one, has a green boarder around it.  That’s my active screen.  Now if I want to split this screen horizontally I can use ‘Ctrl-b + “‘…

You might be figuring it out, but ‘Ctrl-b’ is the magic key sequence in TMUX.  Here’s a list of commands that relate to panes…

# Panes
## Split pane vertically
Ctrl-b %
## Split pane horizontally
Ctrl-b "
## Navigate panes
Ctrl-b <arrow key>
## Exit pane
type 'exit' or Ctrl-d
## Make a pane go full screen or shrink
Ctrl-b z

As you can see, we can select different panes pretty easily by using the arrow keys and end them as we typically end windows with ‘exit’.  So that’s all well and good, but that didnt buy us much.  What else is there?

Well before I forget, remember that we’re in a session that I can attach and detach to.  Have you ever needed to start a download on a remote server and worried about losing your connection and having the download crash?  I have!  So why not run it in a session?  Simply start a TMUX session, start the download, and then detach from the session (Cltrl-b d)…

Now I shut my laptop and go home.  When I get home, I get on VPN, log back into the server, and see that the download is still running (the size is increasing) and the TMUX session is still there which I can reconnect to…

I can reconnect to the session by using ‘tmux attach -t <session ID or name>’ which in this case is ‘3’…

Pretty cool huh?  Here are some commands that are relevant to dealing with sessions…

# Sessions
## Create session
tmux new -s <name>
## Detach
Ctrl-b d
## Show sessions
tmux ls
## Attach to session
tmux attach -t 0
## Rename session
tmux rename-session -t <session number or name> <session number or name>
## Kill session
tmux kill-session -t <session number or name>

And before I get too far ahead of myself, I should talk about windows as well.  A session can have multiple windows.  For instance, if I start a TMUX session I can press ‘Ctrl-b c’ (I’ll do it twice)..

Notice how along the bottom I now have 3 sessions?  They’re labelled 0, 1, and 2.  I’m currently on the ‘2’ sessions which we can tell by the asterisk being next to it.  I can change between windows by pressing ‘Ctrl-b p’ or ‘Ctrl-b n’ which stand for previous and next.  Here are some other commands related to windows…

# Windows
## Create Window
Ctrl-b c
## Switch to next window
Ctrl-b n
## Switch to previous window
Ctrl-b p
## Switch to specific window
Ctrl-B <number>
## Rename current window
Ctrl-b ,

Ok – so now for the terminal sharing.  We know how to create a session and how to attach and detach from them.  Surprisingly, that’s all we need to know to share a session between two users using the same account.  For instance, let’s have two users log into the server both using the ‘user’ account. We’ll have the first user create a session called ‘watchme’ with the command ‘tmux new -s watchme’.  Now if we have another person log in to the same server, with the same user account, they can see that session by using ‘tmux ls’…

All the second user has to do is attach to that same session using the ‘tmux attach -t watchme’ command…

Pretty slick huh?  But what about if you have two different users on the same server?  Unfortunately, it’s not as easy to share sessions between multiple users, but it is still possible by creating a socket that both users can reach.  For instance, if we log into the same server with two different users we can see that by default user2 can’t see user1’s sessions…

However, we can specify a socket file for TMUX to use and make it accessible by both users like this…

Notice how we create the session under user1 as normal with the exception of passing a parameter to the ‘-S’ flag.  The ‘-S’ flag tells TMUX to create a socket at the specified location, in this case ‘/var/tmp/tmuxsocket’. Then I detach from the session, and change the permissions on the file so that all users can access it with the ‘chmod 777 /var/tmp/tmuxsocket’ command.  Now, user2 can see the session by telling TMUX where to look for the socket.  Is this secure?  Gosh no.  Is this probably something you should only do with another user that you fully trust?  Yes.  But if you fit that criteria, it’s drop dead simple to share a session.  That or you can both use the same account as we saw in the first example.

In any case – TMUX is pretty awesome.  I plan on making it a permanent addition to my toolbox.

Leave a Reply

Your email address will not be published. Required fields are marked *