Home Tmux Enhancing SSH Login With A Tmux Session Selection Menu In Linux

Enhancing SSH Login With A Tmux Session Selection Menu In Linux

Improve Remote SSH Login with Tmux Session Selector Script in Linux.

By sk
3K views

In this article, we'll demonstrate how to setup a tmux session selection menu that activates upon SSH login to a remote Linux system. This menu will list existing tmux sessions and offer the option to create a new session.

Introduction

Tmux, a terminal multiplexer, enables users to manage multiple terminal sessions within a single window. This comes in handy especially when working on remote systems via SSH.

A common practice is to automatically attach to a tmux session upon SSH login. However, what if you wish to have more control over this, like being able to choose among existing sessions or creating a new one? This is doable with the help of a Bash script.

In the following steps, I will show you how to improve remote SSH login with Tmux Session Selector script in Linux.

Step 1: Create the Tmux Menu Script

We will first create a script called tmux_menu.sh with the following contents. This script will fetch existing tmux sessions, display them to the user, and allow the user to either attach to one of them or create a new session.

#!/bin/bash

# Get a list of existing tmux sessions:
TMUX_SESSIONS=$(tmux ls | awk -F: '{print $1}')

# If there are no existing sessions:
if [[ -z $TMUX_SESSIONS ]]; then
    echo "No existing tmux sessions. Creating a new session called 'default'..."
    tmux new -s default
else
    # Present a menu to the user:
    echo "Existing tmux sessions:"
    echo "$TMUX_SESSIONS"
    echo "Enter the name of the session you want to attach to, or 'new' to create a new session: "
    read user_input

    # Attach to the chosen session, or create a new one:
    if [[ $user_input == "new" ]]; then
        echo "Enter name for new session: "
        read new_session_name
        tmux new -s $new_session_name
    else
        tmux attach -t $user_input
    fi
fi

Save this script to a file named tmux_menu.sh in your home directory.

Script Explanation:

Let's dissect the provided script piece by piece:

1. Script Shebang:

#!/bin/bash

The shebang (#!/bin/bash) is utilized to specify the interpreter for executing the script. In this case, it's bash.

2. Fetch Existing tmux Sessions:

# Get a list of existing tmux sessions:
TMUX_SESSIONS=$(tmux ls | awk -F: '{print $1}')

In this snippet, tmux ls lists all existing tmux sessions. The output is piped (|) to awk, which then processes it to extract only the session names (before the colon :). These names are stored in the variable TMUX_SESSIONS.

3. Check for Existing tmux Sessions:

# If there are no existing sessions:
if [[ -z $TMUX_SESSIONS ]]; then
    echo "No existing tmux sessions. Creating a new session called 'default'..."
    tmux new -s default
else
   ...
fi

Here, an if-else block checks if TMUX_SESSIONS is empty (-z checks for a zero length string). If it's empty, it indicates there are no existing sessions, hence a new session named 'default' is created with tmux new -s default. If it's not empty, the script proceeds to the else block.

4. Display Existing Sessions and Prompt User:

    # Present a menu to the user:
    echo "Existing tmux sessions:"
    echo "$TMUX_SESSIONS"
    echo "Enter the name of the session you want to attach to, or 'new' to create a new session: "
    read user_input

In the else block, the script first displays the existing tmux sessions. It then prompts the user to either enter the name of an existing session to attach to or type 'new' to create a new session. The read command is used to capture the user's input.

5. Process User Input:

    # Attach to the chosen session, or create a new one:
    if [[ $user_input == "new" ]]; then
        echo "Enter name for new session: "
        read new_session_name
        tmux new -s $new_session_name
    else
        tmux attach -t $user_input
    fi

Based on the user's input, another if-else block is used to process the next steps. If the user entered 'new', they are prompted to enter a name for the new session, which is then created with tmux new -s $new_session_name. If the user entered the name of an existing session, the script attaches to that session using tmux attach -t $user_input.

In summary, this script provides a simple user interface upon login to either attach to an existing tmux session or create a new one, enhancing the user's experience when managing multiple tmux sessions.

Step 2: Make the Script Executable

Grant execution permissions to the script with the following command:

$ chmod +x ~/tmux_menu.sh

Step 3: Update Your Bash Profile

Now, update your ~/.bash_profile file on the remote system to call this script whenever you log in via SSH, but only if you're not already inside a tmux session, and the shell is interactive.

Open your ~/.bash_profile file in your favorite editor:

$ nano ~/.bash_profile

Add the following lines in it:

# If not inside a tmux session, and if the shell is interactive, then run the tmux menu script
if [[ -z "$TMUX" ]] && [[ $- == *i* ]]; then
    ~/tmux_menu.sh
fi

Save the file and close it by pressing CTRL+O followed by CTRL+X.

Step 4: Test the Tmux Session Selection Menu Setup

1. From your local machine, SSH into the remote system using this command: ssh user@remote_system.

Example:

$ ssh ostechnix@192.168.1.20

Upon successful login you'll be automatically attached to the 'default' Tmux session.

2. While logged into the remote system, create multiple tmux sessions by running the following commands:

$ tmux new -s tmux1 -d
$ tmux new -s tmux2 -d

The above commands will create two detached Tmux sessions namely tmux1 and tmux2.

Let us verify them by listing the available tmux sessions:

$ tmux ls

Sample Output:

default: 1 windows (created Mon Oct  2 13:31:25 2023)
tmux1: 1 windows (created Mon Oct  2 13:32:28 2023)
tmux2: 1 windows (created Mon Oct  2 13:32:32 2023)
Create and List Tmux Sessions
Create and List Tmux Sessions

As you can see, there are 3 Tmux sessions (default, tmux1, and tmux2) exists in my remote Debian 12 server.

3. Now detach from the tmux session(s) (if you attached to one) using Ctrl+b followed by d.

Log out from the current SSH session.

4. SSH into the remote system again, and you should now see multiple sessions listed in the tmux session selection menu.

$ ssh ostechnix@192.168.1.20
Linux debian12 6.1.0-10-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.38-1 (2023-07-14) x86_64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Mon Oct  2 13:44:33 2023 from 192.168.1.101
Existing tmux sessions:
default
tmux1
tmux2
Enter the name of the session you want to attach to, or 'new' to create a new session: 

5. As you see in the output, the Tmux session selection menu lists three existing tmux sessions. If you want to attach to any of the listed sessions, simply type the name of the tmux session and hit ENTER and you will automatically attached to the respective session.

If you don't want to connect to existing session but create a new session, type 'new' followed by the name for the new session.

Tmux Session Selection Menu
Tmux Session Selection Menu

After typing the name of the new tmux session, hit the ENTER key, and you will be attached to the new tmux session.

List Tmux Sessions
List Tmux Sessions

Please note that the tmux session selection menu will only appear if two or more tmux sessions are active. If there are no active sessions, a new 'default' session will be automatically started.

Conclusion

With this setup, every time you SSH into the remote system, you'll be greeted with a tmux session selection menu. This allows you to better manage your tmux sessions, providing a more organized and efficient remote working environment.

Now, the next time you SSH into your remote system, you'll have the ability to either attach to an existing tmux session or create a new one, helping you keep your work organized and easily accessible.

You May Also Like

3 comments

Paolo October 5, 2023 - 3:22 pm

Thank you very much, very detailed guide!

Reply
Kevin October 6, 2023 - 8:36 pm

First, this is a really cool idea and I like your script. One option that might be nice would be to use the output of ‘whoami’ instead of ‘default’ for the new tmux session name. That might make it easier to keep track of which session you want to attach to if there are multiple users on the system.

Reply
sk October 7, 2023 - 2:23 pm

Thanks for the suggestion. You can update the script however you like. I’d like to keep it really simple.

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More