tmux-Solve the problem of ssh session ending without operation for a long time

Tmux is a terminal multiplexer (terminal multiplexer), which is very useful and is a commonly used development tool.

If the computer crashes and the session is interrupted, all bash-based processes on the server will be forcibly closed. Tmux is indeed a good way to solve this problem, and it is very easy to use. If you often use the terminal to solve problems like me, then Tmux is worth learning for you.

1. What is Tmux?

1.1 Session and process

A typical way to use the command line is to open a terminal window (terminal window, hereinafter referred to as “window”), and enter commands in it. This temporary interaction between the user and the computer is called a “session” (session).

An important feature of a session is that the window is associated with the process started in it. Open the window, the session starts; close the window, the session ends, and the process inside the session will also be terminated, regardless of whether it has finished running or not.

A typical example is that SSH logs in to a remote computer and opens a remote window to execute commands. At this time, the network is suddenly disconnected, and when you log in again, the command executed last time cannot be retrieved. Because the last SSH session has been terminated, the process inside has also disappeared.

In order to solve this problem, the session and the window can be “unbound”: when the window is closed, the session is not terminated, but continues to run, and when it is needed later, let the session “bind” other windows.

1.2 The role of Tmux

Tmux is the “unbind” tool for sessions and windows, completely separating them.

(1) It allows simultaneous access to multiple sessions in a single window. This is useful for running multiple command-line programs at the same time.

(2) It allows new windows to “access” existing sessions.

(3) It allows multiple connection windows per session, so multiple people can share sessions in real time.

(4) It also supports arbitrary vertical and horizontal splitting of windows.

A similar terminal multiplexer is GNU Screen. Tmux is similar in functionality, but easier to use and more powerful.

2. Basic usage

2.1 Installation

Tmux generally needs to be installed by itself.

# Ubuntu or Debian
$ sudo apt-get install tmux

# CentOS or Fedora
$ sudo yum install tmux

# Mac
$ brew install tmux

2.2 Startup and exit

After the installation is complete, type the tmux command to enter the Tmux window.

$ tmux

The above command will start a Tmux window with a status bar at the bottom. The left side of the status bar is window information (number and name), and the right side is system information.

To exit a Tmux window, press Ctrl + d or explicitly enter the exit command.

$ exit

2.3 Prefix keys

The Tmux window has a large number of shortcut keys. All shortcuts are invoked by prefix keys. The default prefix key is Ctrl + b, that is, press Ctrl + b before the shortcut key will take effect.

For example, the shortcut key for the help command is Ctrl + b ?. Its usage is, in the Tmux window, first press Ctrl + b, then press ?, and the help information will be displayed.

Then, press the ESC key or the q key to exit the help.

3. Session management

3.1 Create a new session

The number of the first Tmux window started is 0, the number of the second window is 1, and so on. The sessions corresponding to these windows are session 0 and session 1.

It is not very intuitive to use numbers to distinguish sessions. A better way is to name the sessions.

$ tmux new -s <session-name>

The above command creates a new session with the specified name.

3.2 Detach session

In the Tmux window, press Ctrl + b d or enter the tmux detach command to detach the current session from the window.

$ tmux detach

After the above command is executed, the current Tmux window will be exited, but the session and the processes inside it will still run in the background.

The tmux ls command can view all current Tmux sessions.

$ tmux ls
# or
$ tmux list-session

3.3 Access session

The tmux attach command is used to reattach an existing session.

# use session id
$ tmux attach -t 0

# use the session name
$ tmux attach -t <session-name>

3.4 Kill session

The tmux kill-session command is used to kill a session.

# use session id
$ tmux kill-session -t 0

# use the session name
$ tmux kill-session -t <session-name>

3.5 Switch session

The tmux switch command is used to switch sessions.

# use session id
$ tmux switch -t 0

# use the session name
$ tmux switch -t <session-name>

3.6 Rename session

The tmux rename-session command is used to rename a session.

$ tmux rename-session -t 0 <new-name>

The above command renames session 0.

3.7 Session shortcut keys

Below are some session-related shortcuts.

  • Ctrl + b d: Detach the current session.
  • Ctrl + b s: List all sessions.
  • Ctrl + b $: Rename the current session.

4. The simplest operation process

To sum up, the following is the simplest operation process of Tmux.

  1. Create a new session tmux new -s my_session.
  2. Run the desired program in a Tmux window.
  3. Press the shortcut key Ctrl + b d to detach the session.
  4. Next time you use it, reattach to the session tmux attach-session -t my_session.

5. Pane operation

Tmux can divide the window into multiple panes (panes), and each pane runs a different command. The following commands are all executed in the Tmux window.

5.1 Split pane

The tmux split-window command is used to split panes.

# Divide the upper and lower panes
$ tmux split-window

# Divide left and right panes
$ tmux split-window -h

5.2 Move the cursor

The tmux select-pane command is used to move the cursor position.

# The cursor switches to the upper pane
$ tmux select-pane -U

# The cursor switches to the lower pane
$ tmux select-pane -D

# Cursor switches to the left pane
$ tmux select-pane -L

# Cursor switches to the right pane
$ tmux select-pane -R

5.3 Swapping pane positions

The tmux swap-pane command is used to swap pane positions.

# Move the current pane up
$ tmux swap-pane -U

# Move the current pane down
$ tmux swap-pane -D

5.4 Pane shortcuts

Below are shortcut keys for some pane operations.

  • Ctrl + b %: Divide the left and right panes.
  • Ctrl + b ": Divide the upper and lower panes.
  • Ctrl + b : The cursor switches to other panes. is the arrow key pointing to the pane to be switched to, for example, to switch to the lower pane, press the arrow key .
  • Ctrl + b ;: The cursor switches to the previous pane.
  • Ctrl + b o: The cursor switches to the next pane.
  • Ctrl + b {: The current pane is swapped with the previous pane.
  • Ctrl + b }: Swap the current pane with the next pane.
  • Ctrl + b Ctrl + o: Move all panes forward one position, the first pane becomes the last pane.
  • Ctrl + b Alt + o: Move all panes back one position, the last pane becomes the first pane.
  • Ctrl + b x: Close the current pane.
  • Ctrl + b !: Split the current pane into an independent window.
  • Ctrl + b z: The current pane is displayed in full screen, and it will return to its original size when used again.
  • Ctrl + b Ctrl + : Resize the pane in the direction of the arrow.
  • Ctrl + b q: Display the pane number.

6. Window management

In addition to dividing a window into multiple panes, Tmux also allows creating multiple windows.

6.1 New window

The tmux new-window command is used to create a new window.

$ tmux new-window

# Create a new window with the specified name
$ tmux new-window -n <window-name>

6.2 Switching windows

The tmux select-window command is used to switch windows.

# Switch to the window with the specified number
$ tmux select-window -t <window-number>

# Switch to the window with the specified name
$ tmux select-window -t <window-name>

6.3 Rename window

The tmux rename-window command is used to name (or rename) the current window.

$ tmux rename-window <new-name>

6.4 Window shortcut keys

The following are shortcut keys for some window operations.

  • Ctrl + b c: Create a new window, and the status bar will display the information of multiple windows.
  • Ctrl + b p: switch to the previous window (according to the order on the status bar).
  • Ctrl + b n: switch to the next window.
  • Ctrl + b : Switch to the window with the specified number, where is the window number on the status bar.
  • Ctrl + b w: Select a window from the list.
  • Ctrl + b ,: Rename the window.

7. Other commands

Below are some other commands.

# List all shortcuts and their corresponding Tmux commands
$ tmux list-keys

# List all Tmux commands and their arguments
$ tmux list-commands

# List information about all current Tmux sessions
$ tmux info

# Reload the current Tmux configuration
$ tmux source-file ~/.tmux.conf

1. What is Tmux?

1.1 Session and process

A typical way to use the command line is to open a terminal window (terminal window, hereinafter referred to as “window”), and enter commands in it. This temporary interaction between the user and the computer is called a “session” (session).

An important feature of a session is that the window is associated with the process started in it. Open the window, the session starts; close the window, the session ends, and the process inside the session will also be terminated, regardless of whether it has finished running or not.

A typical example is that SSH logs in to a remote computer and opens a remote window to execute commands. At this time, the network is suddenly disconnected, and when you log in again, the command executed last time cannot be retrieved. Because the last SSH session has been terminated, the process inside has also disappeared.

In order to solve this problem, the session and the window can be “unbound”: when the window is closed, the session is not terminated, but continues to run, and when it is needed later, let the session “bind” other windows.

1.2 The role of Tmux

Tmux is the “unbind” tool for sessions and windows, completely separating them.

(1) It allows simultaneous access to multiple sessions in a single window. This is useful for running multiple command-line programs at the same time.

(2) It allows new windows to “access” existing sessions.

(3) It allows multiple connection windows per session, so multiple people can share sessions in real time.

(4) It also supports arbitrary vertical and horizontal splitting of windows.

A similar terminal multiplexer is GNU Screen. Tmux is similar in functionality, but easier to use and more powerful.

2. Basic usage

2.1 Installation

Tmux generally needs to be installed by itself.

# Ubuntu or Debian
$ sudo apt-get install tmux

# CentOS or Fedora
$ sudo yum install tmux

# Mac
$ brew install tmux

2.2 Startup and exit

After the installation is complete, type the tmux command to enter the Tmux window.

$ tmux

The above command will start a Tmux window with a status bar at the bottom. The left side of the status bar is window information (number and name), and the right side is system information.

To exit a Tmux window, press Ctrl + d or explicitly enter the exit command.

$ exit

2.3 Prefix keys

The Tmux window has a large number of shortcut keys. All shortcuts are invoked by prefix keys. The default prefix key is Ctrl + b, that is, press Ctrl + b before the shortcut key will take effect.

For example, the shortcut key for the help command is Ctrl + b ?. Its usage is, in the Tmux window, first press Ctrl + b, then press ?, and the help information will be displayed.

Then, press the ESC key or the q key to exit the help.

3. Session management

3.1 Create a new session

The number of the first Tmux window started is 0, the number of the second window is 1, and so on. The sessions corresponding to these windows are session 0 and session 1.

It is not very intuitive to use numbers to distinguish sessions. A better way is to name the sessions.

$ tmux new -s <session-name>

The above command creates a new session with the specified name.

3.2 Detach session

In the Tmux window, press Ctrl + b d or enter the tmux detach command to detach the current session from the window.

$ tmux detach

After the above command is executed, the current Tmux window will be exited, but the session and the processes inside it will still run in the background.

The tmux ls command can view all current Tmux sessions.

$ tmux ls
# or
$ tmux list-session

3.3 Access session

The tmux attach command is used to reattach an existing session.

# use session id
$ tmux attach -t 0

# use the session name
$ tmux attach -t <session-name>

3.4 Kill session

The tmux kill-session command is used to kill a session.

# use session id
$ tmux kill-session -t 0

# use the session name
$ tmux kill-session -t <session-name>

3.5 Switch session

The tmux switch command is used to switch sessions.

# use session id
$ tmux switch -t 0

# use the session name
$ tmux switch -t <session-name>

3.6 Rename session

The tmux rename-session command is used to rename a session.

$ tmux rename-session -t 0 <new-name>

The above command renames session 0.

3.7 Session shortcut keys

Below are some session-related shortcuts.

  • Ctrl + b d: Detach the current session.
  • Ctrl + b s: List all sessions.
  • Ctrl + b $: Rename the current session.

4. The simplest operation process

To sum up, the following is the simplest operation process of Tmux.

  1. Create a new session tmux new -s my_session.
  2. Run the desired program in a Tmux window.
  3. Press the shortcut key Ctrl + b d to detach the session.
  4. Next time you use it, reattach to the session tmux attach-session -t my_session.

5. Pane operation

Tmux can divide the window into multiple panes (panes), and each pane runs a different command. The following commands are all executed in the Tmux window.

5.1 Split pane

The tmux split-window command is used to split panes.

# Divide the upper and lower panes
$ tmux split-window

# Divide left and right panes
$ tmux split-window -h

5.2 Move the cursor

The tmux select-pane command is used to move the cursor position.

# The cursor switches to the upper pane
$ tmux select-pane -U

# The cursor switches to the lower pane
$ tmux select-pane -D

# Cursor switches to the left pane
$ tmux select-pane -L

# Cursor switches to the right pane
$ tmux select-pane -R

5.3 Swapping pane positions

The tmux swap-pane command is used to swap pane positions.

# Move the current pane up
$ tmux swap-pane -U

# Move the current pane down
$ tmux swap-pane -D

5.4 Pane shortcuts

Below are shortcut keys for some pane operations.

  • Ctrl + b %: Divide the left and right panes.
  • Ctrl + b ": Divide the upper and lower panes.
  • Ctrl + b : The cursor switches to other panes. is the arrow key pointing to the pane to be switched to, for example, to switch to the lower pane, press the arrow key .
  • Ctrl + b ;: The cursor switches to the previous pane.
  • Ctrl + b o: The cursor switches to the next pane.
  • Ctrl + b {: The current pane is swapped with the previous pane.
  • Ctrl + b }: Swap the current pane with the next pane.
  • Ctrl + b Ctrl + o: Move all panes forward one position, the first pane becomes the last pane.
  • Ctrl + b Alt + o: Move all panes back one position, the last pane becomes the first pane.
  • Ctrl + b x: Close the current pane.
  • Ctrl + b !: Split the current pane into an independent window.
  • Ctrl + b z: The current pane is displayed in full screen, and it will return to its original size when used again.
  • Ctrl + b Ctrl + : Resize the pane in the direction of the arrow.
  • Ctrl + b q: Display the pane number.

6. Window management

In addition to dividing a window into multiple panes, Tmux also allows creating multiple windows.

6.1 New window

The tmux new-window command is used to create a new window.

$ tmux new-window

# Create a new window with the specified name
$ tmux new-window -n <window-name>

6.2 Switching windows

The tmux select-window command is used to switch windows.

# Switch to the window with the specified number
$ tmux select-window -t <window-number>

# Switch to the window with the specified name
$ tmux select-window -t <window-name>

6.3 Rename window

The tmux rename-window command is used to name (or rename) the current window.

$ tmux rename-window <new-name>

6.4 Window shortcut keys

The following are shortcut keys for some window operations.

  • Ctrl + b c: Create a new window, and the status bar will display the information of multiple windows.
  • Ctrl + b p: switch to the previous window (according to the order on the status bar).
  • Ctrl + b n: switch to the next window.
  • Ctrl + b : Switch to the window with the specified number, where is the window number on the status bar.
  • Ctrl + b w: Select a window from the list.
  • Ctrl + b ,: Rename the window.

7. Other commands

Below are some other commands.

# List all shortcuts and their corresponding Tmux commands
$ tmux list-keys

# List all Tmux commands and their arguments
$ tmux list-commands

# List information about all current Tmux sessions
$ tmux info

# Reload the current Tmux configuration
$ tmux source-file ~/.tmux.conf