Ubuntu 22.04 Tmux: Difference between revisions
m (Noob moved page Tmux to Ubuntu 22.04 Tmux) |
No edit summary |
||
Line 1: | Line 1: | ||
= tmux — terminal multiplexer = | = tmux — terminal multiplexer = | ||
== Basics == | |||
==Basics== | |||
Description Quote from man page | Description Quote from man page | ||
Line 14: | Line 13: | ||
Tmux can do a number of things. | Tmux can do a number of things. | ||
In this module we are just going to concentrate on solving the problem of something going wrong with | In this module we are just going to concentrate on solving the problem of something going wrong with our SSH connection. | ||
If we lose | If we lose our SSH terminal connection, processes we were running will be sent a SIGHUP signal. | ||
[[SIGHUP|SIGHUP]] | [[SIGHUP|SIGHUP]] | ||
Let’s go straight to an example of using '''tmux''' to solve this problem. | |||
On | On our server, let’s run/start tmux: | ||
<code>tmux</code> | <code>tmux</code> | ||
That’s it. | |||
Now | Now let’s run a program and see what happens when we disconnect: | ||
<code>$EDITOR test-txt-file</code> | <code>$EDITOR test-txt-file</code> | ||
Line 34: | Line 33: | ||
<pre> | <pre> | ||
Hello world | Hello world | ||
I am writing or doing something important. | |||
I hope I don’t disconnect and lose this | |||
</pre> | </pre> | ||
Now just close the | Now just close the terminal—do not save and/or exit the text editor, just close the terminal. | ||
Open another terminal and reconnect to your server. | Open another terminal and reconnect to your server. | ||
Use the command | Use the command '''tmux ls''' to list your tmux sessions: | ||
<code>tmux ls</code> | <code>tmux ls</code> | ||
Line 52: | Line 51: | ||
</pre> | </pre> | ||
I am going to reconnect to my tmux session 0 | I am going to reconnect to my tmux session 0 with the command: | ||
<code>tmux attach -t 0</code> | <code>tmux attach -t 0</code> | ||
And | And that’s it. | ||
Tmux also solves the problem of your | Tmux also solves the problem of your SSH session closing due to inactivity: | ||
<blockquote> | <blockquote> | ||
Line 64: | Line 63: | ||
</blockquote> | </blockquote> | ||
Note: | Note: You can run the same tmux session in more than one terminal at the same time. Open 3 terminals and log into the server. In each terminal: | ||
Open 3 | <br \> | ||
<code>tmux attach -t 0</code> | |||
<code>tmux attach -t 0</code><br \> | <br \> | ||
All will be updated in real time. If you exit tmux by typing <code>exit</code> in one terminal, the tmux session will end and exit on all 3 terminals. If you don’t want that, just close the terminal without typing <code>exit</code>. | |||
If you exit tmux by typing <code>exit</code> in one terminal the tmux session will end and exit on all 3 terminals. | |||
If you | |||
You do not need tmux installed on your local computer, just the computer(server) you are connecting to. | You do not need tmux installed on your local computer, just the computer (server) you are connecting to. | ||
tmux is installed by default on | tmux is installed by default on Ubuntu Server 18.04, but if you want to install it on your desktop or if it’s been removed from your server and you want to reinstall: | ||
but if you want to install it on your desktop | |||
or if | |||
<code>apt install tmux</code> | <code>apt install tmux</code> | ||
=== | == Scrolling in tmux == | ||
Press <code>Ctrl+b</code> followed by <code>[</code>. Now you can scroll up and down with arrow keys. Press <code>q</code> to quit scroll mode. | |||
== Managing tmux Sessions and Windows == | |||
To close a tmux window and later come back to it, you can use the following steps: | |||
=== Detaching from a tmux Session (Without Terminating It) === | |||
Detach from the current tmux session by pressing: | |||
<code>Ctrl-b d</code> | |||
This will leave the session running in the background, allowing you to close the terminal or work on other tasks. | |||
=== Reattaching to a tmux Session === | |||
To reattach to a detached session, use: | |||
<code>tmux attach-session</code> | |||
This command will reconnect you to the most recently detached session. | |||
If you have multiple sessions, list them with: | |||
<code>tmux list-sessions</code> | |||
Then, reattach to a specific session by its name or number: | |||
<code>tmux attach-session -t <session_name></code> | |||
If another client is attached to the session, you can forcefully detach them and reattach using: | |||
<code>tmux attach-session -t <session_name> -d</code> | |||
=== Closing a tmux Window === | |||
To close a specific window in tmux, press: | |||
<code>Ctrl-b &</code> | |||
Confirm with <code>y</code> to kill the window. | |||
Alternatively, you can type <code>exit</code> in the shell of the window to close it. | |||
If you prefer using commands, run: | |||
<code>tmux kill-window -t <window_number></code> | |||
By detaching instead of killing the session, you ensure that your work is preserved and can be resumed later. | |||
Latest revision as of 14:59, 10 April 2025
tmux — terminal multiplexer
Basics
Description Quote from man page
tmux is a terminal multiplexer: it enables a number of terminals to be created, accessed, and controlled from a single screen. tmux may be detached from a screen and continue running in the background, then later reattached.
Tmux can do a number of things.
In this module we are just going to concentrate on solving the problem of something going wrong with our SSH connection.
If we lose our SSH terminal connection, processes we were running will be sent a SIGHUP signal.
Let’s go straight to an example of using tmux to solve this problem.
On our server, let’s run/start tmux:
tmux
That’s it.
Now let’s run a program and see what happens when we disconnect:
$EDITOR test-txt-file
Hello world I am writing or doing something important. I hope I don’t disconnect and lose this
Now just close the terminal—do not save and/or exit the text editor, just close the terminal.
Open another terminal and reconnect to your server.
Use the command tmux ls to list your tmux sessions:
tmux ls
My output:
0: 1 windows (created Mon Jun 1 06:41:43 2020) [140x14]
I am going to reconnect to my tmux session 0 with the command:
tmux attach -t 0
And that’s it.
Tmux also solves the problem of your SSH session closing due to inactivity:
packet_write_wait: Connection to 192.248.157.27 port 22: Broken pipe
Note: You can run the same tmux session in more than one terminal at the same time. Open 3 terminals and log into the server. In each terminal:
tmux attach -t 0
All will be updated in real time. If you exit tmux by typing exit
in one terminal, the tmux session will end and exit on all 3 terminals. If you don’t want that, just close the terminal without typing exit
.
You do not need tmux installed on your local computer, just the computer (server) you are connecting to.
tmux is installed by default on Ubuntu Server 18.04, but if you want to install it on your desktop or if it’s been removed from your server and you want to reinstall:
apt install tmux
Scrolling in tmux
Press Ctrl+b
followed by [
. Now you can scroll up and down with arrow keys. Press q
to quit scroll mode.
Managing tmux Sessions and Windows
To close a tmux window and later come back to it, you can use the following steps:
Detaching from a tmux Session (Without Terminating It)
Detach from the current tmux session by pressing:
Ctrl-b d
This will leave the session running in the background, allowing you to close the terminal or work on other tasks.
Reattaching to a tmux Session
To reattach to a detached session, use:
tmux attach-session
This command will reconnect you to the most recently detached session.
If you have multiple sessions, list them with:
tmux list-sessions
Then, reattach to a specific session by its name or number:
tmux attach-session -t <session_name>
If another client is attached to the session, you can forcefully detach them and reattach using:
tmux attach-session -t <session_name> -d
Closing a tmux Window
To close a specific window in tmux, press:
Ctrl-b &
Confirm with y
to kill the window.
Alternatively, you can type exit
in the shell of the window to close it.
If you prefer using commands, run:
tmux kill-window -t <window_number>
By detaching instead of killing the session, you ensure that your work is preserved and can be resumed later.