this post was submitted on 11 Jul 2023
125 points (100.0% liked)

Linux

48372 readers
1713 users here now

From Wikipedia, the free encyclopedia

Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991 by Linus Torvalds. Linux is typically packaged in a Linux distribution (or distro for short).

Distributions include the Linux kernel and supporting system software and libraries, many of which are provided by the GNU Project. Many Linux distributions use the word "Linux" in their name, but the Free Software Foundation uses the name GNU/Linux to emphasize the importance of GNU software, causing some controversy.

Rules

Related Communities

Community icon by Alpár-Etele Méder, licensed under CC BY 3.0

founded 5 years ago
MODERATORS
 

I am fairly familiar with Linux, I've been using different distros for some years now and have done some config editing here and there. I am also a web developer and use the terminal quite a lot and so I always stumble on people's recommendation to use tmux and how good it is, but I never really understood what it does and, in layman's terms, how can it be useful and for what use cases.

Can you guys please enlight me a bit on this?

Thank you.

Edit: if my phrasing is a bit awkward or confusing I apologize since I am not an English native speaker. (Maybe that's why I never fully grasped what tmux is from other explanations xD)

Edite: Ok, just to clarify, my original struggle was to understand what made tmux different from using some terminal app and just split the screen xD

top 50 comments
sorted by: hot top controversial new old
[–] [email protected] 64 points 1 year ago* (last edited 1 year ago) (5 children)

I guess the best analogy is a "virtual desktop" but for the terminal.
It's is a program which runs in a terminal and allows multiple other terminal programs to be run inside it.

Each program inside tmux gets its own "page" or "screen" and you can jump between them (next-screen, previous-screen etc).
So instead of having multiple terminal windows, you only have one and switch the screen/page inside it.

You can detech from the program and leave it running - so next time you log on to the server, you can re-attach to it and all your screens/sessions are still there.

Not super useful on your local machine - but when you have to connect to a remote server (or several) is really shines. Especially if you have to go through a jumphost. You can just connect to your jumphost, start tmux, then create a "screen" for each server you need to connect to - do your stuff and deattach. Next time, just re-attach and all your stuff is there.

Did that help?

[–] [email protected] 12 points 1 year ago (3 children)

Ok, now I guess I am seeing the value of it, specially with the "virtual desktop" analogy and the remote scenario, since I need to do some of it at work and having everything as I left it last time will be nice. Thank you!

[–] topperharlie 15 points 1 year ago (1 children)

plus, if you disconnect in the middle of a command execution it doesn't get killed (very important for system updates for example)

[–] [email protected] 10 points 1 year ago (1 children)

…. or if you get disconnected by, say, dodgy internet connection or such.

[–] [email protected] 1 points 1 year ago

Mosh is really good for that as well. It's like the tmux of ssh

[–] [email protected] 2 points 1 year ago

I use the "being able to detach and re-attach" capability to run my Minecraft server on my in-home server box.

load more comments (1 replies)
[–] adj 6 points 1 year ago* (last edited 1 year ago)

I actually get a lot of use out of it locally. I usually have multiple sessions for different concerns and prefix + s lets me switch between them quickly using vi keybindings. I can even do prefix + w to switch to a specific window in a different session.

I don't use vscode much lately, but when I did it was also useful sometimes to have the same window in my terminal client on one desktop and in vscode's terminal on another when switching back and forth a lot to see a browser or database client or whatever. Just having the freedom to move the session around to different applications is nice.

[–] [email protected] 3 points 1 year ago

You can also split windows to have multiple terminals side by side, or above below each other. Many use it locally as a tileing window manager when their main terminal or window manager does not support tiling.

[–] s38b35M5 2 points 1 year ago

Adding to this comment that tmux allows team members spread through the world to work on the same terminal together on different SSH sessions.

Both admins connect, then one spins up the tmux and the other can attach to it and both collaborate and see all inputs/outputs.

[–] Phoenix3875 15 points 1 year ago* (last edited 1 year ago) (1 children)

It's a "terminal multiplexer", i.e. you can start multiple terminals in a single terminal.

You might ask, why not open a new terminal window or tab? Well, you can only do that in a desktop environment and that's not always available. Even if you can, you might want the terminals to be side by side in a single screen, which might not be easy to do with window tiling.

The real power of tmux, though, is that it manages the session you created. To quote from the manual:

tmux may be detached from a screen and continue running in the background, then later reattached.

So, one use case would be saving your current terminal setup. Instead of exiting the terminal and navigating to the project and setting up the environment again next time, you can simply detach and re-attach.

When connecting to a remote server, this is especially useful:

Each session is persistent and will survive accidental disconnection (such as ssh(1) connection timeout) or intentional detaching

Suppose you want to execute a long running command on a remote server. If you just put it to foreground, when you exit the ssh session, the job is also killed. If you put it to the background, its output can't be easily observed.

With tmux, you can simply run it in the foreground like normal and detach. When you reattach later, the job is running and you get all the output easily, as if you have been in that session all along.

[–] [email protected] 12 points 1 year ago

Decoupling the terminal from the shell also means you can keep your shell open while closing your terminal window. This can be useful on servers, or for long running commands that you want to be able to check on.

[–] belshamharoth 12 points 1 year ago (1 children)

To understand why you might want to use tmux try the following:

  1. Open your terminal
  2. Start editing a file with vim or nano but don't save the file
  3. Close then re-open your terminal

You will have lost your progress, next we can repeat but this time using tmux so you don't lose your session:

  1. Open your terminal
  2. Start a tmux session using tmux
  3. Start editing a file again using vim or nano
  4. Close and re-open the terminal
  5. Type tmux a to re-attach to the existing session

Note that this time none of your progress is lost.

Aside from enabling you to have a persistent session, tmux also allows you to have multiple terminal panes open so you can do more than one thing at a time in the window, to see what I mean try this:

  1. Open your terminal
  2. Start a new tmux session using tmux
  3. Type top to begin listing processes
  4. Press ctrl b then % to make a new split pane
  5. Enter ls or other terminal commands

You will see that you can use more than one panel to do things. This can be useful for example if you want to watch run tests and also run other commands.

[–] [email protected] 3 points 1 year ago

All of this is way more useful when all you have to work with is a tty or an ssh session. No X? No problem.

[–] [email protected] 9 points 1 year ago (1 children)
[–] [email protected] 1 points 1 year ago

So, one use case would be saving your current terminal setup. Instead of exiting the terminal and navigating to the project and setting up the environment again next time, you can simply detach and re-attach.

Thank you, I'll check on it!

[–] [email protected] 9 points 1 year ago

Ok, just to clarify, my original struggle was to understand what made tmux different from using some terminal app and just split the screen xD

Not every terminal emulator has window splitting capabilities. Some, like Alacritty, specifically expect you to run a program like tmux if you want this functionality. Splitting within tmux also makes it vastly easier to multitask on a remote host via SSH: if you run a remote tmux, every split window is already running on the same remote host, no need to log in again and again.

[–] [email protected] 7 points 1 year ago (1 children)

I didn't see this mentioned, but by far the thing I depend on tmux for the most is being able to quickly copy and paste text from the terminal. e.g. grabbing a file name from the output of git diff. How does everyone else do this?

Another cool one is being able to attach to a session on my phone to check on something, and have it automatically resize without disconnecting my desktop.

[–] [email protected] 4 points 1 year ago (1 children)

I copy by pressing ctrl+shift+C. Some terminal emulators copy on select. A terminal multiplexer isn't needed to copy.

[–] piranhaphish 2 points 1 year ago (2 children)

I suspect what they meant was copy and paste from the console and not a terminal.

I don't know how else somebody could do copy and paste at the console. And I don't necessarily know that tmux can do this (I still haven't graduated from 'screen'), but this interpretation makes the most sense.

If it can do this, presumably with just the keyboard, that's a pretty decent feature.

[–] [email protected] 2 points 1 year ago

Yeah, doing it with the keyboard is key. I know some terminals have a way to do it, but it's so ingrained in my muscle memory that I struggle without it, and having something that works everywhere (including try) is nice.

[–] [email protected] 2 points 1 year ago (2 children)

I'm not familiar with the terminology. What's the distinction between a terminal and a console?

Tmux does let you copy from a shell to your system clipboard using the keyboard, which is nice. But many terminal emulators like mobaxterm on windows let you copy as well.

[–] piranhaphish 1 points 1 year ago

The console is the virtual terminal (VT) seen initially at boot before the desktop login starts up, or where you land if there is no desktop, and where the kernel spits its raw output. It could even be configured to be a physical serial port.

I'm using the term in a similar manner to describe the virtual terminals spawned at boot (typically 7 of them) and occupied either by a login prompt (getty) or the desktop session, and switchable with Alt-Left/Right or using the chvt command. These are analogous to the real terminals of old such as VT100 or even typewriters.

This is in contrast to what we normally call a terminal like xterm or Konsole which runs in the GUI where it is resizable, zoomable, etc. The console, and virtual terminals, are pretty limited in the interactivity they have. For instance, there's no mouse interaction or copy-paste functionality, at least not without some exotic setup.

[–] [email protected] 1 points 1 year ago

@karlthemailman @piranhaphish I think of the console as the terminal where the system writes. For example, when it boots.

[–] [email protected] 7 points 1 year ago (3 children)

You’ve gotten a lot of good replies, so I’ll give you an example:

My wife and I set up a Minecraft server on an old work computer of hers. We would SSH in, start the server, and play. However, if the host lost the SSH session, the entire server would crash because the session would close.

With tmux, we could attach, start the server, and unattach. I could start the server and later my wife could attach to close it. I could SSH on my phone via iSH, attach, start the server, unattach, and close the app. We could troubleshoot mods together, since we could both see everything that happened in the session on our screens.

It offered a level of flexibility a traditional SSH session doesn’t give.

[–] [email protected] 3 points 1 year ago (1 children)

Sounds like "screen"? (I never heard about tmux until today, I work a lot with Linux on a daily base, maintaining servers etc. I use screen a lot.)

[–] [email protected] 3 points 1 year ago (1 children)

Tmux is screen in more powerful. Zellij is another great terminal multiplexer. Worth giving all of them a whirl.

load more comments (1 replies)
load more comments (2 replies)
[–] [email protected] 6 points 1 year ago

tmux (and GNU screen, its older predecessor) is a terminal multiplexer, which is a fancy phrase used to describe turning one terminal window into multiple terminal windows. It basically turns a single terminal window into a text-based tiling window manager that lets you run different shells concurrently in a single terminal, easily copy text between them, and have other quality of life improvements over using a single raw terminal.

Imagine you're SSH'd into a remote machine. Unless you SSH again from a different terminal at the same time, you're basically limited to a single terminal, and whatever you're doing is interrupted if your connection drops. tmux runs on the remote machine, which means that if your connection is interrupted, tmux will continue running exactly as you left it, and you'll be able to reattach to it using tmux attach.

Or, imagine your video drivers break and you're forced to troubleshoot in a raw TTY. tmux will let you have a manpage and a shell open at the same time, or three different directories opened side by side. That's a slightly more convoluted use case, but the point is that terminal multiplexers make it far more convenient to use the terminal in basically any situation that's not just running a single short command and leaving.

[–] kurotora 6 points 1 year ago* (last edited 1 year ago) (1 children)

Probably somebody can provide a better answer, but for me tmux is useful due that it has session manager (really useful if your remote connection drops) and the ability to split the screen in multiple screens (usually I split vertical, but you can create easily 4x4 screen).

The only trick is the learning curve of the actions (usually ctrl + b and the key required). For example to split the window vertical, you must do ctrl + b and then %.

But as I said, probably you will get better and more technical answers ^_^U

EDIT: some grammar mistakes.

[–] [email protected] 3 points 1 year ago

The irony is that once you find your way around through the default keys and search a little you soon discover how easy it is to reset them with "sane" settings. Same for window frames, etc. But yes, there's definitely a learning curve.

[–] [email protected] 5 points 1 year ago

Tmux is no different from a terminal app that split the screen in terms of "multi window" functionality. However it's not a graphical software, so you can start it remotely (eg. over ssh), and detach/reattach to it later without loosing what you where doing.

[–] [email protected] 5 points 1 year ago (1 children)

What if we put a terminal in your terminal, so you could terminal while you terminal?

[–] [email protected] 4 points 1 year ago (1 children)

sup dawg, heard you like sessions

[–] [email protected] 5 points 1 year ago

It's an older meme, sir, but it checks out..

[–] PeterPoopshit 4 points 1 year ago

People have already made lots of good replies but here's my summary:

tmux is a terminal multiplexer. It allows multitasking in command line only environments. For example if you have to do a sudo apt upgrade but don't want to leave your ssh client logged in until it finishes, you can run it in a tmux session so it will happen in the background even if you're not logged in.

To start a new session, type "tmux"

To view running sessions, type "tmux list-sessions"

To switch to a running session, type "tmux attach-session -c N" where N is the number of the session.

To exit a tmux terminal and go back to the main terminal, do ctrl+b and then press d.

[–] [email protected] 3 points 1 year ago (2 children)

Well, not knowing what other explanations you've read but don't understand/grasp makes it a bit difficult to narrow down specifics, though to start from the beginning, tmux is a terminal multiplexer, what that means is that it will allow multiple sessions running concurrently under the same virtual terminal. It provides keyboard shortcuts to switch between them, or split them and display them concurrently.

The biggest use case for me however (though I use an older one called screen out of hard to shake habits) is the ability to detach and attach at will, so that any disconnected remote sessions won't kill whatever I happen to be working on. Alternatively, I can have running sessions locally on my current machine and then I can go elsewhere and remote in and resume from where I've left off.

A somewhat frowned upon use case is to use it to run "background" processes on a remote server - like a development web service that you just can't be bothered to properly package/daemonize - just open screen or tmux, start it, and detach the session and it should stay running barring any other problems.

[–] [email protected] 3 points 1 year ago (1 children)

I use screen still too, partly because it's generally installed on everything already, like vim. I hardly ever use anything but a maximised (i.e. full-terminal) screen at once, so it doesn't sound like I'm missing much from tmux.

De/reattaching's extremely useful and another thing I really like in screen is being able to scroll and search the scrollbuffer.

If I was ready for an upgrade, I'd probably go for zellij.

[–] [email protected] 3 points 1 year ago* (last edited 1 year ago)

I use screen as well. It is significantly faster than tmux.

[–] [email protected] 1 points 1 year ago* (last edited 1 year ago)

A somewhat frowned upon use case is to use it to run "background" processes on a remote server

in most cases screen/tmux is an overkill, I prefer using setsid for quick and dirty scripts, it just starts a process in a new session, detached from parent terminal. Or nohup when I need to check the output. Both available on most linux systems by default.

[–] [email protected] 3 points 1 year ago

Have you ever seen someone use a tiling window manager instead of a desktop environment? Where it keeps all the currently running apps on screen in evenly sized tiles so you can see everything at once, nothing is in the "background?"

Tmux is a bit like that, but only for the terminal. It allows you to open multiple terminals in one "screen" or terminal emulator window, and switch between them with keyboard shortcuts. So if you want to look at your source code, test run your source code, and watch htop to see how it performs, you can do that with Tmux. It's a bit less cumbersome than opening three terminal windows.

It also works over SSH, so you can SSH into a server or something, start tmux, then easily run several tools simultaneously.

Tmux sessions are also persistent. Imagine if you were in the middle of working on something on your desktop at the office, then it's time to go home. You can detach your session, SSH into the box from your laptop, reattach that session and keep working right where you left off.

If you work in the terminal a lot, it's a handy tool.

[–] GustavoM 3 points 1 year ago

It's a "terminal inside your terminal" that you can cut into tiny little pieces where programs are run in said little pieces.

[–] [email protected] 2 points 1 year ago

Tmux is essentially an extension of the concept of running ideas in the background. Like 0xtero mentioned, you use a sort of "virtual terminal" that is persistent (unlike the jobs system you might be familiar with on your shell).

Now, some people do just use it to split the screen. The idea is it can have multiple terminals, show them, and manage them - it is thus a "multiplexer", which is where it gets its name (tmux = terminal multiplexer). If you're on a terminal-only system, this isn't that bad of a usage.

Say you're compiling a large program, like the Linux kernel, and you want to step away, maybe even close the terminal and come back later. Tmux is great for this. You can start the compile in tmux, "detach" (stop viewing it) from it, and it'll still run full-speed in the background. When you want to look at it again, or check the status, you just re-attach.

[–] zikk_transport2 2 points 1 year ago

In the company I work, we have to use jumpbox + "password" from proprietary code generator.

Imagine going through this, then you suddenly need 2nd terminal. Inconvenience doing it again in another terminal?

Well, there is a solution:

  1. tmux
  2. CTRL+B then ". And now you have 2 terminals.

Also tmux is great for "quick solution" kind of things - to leave something running in the background. Talking about background - you can have many terminals open, from only 1 SSH session. :)

[–] [email protected] 1 points 1 year ago

it's just a terminal session you can hide.

load more comments
view more: next ›