如何使用Linux屏幕命令?

Linux是一种命令行操作系统,人们非常喜欢通过它强大的命令行来管理Linux。Linux提供了许多从命令行进行管理的工具。Linux提供了screen命令和工具,用于在命令行中创建多个会话和窗口,就像多个选项卡一样。此外,即使终端关闭,这些会话和窗口也会运行,然后可以在没有任何中断的情况下进行连接。screen工具是由GNU社区创建和开发的,也称为“gnuscreen”。

null

Linux屏幕通常与SSH连接一起使用,即使SSH连接关闭,屏幕会话也将继续,并且可以从另一个SSH会话访问它,而不会丢失当前屏幕内容。

安装屏幕命令

screen命令可以很容易地为大多数Linux发行版安装。默认情况下,未安装。对于基于deb或apt的发行版,如Ubuntu、Debian、Mint、Kali,可以使用以下命令安装Linux屏幕。

$ sudo apt update$ sudo apt install screen

对于基于rpm或yum的发行版,如Fedora、CentOS、RHEL等,可以使用以下命令。

$ sudo yum update$ sudo yum install screen

屏幕版本

屏幕是一个命令行工具,它会定期更新新功能和错误修复。屏幕命令的当前版本可以与 –版本 选项如下。

screen --version
Screen version 4.08.00 (GNU) 05-Feb-20

启动Linux屏幕

只需运行 screen 命令如下。

$ screen

我们将第一次看到下面的屏幕,它提供了有关屏幕工具的信息。我们可以按跳过这个屏幕 Space 钥匙。

图片[1]-如何使用Linux屏幕命令?-yiteyi-C++库

我们将看到下面的屏幕,这是一个常规的命令行或bash屏幕,其中包含用户名、主机名和当前工作路径。

图片[2]-如何使用Linux屏幕命令?-yiteyi-C++库
Linux屏幕

启动命名会话

在启动屏幕会话期间,我们可以使用 -S 选项并提供会话名称。在下面的示例中,我们将屏幕会话名称设置为 mysession .

$ screen -S mysession

命名会话对于以后用名称调用屏幕非常有用。此名称还用于通过其名称将会话与其他会话区分开来。

相关文章: 使用SCP命令从本地复制到远程

列表屏幕会话

屏幕可以执行多次,也可以同时使用多个屏幕。可以使用screen命令和-ls选项列出这些屏幕。

screen -ls

下面我们可以看到有两个屏幕会话,它们当前正在运行和连接。屏幕的ID列在名称的开头,编号为15538和15518。

There are screens on:     15538.pts-3.ubuntu  (25-03-2021 11:21:10)   (Attached)     15518.pts-0.ubuntu  (25-03-2021 11:21:06)   (Attached) 2 Sockets in /run/screen/S-ismail.

创建新窗口

屏幕的力量来自于在一个屏幕会话中创建和使用多个窗口。以下快捷方式将在同一屏幕会话中创建新窗口。

Ctrl+a c

使用Linux屏幕窗口进行工作和管理

屏幕窗口、会话通过键盘快捷键进行管理。您可以在下表中找到流行的快捷方式。

快捷键 操作
Ctrl+a c组合键
Ctrl+a“ 列出所有窗口
Ctrl+a 0组合键 切换到0号窗口
Ctrl+a 1键 换到1号窗口
按住Ctrl+a 重命名当前窗口
Ctrl+a和S 将当前窗口水平拆分为两个区域
Ctrl+a键| 将当前窗口垂直拆分为两个区域
Ctrl+a选项卡 将焦点切换到下一个区域
Ctrl+a Ctrl+a 在当前区域和上一区域之间切换
Ctrl+a Q组合键 关闭所有其他区域
Ctrl+a X组合键 关闭当前区域

分离Linux屏幕会话

其中一个强大的功能是分离和重新连接屏幕。下面的命令将分离当前的Linux屏幕会话。但这不会结束Linux屏幕会话。这个屏幕会话可以像下一个示例一样重新连接。

Ctrl+a d

重新连接到Linux屏幕会话

我们还可以使用 -r 选项与屏幕命令如下。

$ screen -r

如果有多个取消附加的屏幕会话,首先应该列出这些会话。列出这些会话还将提供会话ID。

$ screen -ls

我们可以通过在 -r 选项如下。

$ screen -r 123456

更改Linux屏幕配置

作为一个复杂且可定制的工具,linux屏幕有一个名为 .screenrc 位于当前用户主目录下 /etc 目录如下。

~/.screenrc/etc/screenrc

这个 ~/.screenrc 默认情况下不创建文件。作为常规文本文件,您可以使用nano或vim之类的文本编辑器来创建它。这个 /etc/screenrc 具有以下默认配置。

# $Id: screenrc,v 1.15 2003/10/08 11:39:03 zal Exp $## /etc/screenrc##   This is the system wide screenrc.##   You can use this file to change the default behavior of screen system wide#   or copy it to ~/.screenrc and use it as a starting point for your own#   settings.##   Commands in this file are used to set options, bind screen functions to#   keys, redefine terminal capabilities, and to automatically establish one or#   more windows at the beginning of your screen session.##   This is not a comprehensive list of options, look at the screen manual for#   details on everything that you can put in this file.## ------------------------------------------------------------------------------# SCREEN SETTINGS# ------------------------------------------------------------------------------#startup_message off#nethack on#defflow on # will force screen to process ^S/^Qdeflogin on#autodetach off# turn visual bell onvbell onvbell_msg "   Wuff  ----  Wuff!!  "# define a bigger scrollback, default is 100 linesdefscrollback 1024# ------------------------------------------------------------------------------# SCREEN KEYBINDINGS# ------------------------------------------------------------------------------# Remove some stupid / dangerous key bindingsbind ^k#bind Lbind ^# Make them betterbind  quitbind K killbind I login onbind O login offbind } history# An example of a "screen scraper" which will launch urlview on the current# screen window##bind ^B eval "hardcopy_append off" "hardcopy -h $HOME/.screen-urlview" "screen urlview $HOME/.screen-urlview"# ------------------------------------------------------------------------------# TERMINAL SETTINGS# ------------------------------------------------------------------------------# The vt100 description does not mention "dl". *sigh*termcapinfo vt100 dl=5E[M# turn sending of screen messages to hardstatus offhardstatus off# Set the hardstatus prop on gui terms to set the titlebar/icon titletermcapinfo xterm*|rxvt*|kterm*|Eterm* hs:ts=E]0;:fs= 07:ds=E]0; 07# use this for the hard status stringhardstatus string "%h%? users: %u%?"# An alternative hardstatus to display a bar at the bottom listing the# windownames and highlighting the current windowname in blue. (This is only# enabled if there is no hardstatus setting for your terminal)##hardstatus lastline "%-Lw%{= BW}%50>%n%f* %t%{-}%+Lw%<"# set these terminals up to be 'optimal' instead of vt100termcapinfo xterm*|linux*|rxvt*|Eterm* OP# Change the xterm initialization string from is2=E[!pE[?3;4lE[4lE># (This fixes the "Aborted because of window size change" konsole symptoms found#  in bug #134198)termcapinfo xterm 'is=E[rE[mE[2JE[HE[?7hE[?1;4;6l'# To get screen to add lines to xterm's scrollback buffer, uncomment the# following termcapinfo line which tells xterm to use the normal screen buffer# (which has scrollback), not the alternate screen buffer.##termcapinfo xterm|xterms|xs|rxvt [email protected]:[email protected]# Enable non-blocking mode to better cope with flaky ssh connections.defnonblock 5# ------------------------------------------------------------------------------# STARTUP SCREENS# ------------------------------------------------------------------------------# Example of automatically running some programs in windows on screen startup.##   The following will open top in the first window, an ssh session to monkey#   in the next window, and then open mutt and tail in windows 8 and 9#   respectively.## screen top# screen -t monkey ssh monkey# screen -t mail 8 mutt# screen -t daemon 9 tail -f /var/log/daemon.log
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享