Baeldung Pro – Linux – NPI EA (cat = Baeldung on Linux)
announcement - icon

Learn through the super-clean Baeldung Pro experience:

>> Membership and Baeldung Pro.

No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.

Partner – Orkes – NPI EA (tag=Kubernetes)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

1. Overview

In Linux, screen is a powerful command-line utility used to manage multiple terminal sessions in one window. Additionally, it’s handy for tasks like server monitoring, development, or running long-term processes. However, by default, screen assigns a numeric identifier to each terminal session, which sometimes becomes difficult to distinguish. For this reason, we assign meaningful names to sessions, allowing us to organize, identify, and reattach to specific sessions when necessary.

In this tutorial, we’ll look at various methods to assign a name to a screen session in Linux.

2. Assigning Name to Screen Sessions

Let’s see some practical examples of how we can effectively assign and manage names for screen sessions.

2.1. Start a Named Screen Session

First, to start a screen session with a particular name, we use the -S option along with the screen command and specify the session name:

$ screen -S <Session_Name>

In this syntax, we replace <Session_Name> with the respective session name.

For instance, to start the Server_task named session, we can execute this command:

$ screen -S Server_task

This command initializes a new screen session with the name Server_task.

2.2. List All Active Screen Sessions

In case we want to list all active sessions to identify or manage them, we can use the -ls option with the screen command:

$ screen -ls
There are screens on:
        3341.dev_task   (25/12/2024 15:44:21)   (Attached)
        3330.Server_task        (25/12/2024 15:43:38)   (Detached)
2 Sockets in /run/screen/S-henry.

In this output, we see details of two active sessions: 3341.dev_task is attached, while 3330.Server_task is detached and running in the background. Here, 3314 and 3330 are the process IDs (PIDs) associated with the sessions.

2.3. Reattach to a Named Screen Session

After listing, if we want to reattach to a specific session, we can use the -r option with the screen command:

$ screen -r <session_name_or_ID>

Above, we specify <session_name_or_ID> with the particular session name or ID we want to reattach to.

For example, to reattach to the dev_task screen session, we can run this command:

$ screen -r dev_task

After working, we can detach from the session by pressing Ctrl+a and then d.

2.4. Rename an Existing Screen Session

Sometimes, we start a screen session without giving a meaningful name or wish to rename it later on. In such cases, we can rename the particular session inside the screen session.

First, if detached, we reattach to the particular session. For example, let’s reattach to one of the existing session dev_task:

$ screen -r dev_task

Now, to rename the dev_task session, we press Ctrl+a and then :. Afterward, we use this command to rename the session:

sessionname <new_session_name>

In this command, we specify the <new_session_name> with the new session name. Also, please note that the sessionname is the predefined keyword that should be used as shown.

To illustrate, let’s rename the dev_task to new_dev_task. To achieve this, we can execute the command:

sessionname new_dev_task

Now, to verify, let’s list all active sessions:

$ screen -ls
There are screens on:
        3341.new_dev_task       (25/12/2024 15:44:21)   (Attached)
        3330.Server_task        (25/12/2024 15:43:38)   (Detached)
2 Sockets in /run/screen/S-henry.

Consequently, we can see that the particular session has been renamed to new_dev_task.

2.5. Name Individual Windows Within a Screen Session

Lastly, as a screen session can host multiple windows, we can assign a specific name to each window, which is helpful when multitasking.

Firstly, to open a new window within a screen, we can press Ctrl+a and c, which creates a new shell in the same session.

Now, to give names, we press Ctrl+a and A (uppercase) which opens a prompt at the bottom of the screen asking for a new name:

$ 
Set window's title to:     

Here, we can give names like server_logs, printing_logs, and then press enter to set it.

After naming, we switch between them easily by pressing Ctrl+a and (double quotes) that list all windows in the session along with their titles:

 Num Name                                                                  Flags
   0 server_logs                                                               $
   1 printing_logs                                                             $
   2 bash                                                                      $

Above, we see that three windows, server_logs, printing_logs, and bash, are opened. To navigate, simply use the arrow keys and press enter.

3. Conclusion

In this article, we learned how to assign names to screen sessions. In addition, we covered starting screen sessions with names, listing, renaming, and reattaching to specific screen sessions. Furthermore, we showed naming individual windows within a screen sessions.