How to Keep Remote Servers Running With Tmux for Days Even if You Close Your Terminal

Tmux is a powerful terminal multiplexer that allows you to manage multiple terminal sessions within a single window. This capability is incredibly useful for keeping remote servers running for days, even if you close your terminal. In this cheat sheet, we’ll cover essential commands and keyboard shortcuts to help you get started with Tmux.

Why Tmux is Useful

  • Run and manage multiple terminal sessions within a single window.
  • Easily switch between them using keyboard shortcuts.
  • Detach from a session and reattach later to pick up right where you left off.
  • Keep processes running even after you close your terminal, ensuring uninterrupted server tasks.

Installation

Ubuntu

1
2
sudo apt-get update
sudo apt-get install tmux

Creating and Managing Sessions

Create a New Session

1
tmux new -s session_name

To attach to an existing session:

1
tmux a -t session_name

To kill a session:

1
tmux kill-session -t session_name

To list all sessions:

1
tmux ls

To exit a session:

1
exit

Keyboard Shortcuts

Note: To use these shortcuts, press Ctrl+b first, release it, and then press the corresponding key.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
Ctrl-b %      - Split the current pane vertically.
Ctrl-b "      - Split the current pane horizontally.
Ctrl-b arrow  - Move between panes.
Ctrl-b c      - Create a new window.
Ctrl-b n      - Move to the next window.
Ctrl-b p      - Move to the previous window.
Ctrl-b d      - Detach from the current session.
ctrl-b 0-9    - Move to the window number 0-9.
Ctrl-b l      - Move to the previously selected window.
Ctrl-b w      - List all windows.
Ctrl-b &      - Kill the current window.
Ctrl-b ,      - Rename the current window.
ctrl-b space  - Toggle between pane layouts.
Ctrl-b !      - Convert the current pane into a window.
Ctrl-b x      - Close the current pane.
Ctrl-b o      - Switch to the next pane.
Ctrl-b z      - Toggle pane zoom.
Ctrl-b q      - Show pane numbers (used to switch between panes).
Ctrl-b ?      - List all keybindings.

Customizing Tmux: Mouse Scrolling, Status Bar, and More

You can customize Tmux by editing the configuration file located at ~/.tmux.conf. Here are a few examples of customizations:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# Enable mouse scrolling
set -g mouse on

# Enable two prefix keys
set-option -g prefix C-a
unbind-key C-b
bind-key C-a send-prefix

# Set the status bar to display in the bottom
set-option -g status-position bottom

# Set the status bar colors
set-option -g status-style fg=white,bg=black

# Customize the window list format
set-option -g window-status-format " #[fg=yellow]#I:#W#[fg=default]"

# Set the window list colors
set-option -g window-status-current-style fg=black,bg=yellow

# Customize the pane border colors
set-option -g pane-active-border-style fg=green
set-option -g pane-border-style fg=default

# Set the default shell
set-option -g default-shell "/bin/bash"

These are just a few examples of customizations you can make to your Tmux configuration. Feel free to explore the Tmux manual and experiment with different settings to suit your preferences.

Remember to save the changes to your ~/.tmux.conf file and restart your Tmux session for the new settings to take effect.

With these essential commands, keyboard shortcuts, and customization options, you’ll be able to enhance your productivity and efficiently manage multiple terminal sessions using Tmux. This approach ensures that your remote servers and processes can continue running seamlessly, even if you disconnect from your terminal.

Enjoy using Tmux!

0%