How to List and Attach Tmux Sessions

Several terminals that can work separately by connecting to form a terminal and share the information between them provide the Tmux utility a big edge over the default Linux terminal. The Tmux program allows the users to manage and rub several sessions/terminals simultaneously, either several panes in a single window or as different windows.

While working with the Tmux, the listing, attaching, and detaching Tmux session is a basic task. Multiple methods can be used to list and attach Tmux sessions in Linux while creating the new session. This writeup will explain this with the below supporting topics:

How to List the Tmux Sessions?

This section will discuss all the methods to list Tmux sessions in Linux.

Method 1: List Tmux Sessions Using the ls Command

The “ls” is the popular command to list the directory’s content. Using the “ls” command with the tmux lists the Tmux sessions:

$ tmux ls

Three sessions are currently active, and the output shows the following details:

  • 0: It shows the session Id for every Tmux session.
  • 1 windows: It shows the session name.
  • Created <date time>: It shows the creation date & time of the session.
  • attached: It shows the session’s status, if it’s attached or not.

Method 2: List Tmux Sessions Using the list-sessions Command

The “list-sessions” command lists the currently active sessions in Tmux as shown below::

$ tmux list-sessions

Method 3: List Tmux Sessions Using the lsc Command

The lsc command provides another option to list the open Tmux sessions in Linux as done below:

$ tmux lsc

How to Attach Tmux Sessions?

A session is an opened Tmux window where multiple users can work connected to share the resources. Sometimes, we need to attach the Tmux session to other sessions to form a single terminal. Several commands can attach the Tmux sessions.

Method 1: Using attach-session Command

The tmux “attach-session” can be used to attach/connect to the session, but first, check the session if it’s attached or not using the ls command:

$ tmux ls

The output shows the session with ID “session1” is not attached.

To attach the session1 ID to other sessions, the below command can be used:

$ tmux attach-session -t session1

After entering the attach session command, the session1 will be attached, which can be verified by executing the ls command:

$ tmux ls

The session1 is attached to other sessions.

Method 2: Using attach Command

The attach command can be used with the “t” option to attach the session using its session ID. For instance, to attach a session having ID “session1” can be attached by running the below Tmux command:

$ tmux attach -t session1

After running the command, the session1 will attach with other sessions as can be verified by running the following command:

$ tmux ls

Method 3: Using an Option With Tmux Command

Similarly, the “a” (attach) option can be used with the “t” (type) option to watch the session using the session ID. For example, the below command will attach the detached session “session1” with the rest of the sessions:

$ tmux a -t session1

The execution of the command will attach the “session1” with other active sessions as verified below:

$ tmux ls

The session1 shows the attached status.

That’s how the tmux sessions are attached and listed.

Conclusion

To list Tmux sessions, “tmux ls”, “tmux list-sessions” and “tmux lsc” commands are executed in the Linux terminal. However, to list the Tmux sessions, we can use the “tmux attach-session -t <sessioID>”, “tmux attach -t sessionID”, or “tmux a -t sessionID” commands.