- How to Use the Screen Command in Linux
- Basic Commands
- Screen Flags
- Default Session Key Bindings
- Screen Examples
- Modifying Screens Behavior
- Scrollback
- Logging
- Multiuser Screen Sessions
- Further Exploration
- Screen Tricks
- Most Useful Screen Commands
- Conclusion
- Related Articles:
- About the Author: David Singer
- Can’t resume screen, says I am already attached?
- 4 Answers 4
How to Use the Screen Command in Linux
Screen is a terminal program in Linux which allows us to use a virtual (VT100 terminal) as full-screen window manager which multiplexes an open physical terminal between multiple processes, which are typically, interactive shells. It allows us to access multiple terminal sessions within a single terminal or a remote terminal session. It is most useful when addressing multiple Linux shell commands on the command line, as well as separating commands from the shell that started the commands.
Screen also allows a user to initiate a command from one terminal, disconnect from that terminal, and then reconnect from a different location to that same terminal, while using a different terminal without having to restart the command. This simply lets a user better control multiple and separate command windows.
Screen also lets multiple remote computers connect to the same screen session at once. This allows multiple users to connect to the same screen session allowing a second user to follow along with another administrator working on a server.
Basic Commands
Screen is started by using the following screen command:
Screen Flags
Here is a list of some of the more common flags and how we can use them.
Flag | Description | Example |
-ls | Lists the current screen sessions | screen -ls |
-aS | Create and names a screen session which include all capabilities | screen -aS screenname |
-r | Resume a detached screen session | screen -r (name|pid) |
-x | Attach to an attached screen session | screen -x (name|pid) |
-d | Detach an attached screen session | screen -d (name|pid) |
-d -m | Start an new screen session in detached mode and run $command | screen -d -m $command |
-DR | Force disconnect of current user and reconnect with your user | screen -DR |
-wipe | Removes all dead screen sessions. (Sessions that are unreachable) | screen -wipe |
Default Session Key Bindings
While inside the screen session, ctrl+a activates the key bindings. The following table shows some of the default key bindings:
key | binding |
? | Show key bindings. |
A | Set a title for the current window. |
c | Create a new window. |
n | Switch to the next window. |
p | Switch to the previous window. |
0-9 | Switch to window number n. |
w | Show a list of windows. |
« | Present a list of windows for selection. |
F | Resize the window to the current region size. |
[ or esc | Enter copy/scrollback mode. |
k | Destroy current window. |
Screen Examples
To get a full list of available screens, use this command:
we will then see output similar to this:
There is a screen on: 26092.ttyp0.host (Attached) 1 Socket in /tmp/screens/S-root.
This output typically shows us the currently running screens. As you can see by the «(Attached)» output, the 26092 session is currently attached to a terminal. Since this screen is currently attached, we can detach it remotely using this command.
If another user is logged onto this screen session and actively working, this modification will kill their session. So, beware of detaching screen sessions this way. To reattach to that same screen, run this command.
We can detach and reattach to the screen concurrently using this command.
In order to create a new screen session called LW, we can enter this command.
Modifying Screens Behavior
Scrollback
To enable scrollback in xterm, we can add the following information to our ~/.screenrc file on our workstation.
# Enable mouse scrolling and scroll bar history scrolling termcapinfo xterm* ti@:te@
Logging
If we use the -L option with screen, it tells screen to turn on the automatic logging output for the window. This will create a file within the current directory, with the name screenlog.# where # is the ID number for that screen beginning from 0.
Multiuser Screen Sessions
Multiuser is an excellent option to use if we need to share our session with others. It is not turned on by default for security reasons but can be enabled easily. In order to allow multiple users to connect to the same screen session, the multiuser option must be enabled. Here are the steps needed to complete this task.
- Begin by logging in to the server and starting the screen session via the command line.
- Next, type Ctrl-a to enter the command mode.
- Now, type in «:» (colon) to enter the screen’s built-in command line.
- We can now activate the multiuser mode by entering multiuser on in the screen’s command line, which appears in a reverse color at the bottom of the console window.
- To confirm this change, hit Enter or Return.
Multiuser mode can be very useful if we are attempting to demonstrate to someone what we are doing on a server. With this mode on, the other user will be able to follow our keystrokes within that screen session.
Further Exploration
If more in depth information is required, we can always consult the man page for screen using:
Screen Tricks
To initiate a screen session named LW and run the top command in it without attaching to it, nothing will appear to happen but you will see the screen with screen -ls:
To end or kill the LW screen session, use this command:
To start a bash session in screen, we can use this command:
To run the ls command inside the screen session use this command:
screen -S LW -p 0 -X exec ls /home
To end or kill the LW screen session, run this command:
To scroll up within a screen window, use this command:
CTRL+a then ESC, #Press CTRL KEY and 'A' KEY together and then hit the ESC KEY #to scroll using the arrow keys hit ESC again to exit.
Most Useful Screen Commands
Here is a quick overview of the most useful screen commands that we might need to use.
We should enter these key commands in lower case, but they are shown here in uppercase to make the command more readable.
- Ctrl+A, C — This key command creates a new screen
- Ctrl+A, N (where N is equal to the number of the screen) — This key command switch us to a specific screen
- Ctrl+A, A — This key command switches us to the next screen
- Ctrl+A, W — This key command lets us view a list of screens
- Ctrl+A, » (double quote) — This key command allows us to view the screen picker
- Ctrl+A, Ctrl+D — This key command detaches us from the current session
- screen -R — This key command attaches us to a running screen session
- Ctrl+A, ? — This key command shows us the shortcut key options
There are numerous other commands that we can use to control our screen session, many of which can be viewed by typing «Ctrl+A ?» at the prompt. This will bring up a help window that shows us all the available key bindings. For example:
- The ‘C-a p‘ and ‘C-a n‘ key sequence can be used to switch to the next or previous window.
- Using the ‘C-a N‘ key sequence — (where N is a number from 0 to 9, that can will connect to the corresponding screen window.
- The ‘C-a w‘ key sequence displays a list of all screen windows. The unique ID of each window, along with its name, and running process is displayed, for each individual window. The current window will be marked with an asterisk (*).
- The ‘C-a k‘key sequence will kill the current window session. We can also type ‘exit’ to kill the current window. If no other screen sessions are open, screen will simply exits. The ‘C-a \‘ key sequence also accomplishes the same task.
- The ‘C-a d‘ key sequence detaches the from the current screen session.
We can also detach from the screen by closing the terminal that is running our screen utility. Although this may appear to close our terminal session, in reality this simply closes the terminal and unbinds our session from the current terminal. All the commands that were started under our screen will continue to run.
We can also log out from the server and login again, then restart a terminal session and type ‘screen -r‘ to connect again from where we left off. In case, there are more than one running screen sessions on the server, screen will prompt us for a host.tty.pid. For example, say I have two screen sessions. So, when we type the ‘screen -r‘ command, it gives the following message:
$ screen -r There are several suitable screens on: 2999.pts-6.localhost (Detached) 1920.PTS-6.localhost (Detached) To connect to one of the above detached screens run, screen -d -r 2999.pts-6.localhost to resume that screen.
Conclusion
Screen is an invaluable tool when we must work with the terminal daily. It allows us to utilize a limited amount of screen space while maintaining the full functionality of a terminal window. Additionally, screen has the ability to be used as a teaching tool allowing multiple users to connect to an existing screen and, if we have enabled logging, share that file with the user for later reference.
Are you trying to use the screen command and experiencing some difficulty with the control commands, flags or other options with the program?
Our knowledgeable system administrators and experienced linux technicians are standing by to assist you with the help you need. Give us a call at today at 800.580.4985, or open a chat or ticket with us to speak with us right away!
Related Articles:
About the Author: David Singer
I am a g33k, Linux blogger, developer, student, and former Tech Writer for Liquidweb.com. My passion for all things tech drives my hunt for all the coolz. I often need a vacation after I get back from vacation.
Join our mailing list to receive news, tips, strategies, and inspiration you need to grow your business
Can’t resume screen, says I am already attached?
I am working on a remote Debian Jessie server. I have started a screen session, started running a script, then been disconnected by a network timeout. Now I have logged in again and want to resume the session. This is what I see when I list screens:
$ screen -ls There are screens on: 30608.pts-8.myserver (11/03/2015 08:47:58 AM) (Attached) 21168.pts-0.myserver (11/03/2015 05:29:24 AM) (Attached) 7006.pts-4.myserver (10/23/2015 09:05:45 AM) (Detached) 18228.pts-4.myserver (10/21/2015 07:50:49 AM) (Detached) 17849.pts-0.myserver (10/21/2015 07:43:53 AM) (Detached) 5 Sockets in /var/run/screen/S-me.
I seem to be attached to two screens at once. Now I want to resume the session I was running before, to see the results of my script:
$ screen -r 30608.pts-8.myserver There is a screen on: 30608.pts-8.OpenPrescribing (11/03/2015 08:47:58 AM) (Attached) There is no screen to be resumed matching 30608.pts-8.myserver.
$ screen -r 21168.pts-0.myserver There is a screen on: 21168.pts-0.OpenPrescribing (11/03/2015 05:29:24 AM) (Attached) There is no screen to be resumed matching 21168.pts-0.myserver.
4 Answers 4
The session is still attached on another terminal. The server hasn’t detected the network outage on that connection: it only detects the outage when it tries to send a packet and gets an error back or no response after a timeout, but this hasn’t happened yet. You’re in a common situation where the client detected the outage because it tried to send some input and failed, but the server is just sitting there waiting for input. Eventually the server will send a keepalive packet and detect that the connection is dead.
In the meantime, use the -d option to detach the screen session from the terminal where it’s in.
screen -rd is pretty much the standard way to attach to an existing screen session.
To attach to an existing screen session, it’s screen -x , screen -rd is to attach to it after having detached it from every terminal it was attached to. In the case of the OP, I’d agree the latter is probably prefered, but in the general case, not necessarily. I generally use -xR to attach or create if there’s nothing to attach.
According to the manual you can «resume» only detached screens:
‘ -ls [match]’
‘ -list [match]’
Do not start screen, but instead print a list of session identification strings (usually of the form pid.tty.host; see Session Name). Sessions marked ‘detached’ can be resumed with screen -r . Those marked ‘attached’ are running and have a controlling terminal. If the session runs in multiuser mode, it is marked ‘multi’. Sessions marked as ‘unreachable’ either live on a different host or are dead. An unreachable session is considered dead, when its name matches either the name of the local host, or the specified parameter, if any. See the -r flag for a description how to construct matches. Sessions marked as ‘dead’ should be thoroughly checked and removed. Ask your system administrator if you are not sure. Remove sessions with the ‘ -wipe ’ option.
What you want is this option:
‘ -x ’
Attach to a session which is already attached elsewhere (multi-display mode). Screen refuses to attach from within itself. But when cascading multiple screens, loops are not detected; take care.