Linux Commands

How to Detach and Reattach With Linux Screen

how to detach and reattach with linux screen

The screen is a powerful tool that is available to use in almost every Linux distribution. With the help of this tool, we can have and use multiple sessions of SSH from single SSH. It acts as a terminal multiplexer which is packed with numerous features.

To use this great tool, we have to start and invoke the “screen” command in our terminal. Once we are inside a screen session, we can create more than one virtual terminal, and they are known as “windows,” and we can also navigate between them effortlessly. Screen sessions continue to run even when we disconnect from our servers or close the terminal windows, which makes it suitable for tasks requiring prolonged execution.

Let’s have some of the advantages of using the screen in Debian or any Linux Distribution.

Advantages of screen

Persistent Sessions: The screen allows you to create and detach from terminal sessions, enabling you to reconnect later and resume your work, making it ideal for long-running processes or remote sessions.

Multiplexing: With a screen, you can have multiple virtual terminals within a single physical terminal, facilitating multitasking and efficient management of multiple tasks simultaneously.

Remote Access: The screen enables remote access to running sessions, allowing you to connect to your terminal session from another machine or device, making it convenient for remote administration and troubleshooting.

Session Sharing: The screen allows multiple users to collaborate by sharing the same terminal session simultaneously, facilitating real-time collaboration and code reviews.

Session Recovery: In the event of a network disruption or unexpected logout, the screen preserves the session state, ensuring you can recover your work and continue where you left off without data loss.

Scriptable Sessions: Screen sessions can be automated and scripted, enabling you to create custom workflows or automate repetitive tasks, enhancing productivity and efficiency.

Many Linux distributions have this pre-installed. But if it is not, we can install it very easily. Let’s see how we can install Screen in Debian 12.

Update Debian 12

Before installing Screen in Debian 12, we should update it first.

$ sudo apt update && upgrade

Install Screen

To install the screen, we will be using the apt package manager.

$ sudo apt install screen

Now let’s see if the screen is available in our system or not. Just write a screen in the terminal.

$ screen

Create Screen

Let’s have an example and let’s create a screen for us here.

$ screen -S myscreen

Here it has created a screen session for us. We can close or detach this session very easily by hitting CTRL+A and D at the same time.

Here we have successfully detached our session.

Attach to a Screen

To reattach the screen that we had earlier, we can use a simple command.

$ screen -r

Here as we can see, we have “myscreen” available as well, we can reattach it. But as we have more than one screen session available, we can attach to one particular session with the following command.

$ screen -r myscreen

Here we are back to our “myscreen” session.

Conclusion

This was all about how we can have multiple screen sessions. We have also seen how we can install screens in case it is not available in our Linux distributions. We have also seen the example of how we can attach and detach the screen session.

Similar Posts