User Tools

Site Tools


tutorial:tmux

tmux Guide

Tmux it is a piece of software that runs on the terminal. It is short for Terminal Multiplexer. It can create terminal sessions that keeps running even when you log off the system, and when you log in again you can hook into the same session and continue working. Use cases for this include -but are not limited to-:

  • Monitoring progress on some command you ran.
  • Running a server on the background and keeping track of its requests.

There's no doubt that it's a useful tool, but it can be unintuitive to use. That's why we include the most important commands in this guide needed to get you started.

Installation

tmux is already installed on all our machines. In case you want to install it on your machine too, the installation is straight forward on major operating systems.

Ubuntu/Debian

sudo apt install tmux

MacOS

brew install tmux

Windows

since Windows 10, there has been an ubuntu subsystem included that can be used and tmux can be installed with running the linux command in the ubuntu terminal.

Trial Run

Let's first check if any tmux sessions are running

tmux ls

If this is the first tmux command you run you should expect an output like this:

Next, we create a tmux session called TmuxSession

tmux new -s TmuxSession

Then tmux will open a new terminal session where we can run any command we want, for the purposes of this guide, we detach temporarily from the session to return to our old terminal window with using the keys

<ctrl-b> + d

tmux uses the concept of a bind-key. It is a combination of keys pressed together to tell tmux to listen to keyboard input as commands, in the previous command we used the default bind-key which is the control button + the letter b. This tells tmux to check the following input for commands. The next letter we typed is d which tells tmux to detach from the current session. There are many other commands that can be found in the extra resources section.

A very useful command to see if others are logged in the system is

who

this also works with tmux and it shows if anyone else is running a tmux session on the same machine.

If we run this command we will see that the terminal session TmuxSession is still running. In order to open it again, or more accurately, to attach to it again we run

tmux a -t TmuxSession

we can then exit this session as we would exit any normal terminal session, by running

exit

now if we run tmux ls again we confirm this by seeing that no sessions are live.

Basic Commands

This is a list of the most common tmux commands you will need when starting out. It includes all commands mentioned in the previous trial run and some other useful commands. You can find a more comprehensive list in the extra resources section.

start new without session name (it will assign a number instead to the session name):

tmux

start new with session name:

tmux new -s myname

attach:

tmux a  #  (or at, or attach)

attach to named:

tmux a -t myname

list sessions:

tmux ls

kill session:

tmux kill-session -t myname

once inside a session, you can detach from it with:

<ctrl-b> + d 

Sharing a session across multiple users

tmux allows for a session(s) to be shared with multiple users. This is achieved by specifying a socket (which is just a file) that both users have access to.

Here's a trial run for some for two users userA and userB.

Assuming userA runs this command:

tmux -S /tmp/shareds new -s shared

the first argument passed in this command is an example socket, the rest of the arguments are as before. So this creates a new shared session called shared.

Then for userB to attach to the same session he would have to run:

tmux -S /tmp/shareds a -t shared

More info can be found in the extra resources section.

Extra Resources

  • A more comprehensive list of most used commands here.
  • Sharing a session between users here.
  • Ubuntuusers tmux guide.
  • A guide to screen; an alternative to tmux.
tutorial/tmux.txt · Last modified: 2020/07/21 15:56 by mohammed.ghannam

Page Tools