Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of the screen command in Linux

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the use of the screen command in Linux, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Linux common command Screen is a free software developed by GNU for command line terminal switching. Through the software, users can connect multiple local or remote command line sessions at the same time and switch freely between them.

Screen is used for command line terminal switching

Add that GNU Screen can be thought of as the command-line interface version of the window manager. It provides a unified interface for managing multiple sessions and corresponding functions.

Session recovery

As long as Screen itself is not terminated, sessions running within it can be resumed. This is particularly useful for remotely logged-in users-even if the network connection is lost, the user does not lose control of the command-line session that is already open. Simply log in to the host again and execute screen-r to resume the session. Similarly, when you are temporarily away, you can also execute the detach command detach to suspend Screen (switch to the background) while ensuring that the programs inside are running properly. This is very similar to VNC in a graphical interface.

Multiple windows

In the Screen environment, all sessions run independently and have their own numbers, inputs, outputs, and window caches. Users can switch between different windows through keyboard shortcuts, and are free to redirect the input and output of each window. Screen implements basic text operations, such as copy and paste, and provides a scrollbar-like function to view the history of window conditions. Windows can also be partitioned and named, and you can monitor the activity of background windows. Session sharing Screen allows one or more users to log in to a session multiple times from different terminals and share all the features of the session (such as seeing exactly the same output). It also provides a mechanism for window access, which can protect the window with password.

GNU's Screen official site: http://www.gnu.org/software/screen/

Syntax # screen-AmRvx-[ls-wipe] [- d] [- h] [- r] [- s] [- S] option-A resizes all windows to the size of the current terminal. -d takes the specified screen job offline. -h specifies the number of buffer rows for the window. -m forces the creation of a new screen job even if it is already in the job. -r resumes offline screen jobs. -R first attempts to resume offline jobs. If an offline job cannot be found, a new screen job is created. -s specifies the shell to be executed when a new window is created. -S specifies the name of the screen job. -v displays version information. -x resumes previously offline screen jobs. -ls or-- list displays all current screen jobs. -wipe checks all current screen jobs and deletes screen jobs that are no longer available. Common screen parameter screen-S yourname-> create a new session screen-ls called yourname-> list all current session screen-r yourname-> go back to yourname this session screen-d yourname-> remote detach some session screen-d-r yourname-> end the current session and go back to yourname this session under each screen session, all commands start with ctrl+a (Cmura).

Cmura?-> display all key binding information Cmura c-> create a new window running shell and switch to this window Cmura n-> Next, switch to the next window Cmera p-> Previous Switch to the previous window Cmura 0.9-> switch to the 0.9th window Ctrl+a [Space]-> from Windows 0 to Windows 9 Cmura Cmura-> switch between the two recently used window Cmura x-> lock the current window, unlock Cmura d-> detach with the user password, temporarily leave the current session, and throw the current screen session (which may contain multiple windows) to the background for execution And will return to the state before the screen, when in the screen session, the process (whether foreground / background) running in each window continues to execute, even the logout does not affect. Cmura z-> put the current session to be executed in the background, and you can go back with shell's fg command. Cmura w-> shows the list of all windows Cmura t-> time, shows the current time, and the load Cmura k-> kill window of the system, forcibly closes the current window Cmura-> enter copy mode, and can roll back, search and copy under copy mode just like using [vi] Cmurb Backward,PageUp Cmurf Forward,PageDown H (uppercase) High, move the cursor to the upper left corner L Low Move the cursor to the lower left corner 0 to the end of the line $w forward one word, move forward b backward one word in words, move back in words Space first press as the starting point of the marked area, press the second press as the end Esc to end copy mode Cmura]-> paste, paste the content you just selected in copy mode to install screen using screen

Popular Linux distributions, such as Red Hat Enterprise Linux, usually come with the screen utility, which, if not available, can be downloaded from GNU screen's official website.

[root@TS-DEV ~] # yum install screen [root@TS-DEV ~] # rpm-qa | grep screen screen-4.0.3-4.el5 [root@TS-DEV ~] # create a new window

After the installation is complete, you can start it by simply typing the command screen. However, the screen session started in this way does not have a name. In practice, it is recommended to give a name to each screen session to facilitate identification:

After [root@TS-DEV] # screen-S davidscreen starts, the first window, window No. 0, is created and opens a system default shell, which is usually bash. So as soon as you type the command screen, you will immediately return to the command prompt as if nothing had happened, but you have already entered the world of Screen. Of course, you can also add your favorite parameters after the screen command to directly open the program you specify, such as:

[root@TS-DEV ~] # screen vi david.txtscreen creates a single-window session that executes vi david.txt, and exiting vi exits the window / session.

View window and window name

After opening multiple windows, you can use the shortcut Cmura w to list all the current windows. If you use a text terminal, the list will be listed in the lower left corner of the screen, and if you use a terminal emulator in the X environment, the list will be listed in the title bar. The list of windows usually looks like this:

In this example, I have opened three windows, where the * sign indicates that it is currently in window 2, and the bash-sign indicates that it was in window 1 the last time it was switched.

By default, Screen will name the window a combination of the number and the name of the program running in the window, which is the default name in the above example. After practicing the method of viewing windows above, you may want each window to have different names to make it easier to distinguish. You can use the shortcut Cmura A to rename the current window. After pressing the shortcut key, Screen will allow you to enter a new name for the current window and enter to confirm it.

Session separation and recovery

You can temporarily disconnect (detach) the screen session without interrupting the programs in the screen window, and reconnect (attach) the session at a later time to regain control of the programs running in each window. For example, we open a screen window to edit the / tmp/david.txt file:

After [root@TS-DEV ~] # screen vi / tmp/david.txt, we want to quit temporarily and do something else, such as going for a walk, then typing Clicha dmemScreen in the screen window will give us a detached prompt:

Temporarily interrupt the session

Come back half an hour later and find the screen session:

[root@TS-DEV ~] # screen-ls reconnect the session:

[root@TS-DEV] # screen-r 12865 everything is there.

Of course, if you don't detach a Screen session on another machine, there is no way to resume the session. At this point, you can force the session to be detached from the terminal on which it is located and transferred to the new terminal using the following command:

Clear dead session

If one of the sessions dies for some reason (such as killing the session artificially), screen-list will show that the session is in a dead state. Use the screen-wipe command to clear the session:

Close or kill a Screen session

Normally, when you exit the last program in a window (usually bash), the window closes. Another way to close a window is to use the ctrl+a key, then press the k key, and finally press the y key when prompted to kill the session. This shortcut will kill the current window as well as the running process in the window.

If the last window in a Screen session is closed, the entire Screen session exits and the screen process is terminated.

In addition to exiting / killing all the windows in the current Screen session in turn, you can also use the shortcut Cmura:, and then enter the quit command to exit the Screen session. It is important to note that this exit kills all windows and exits all programs running in it. In fact, Cmura: this shortcut key allows users to enter a lot of commands directly, including split-screen input split and so on, which is also a way to achieve Screen function, but I think shortcut keys are more convenient.

In addition, here is another command to quickly kill a Screen session:

[root@TS-DEV ~] # screen-ls # list the existing sessions [root@TS-DEV ~] # screen-XS "session id or name" quit example:

[root@TS-DEV] # screen-ls There are screens on: 11235.test (01 Sockets in 25 screen 03:35:31 PM) (Detached) 1 Sockets in / run/screen/S-root. [root@TS-DEV ~] # screen-XS 11235 quit # or [root@TS-DEV ~] # screen-XS test quitscreen Advanced Application session sharing

There is also a more interesting kind of session recovery, which can achieve session sharing. Suppose you and your friend log in to a machine with the same user in different locations, and then you create a screen session, and your friend can command on his terminal:

The command [root@TS-DEV] # screen-x will Attach your friend's terminal to your Screen session, and your terminal will not be Detach. In this way, you can share the same session with your friends. If you are currently in the same window, it is equivalent to sitting in front of the same monitor. Your operation will be demonstrated to your friend at the same time, and your friend's operation will be shown to you at the same time. Of course, if you switch to different windows of this session, you can do different things separately.

Session locking and unlocking

Screen allows you to use the shortcut Cmura s to lock the session. Once locked, any input screen will no longer respond. Note, however, that although there is no response on the screen, your input will be received by the process in Screen. The shortcut key Cmura Q can unlock a session.

You can also use Cmura x to lock the session, except that after locking, the session is protected by the password of the user to which Screen belongs, and you need to enter a password to continue to access the session.

Send a command to a screen session

Outside of the Screen session, you can manipulate a Screen session with the screen command, which also makes it easier to use Screen as a scripting program. With regard to the fact that the application of Screen in scripts is beyond the scope of getting started, here is just an example to experience the operation of Screen outside the session:

The command [root@TS-DEV ~] # screen-S sandy-X screen ping www.baidu.com creates a new window in a screen session called sandy and runs the ping command in it.

Screen segmentation

Now that the monitor is so large, it's obviously cool to split a screen into different areas to display different Screen windows. You can use the shortcut Cmura S to split the display horizontally. After Screen version 4.00.03, vertical split is also supported. The shortcut key is Cmura |. After splitting the screen, you can use Cmura to switch between blocks, and each block can create a window and run processes in it.

You can use the Cmura X shortcut to close the screen block where the current focus is located, or you can use Cmura Q to close all blocks except the current block. The window in the closed block does not close, and it can be found through the window switch.

CCPUP mode and operation

Another powerful feature of screen is the ability to copy and paste between different windows. Use the shortcut keys Cmura or Cmura [to enter copy/paste mode, where you can move the cursor as you would in vi, and use the spacebar to set the tag. In fact, there are many vi-like operations in this mode, such as using / searching, using y to quickly mark a line, using w to quickly mark a word, and so on. This section of the document explains in more detail the advanced operations in CPX P mode.

In general, you can move the cursor to the specified location, press the space to set a beginning tag, then move the cursor to the end position, press the space to set the second tag, and save the part between the two tags in copy/paste buffer and exit copy/paste mode. In normal mode, you can use the shortcut Cmura] to paste the content stored in buffer into the current window.

More screen featur

Like most UNIX programs, GNU Screen provides rich and powerful customization capabilities. You can specify more in Screen's default two-level configuration file / etc/screenrc and $HOME/.screenrc, such as setting screen options, customizing binding keys, setting screen session self-startup window, enabling multi-user mode, customizing user access control, and so on. You can also specify your own screen configuration file if you like.

Take the multi-user function as an example. Screen runs in single-user mode by default. You need to specify multiuser on in the configuration file to turn on multi-user mode through acl* (acladd,acldel,aclchg … ) command, you can flexibly configure other users to access your screen session.

Thank you for reading this article carefully. I hope the article "what is the use of screen commands in Linux" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report