What happens when you’re connected to a remote system, using a long-running program, and then the connection drops? The odds are, at a minimum, you’re going to have to restart the program, and in a worst-case scenario, you’ll have data corruption. To help get around this, some programs run in a window shell on the system. A very basic example of this is the screen
program:
[pgervase@pgervase ~]$ ssh root@rhel7dev.usersys.redhat.com
X11 forwarding request failed on channel 0
Last login: Wed Jan 27 12:10:06 2021 from xxxxxxxx.redhat.com
[root@rhel7dev ~]# screen
This opens my new shell on the rhel7dev
system. I’ll run the ping
command below from inside of that session:
[root@rhel7dev ~]# ping www.google.com
PING www.google.com (74.125.24.147) 56(84) bytes of data.
64 bytes from 74.125.24.147 (74.125.24.147): icmp_seq=1 ttl=100 time=242 ms
64 bytes from 74.125.24.147 (74.125.24.147): icmp_seq=2 ttl=100 time=242 ms
64 bytes from 74.125.24.147 (74.125.24.147): icmp_seq=3 ttl=100 time=242 ms
I’ll now demonstrate how to detach from the session to simulate a network outage or to simply leave something running overnight. To do this, I hit Ctrl, hold that key down, then hit A, then hit D. That brings me back to the default SSH prompt and I am then able to run screen -ls
to see my detached session:
[root@rhel7dev ~]# screen -x
[detached from 25665.pts-0.rhel7dev]
[root@rhel7dev ~]# screen -ls
There is a screen on:
25665.pts-0.rhel7dev (Detached)
1 Socket in /var/run/screen/S-root.
[root@rhel7dev ~]#
[ You might also enjoy: Working with pipes on the Linux command line ]
To resume my screen session, I type screen -x
because there was only one session as an option. That brought me back to the screen session where the ping
command is still running:
[root@rhel7dev ~]# ping www.google.com
PING www.google.com (74.125.24.147) 56(84) bytes of data.
64 bytes from 74.125.24.147 (74.125.24.147): icmp_seq=1 ttl=100 time=242 ms
64 bytes from 74.125.24.147 (74.125.24.147): icmp_seq=2 ttl=100 time=242 ms
<snipped>
64 bytes from 74.125.24.147 (74.125.24.147): icmp_seq=19 ttl=100 time=242 ms
64 bytes from 74.125.24.147 (74.125.24.147): icmp_seq=20 ttl=100 time=242 ms
^C
--- www.google.com ping statistics ---
20 packets transmitted, 20 received, 0% packet loss, time 20278ms
rtt min/avg/max/mdev = 242.105/242.197/242.727/0.576 ms
[root@rhel7dev ~]#
I can have multiple screen sessions at once:
[root@rhel7dev ~]# screen -ls
There is a screen on:
25665.pts-0.rhel7dev (Detached)
1 Socket in /var/run/screen/S-root.
[root@rhel7dev ~]# screen
[detached from 25693.pts-0.rhel7dev]
[root@rhel7dev ~]# screen -ls
There are screens on:
25693.pts-0.rhel7dev (Detached)
25665.pts-0.rhel7dev (Detached)
2 Sockets in /var/run/screen/S-root.
[root@rhel7dev ~]# screen
[detached from 25706.pts-0.rhel7dev]
[root@rhel7dev ~]# screen -ls
There are screens on:
25706.pts-0.rhel7dev (Detached)
25693.pts-0.rhel7dev (Detached)
25665.pts-0.rhel7dev (Detached)
3 Sockets in /var/run/screen/S-root.
[root@rhel7dev ~]#
In each of those three screen sessions, I can have commands running or simply leave a session sitting at the prompt.
A default screen -x
will not work to resume a session now because of the multiple screens running:
[root@rhel7dev ~]# screen -x
There are several suitable screens on:
25706.pts-0.rhel7dev (Detached)
25693.pts-0.rhel7dev (Detached)
25665.pts-0.rhel7dev (Detached)
Type "screen [-d] -r [pid.]tty.host" to resume one of them.
[root@rhel7dev ~]#
To attach to one of my sessions, I need to run screen -x
and add enough of the screen name to be unique:
[root@rhel7dev ~]# screen -x 257
[detached from 25706.pts-0.rhel7dev]
[root@rhel7dev ~]#
Rather than trying to limit yourself to just one session or remembering what is running on which screen, you can set a name for the session by using the -S
argument:
[root@rhel7dev ~]# screen -S "db upgrade"
[detached from 25778.db upgrade]
[root@rhel7dev ~]# screen -ls
There are screens on:
25778.db upgrade (Detached)
25706.pts-0.rhel7dev (Detached)
25693.pts-0.rhel7dev (Detached)
25665.pts-0.rhel7dev (Detached)
4 Sockets in /var/run/screen/S-root.
[root@rhel7dev ~]# screen -x "db upgrade"
[detached from 25778.db upgrade]
[root@rhel7dev ~]#
To exit a screen session, you can type exit
or hit Ctrl+A and then D.
Now that you know how to start, stop, and label screen
sessions let's get a little more in-depth. To split your screen session in half vertically hit Ctrl+A and then the | key (Shift+Backslash). At this point, you’ll have your screen session with the prompt on the left:
To switch to your screen on the right, hit Ctrl+A and then the Tab key. Your cursor is now in the right session, but there’s no prompt. To get a prompt hit Ctrl+A and then C. I can do this multiple times to get multiple vertical splits to the screen:
You can now toggle back and forth between the two screen panes by using Ctrl+A+Tab.
What happens when you cat
out a file that’s larger than your console can display and so some content scrolls past? To scroll back in the buffer, hit Ctrl+A and then Esc. You’ll now be able to use the cursor keys to move around the screen and go back in the buffer.
There are other options for screen
, so to see them, hit Ctrl, then A, then the question mark:
[ Free online course: Red Hat Enterprise Linux technical overview. ]
Further reading can be found in the man page for screen
. This article is a quick introduction to using the screen
command so that a disconnected remote session does not end up killing a process accidentally. Another program that is similar to screen
is tmux
and you can read about tmux
in this article.
About the author
I am a Senior Principal Security Architect at Verizon. Before that, I worked at Red Hat in various roles such as consulting and in the Solutions Architect where I specialized in Smart Management, Ansible, and OpenShift. In my free time, I enjoy spending time with my family, exercising, and woodworking.
Browse by channel
Automation
The latest on IT automation for tech, teams, and environments
Artificial intelligence
Updates on the platforms that free customers to run AI workloads anywhere
Open hybrid cloud
Explore how we build a more flexible future with hybrid cloud
Security
The latest on how we reduce risks across environments and technologies
Edge computing
Updates on the platforms that simplify operations at the edge
Infrastructure
The latest on the world’s leading enterprise Linux platform
Applications
Inside our solutions to the toughest application challenges
Original shows
Entertaining stories from the makers and leaders in enterprise tech
Products
- Red Hat Enterprise Linux
- Red Hat OpenShift
- Red Hat Ansible Automation Platform
- Cloud services
- See all products
Tools
- Training and certification
- My account
- Customer support
- Developer resources
- Find a partner
- Red Hat Ecosystem Catalog
- Red Hat value calculator
- Documentation
Try, buy, & sell
Communicate
About Red Hat
We’re the world’s leading provider of enterprise open source solutions—including Linux, cloud, container, and Kubernetes. We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.
Select a language
Red Hat legal and privacy links
- About Red Hat
- Jobs
- Events
- Locations
- Contact Red Hat
- Red Hat Blog
- Diversity, equity, and inclusion
- Cool Stuff Store
- Red Hat Summit