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

65 points
*

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?

permalink
report
reply
12 points

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!

permalink
report
parent
reply
15 points

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

permalink
report
parent
reply
10 points

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

permalink
report
parent
reply
1 point

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

permalink
report
parent
reply
1 point

Imagine you ssh into a server to do a database import, and from previous experience you know it will take about 3 hours. You start the restore, then get up to make dinner. You come back an hour later and realize you forgot to plug your laptop in.

Is the import command still running? Who knows.

With tmux you just charge your laptop, ssh in again, and reconnect to the virtual term that was running the command to check.

permalink
report
parent
reply
5 points
*

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.

permalink
report
parent
reply
3 points

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.

permalink
report
parent
reply
3 points
*
Removed by mod
permalink
report
parent
reply
1 point

Yeesh, I never knew about that. Probably because I normally only worry about SIGHUP on *BSD.

What kind of arrogance does it take to just decide to change how signals work?

permalink
report
parent
reply
-1 points

Ugh I remembering learning upstart and getting decent with it and then everyone went “nope, systemd”

Let’s just improve what we have and not change the whole goddamn thing again. That’s more annoying.

Also, some of the people on hackernews are so cringy. Like, dude we get it there is a bad default. Make your case and stop being a total jerkoff, because no one is going to listen to that guy and I bet that’s like 20% of the reason the other übernerds are digging their heels in about changing it.

Also fuck systemd 😅

permalink
report
parent
reply
1 point

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.

permalink
report
parent
reply
14 points
*

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.

permalink
report
reply
3 points

The real power of tmux, though, is that it manages the session you created.
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.

systemd: Oh yeah? Hold my beer

permalink
report
parent
reply
1 point

Just…wow.

permalink
report
parent
reply

That was infuriating to read. Thanks.

permalink
report
parent
reply
1 point

I don’t get what you’re referencing… does systemd break this functionality of tmux or something?

permalink
report
parent
reply
12 points

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.

permalink
report
reply
12 points

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.

permalink
report
reply
3 points

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.

permalink
report
parent
reply
9 points

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.

permalink
report
reply

Linux

!linux@lemmy.ml

Create post

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

  • Posts must be relevant to operating systems running the Linux kernel. GNU/Linux or otherwise.
  • No misinformation
  • No NSFW content
  • No hate speech, bigotry, etc

Related Communities

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

Community stats

  • 7.5K

    Monthly active users

  • 6.6K

    Posts

  • 179K

    Comments