- 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
- How to scroll up and down in sliced «screen» terminal
- 5 Answers 5
- Can mouse wheel scrolling work in a Screen session?
- 5 Answers 5
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
How to scroll up and down in sliced «screen» terminal
I just installed screen and like the idea to divide linux terminal into multiple pieces. But I can’t figure out one thing: How to scroll up and down, when I’m in screen . When I’m using regular linux terminal I can achieve this with Shift + Pg Up or Shift + Pg Dn `. but unfortunatelly it doesn’t work in divided terminal. That’s what I mean when saying screen and divided terminal: And that’s a regular termina (just in case):
5 Answers 5
1. Ctrl + A , then Esc
2. After that, you should be able to move your cursor around using the arrow keys:
↑ , ↓ , PgUp , PgDn and sometimes using the mouse wheel
3. Return control: Q or Esc
@MihaiDanila: How would you «normally» use the navigation keys? The navigation keys are handled by the current foreground application. If it’s an editor, they’ll probably work. If it’s a shell, they’ll probably go back through your command history without scrolling the display.
If my pageup/pagedown keys work in a normal shell, I’d like for them to work in a shell from screen . I think that’s what I’m asking here.
in this mode selection of any text is available by typing Space or Enter key once and finish your text selection with another Space or Enter. the selection will enter into the screen based clipboard. you could paste clipboard data later using Ctrl+a then ]
Can mouse wheel scrolling work in a Screen session?
Is there any way to use the mouse wheel to scroll through the output of a screen session? I can use the keypad scroll through previous output in screen after pressing ctrl+a [ . Is it possible to do this with the mouse wheel? (I’m using putty , but I don’t think it’s a putty issue, I believe it’s a screen issue.)
You need to tell PuTTY to send the mouse wheel events into the terminal instead of keeping them for itself. (I don’t know if it’s possible, but check the manual, it’s pretty good.)
Even on a regular terminal session in screen , I haven’t been able to get scrolling working properly outside of screen ‘s «copy mode.» I think it’s a limitation of the program and not so much a Windows/Putty issue, but I could be wrong.
@Caleb: mouse wheel on tmux works like a charm (although I’m not trying it from a putty, I don’t have windows:))
5 Answers 5
Mouse scrolling and elevators will work if you enable them in your .screenrc.
Q: My xterm scrollbar does not work with screen.
A: The problem is that xterm will not allow scrolling if the alternate text buffer is selected. The standard definitions of the termcap initialize capabilities ti and te switch to and from the alternate text buffer. (The scrollbar also does not work when you start e.g. ‘vi’). You can tell screen not to use these initialisations by adding the line termcapinfo xterm ti@:te@ to your ~/.screenrc file.
In tmux, it’d be something like (.tmux.conf):
set -g terminal-overrides 'xterm*:smcup@:rmcup@'