{"id":6815,"date":"2020-01-21T10:23:59","date_gmt":"2020-01-21T16:23:59","guid":{"rendered":"https:\/\/www.poweradmin.com\/blog\/?p=6815"},"modified":"2019-12-04T16:44:01","modified_gmt":"2019-12-04T22:44:01","slug":"how-to-get-the-most-out-of-the-linux-screen-command","status":"publish","type":"post","link":"https:\/\/www.poweradmin.com\/blog\/how-to-get-the-most-out-of-the-linux-screen-command\/","title":{"rendered":"How To Get The Most Out Of The Linux Screen Command"},"content":{"rendered":"<p><span style=\"font-family: verdana, geneva, sans-serif;\">If you\u2019re logging onto a service or running remote command line operations over a network link via the Secure Shell (SSH) protocol, the last thing you need is for your session to be cut off by a faulty connection. This scenario is all too common \u2013 but for Linux users, the Screen utility can prevent it from occurring.<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">The tool also enables you to create multiple screen windows from a single session, navigate between windows, detach and resume screen sessions, and customize your screen terminal. Here\u2019s how.<\/span><\/p>\n<h2><span style=\"font-family: verdana, geneva, sans-serif;\">Installing The Screen Utility<\/span><\/h2>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">These days, the Screen or GNU Screen package comes already installed on most Linux distributions. At the $ prompt, you can type the following to check if the utility is on your distro:<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">$ screen \u2013version<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">The output might look something like this:<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">Screen version 4.06.02 (GNU) 23-Oct-17<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">If the \u201cwhich screen version\u201d query doesn\u2019t return any details, the utility probably isn\u2019t on your system. In this case you can usually install it using one of the following commands:<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">For Linux Screen on Ubuntu and Debian, type:<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">sudo apt install screen<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">For Linux Screen on CentOS and Fedora, the syntax is:<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">sudo yum install screen<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">Otherwise, consult the documentation for the package manager of your distro.<\/span><\/p>\n<p>\u00a0<\/p>\n<h2><span style=\"font-family: verdana, geneva, sans-serif;\">Starting A Screen Session<\/span><\/h2>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">To start a screen session within your SSH session, you simply type:<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">screen<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">This will open a screen session, create a new window, and start a shell in that window. To get a list of commands, type:<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">Ctrl+a ?<\/span><\/p>\n<h2><span style=\"font-family: verdana, geneva, sans-serif;\">Managing Multiple Screen Sessions<\/span><\/h2>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">Screen is a \u201cterminal multiplexer,\u201d allowing you to run many terminal sessions within a single SSH session, detach from them, and reattach them as required.<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">If you intend to run more than one screen session, it\u2019s a good idea to give each of them a meaningful name, so that you can easily identify which tasks they are specifically handling. To create a named session, run the screen command with the following arguments:<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">screen -S session_name<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">So if you were starting a session you wanted to call \u201cslow build\u201d, at the $ prompt, you would use a command like this to name it:<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">$ screen -S slow-build<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">To create a new window with a shell in it, type Ctrl+a c, and the first available number from the range 0 to 9 will be assigned to it.<\/span><\/p>\n<h2><span style=\"font-family: verdana, geneva, sans-serif;\">Some Common Commands For The Screen Tool<\/span><\/h2>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">Some of the most common commands for managing Linux Screen windows include:<\/span><\/p>\n<p>\u00a0<\/p>\n<ul>\n<li><span style=\"font-family: verdana, geneva, sans-serif;\">Ctrl+a c This creates a new window (with shell)<\/span><\/li>\n<li><span style=\"font-family: verdana, geneva, sans-serif;\">Ctrl+a \u201d Lists all existing windows<\/span><\/li>\n<li><span style=\"font-family: verdana, geneva, sans-serif;\">Ctrl+a 0 Switches to window 0 (or whichever number you specify)<\/span><\/li>\n<li><span style=\"font-family: verdana, geneva, sans-serif;\">Ctrl+a A Renames the current window<\/span><\/li>\n<li><span style=\"font-family: verdana, geneva, sans-serif;\">Ctrl+a S Splits the current region horizontally into two separate regions<\/span><\/li>\n<li><span style=\"font-family: verdana, geneva, sans-serif;\">Ctrl+a | Splits the current region vertically into two regions<\/span><\/li>\n<li><span style=\"font-family: verdana, geneva, sans-serif;\">Ctrl+a tab Switches the input focus to the next region<\/span><\/li>\n<li><span style=\"font-family: verdana, geneva, sans-serif;\">Ctrl+a Ctrl+a Toggles between the current region and the previous one<\/span><\/li>\n<li><span style=\"font-family: verdana, geneva, sans-serif;\">Ctrl+a Q Closes all regions except the current one<\/span><\/li>\n<li><span style=\"font-family: verdana, geneva, sans-serif;\">Ctrl+a X Closes the current region<\/span><\/li>\n<\/ul>\n<h2><span style=\"font-family: verdana, geneva, sans-serif;\">Information You Can Get From Screen Listings<\/span><\/h2>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">With the Screen utility, you can start a screen session, then open any number of windows or virtual terminals inside that session. Processes running in Screen will continue to run when their windows are not visible \u2013\u00a0 even if you get disconnected. If you have multiple screen sessions running and you want to detach from the current screen and reattach to another, you\u2019ll first have to choose the session you want from a list.<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">The screenshot below illustrates the kind of result you might get after typing in the Screen list command screen -ls:<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\"><a href=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2019\/12\/screen-ls.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6817\" src=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2019\/12\/screen-ls.png\" alt=\"\" width=\"622\" height=\"117\" srcset=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2019\/12\/screen-ls.png 622w, https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2019\/12\/screen-ls-300x56.png 300w\" sizes=\"auto, (max-width: 622px) 100vw, 622px\"><\/a>(Image source: <a href=\"https:\/\/www.networkworld.com\/article\/3441777\/how-the-linux-screen-tool-can-save-your-tasks-and-your-sanity-if-ssh-is-interrupted.html\" rel=\"nofollow\" target=\"_blank\">NetworkWorld.com<img class=\"extlink-icon\" src=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/plugins\/external-links-nofollow-open-in-new-tab-favicon\/images\/extlink.png\"><\/a>)<\/span><\/p>\n<h2><span style=\"font-family: verdana, geneva, sans-serif;\">Detaching And Reattaching From Linux Screen<\/span><\/h2>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">At any time during a session, you can detach from the current screen by typing:<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">Ctrl+a d<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">Any program running in that screen session will continue to run after you detach from it.<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">If you have multiple screen sessions running on your machine, you\u2019ll need to add the screen session ID after the r switch of the \u201creattach\u201d command. In other words, reattaching to a particular session requires that you supply the name that you initially assigned to it. For example, to reattach to the \u201cslow build\u201d session you would type:<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">$ screen -r slow-build<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">Any process you left running there while you were working on another session should have continued processing while \u201cslow build\u201d was detached. If you now use the list command to ask about your screen sessions, you should see that the session you\u2019re currently reattached to is once again \u201cattached.\u201d<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\"><a href=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2019\/12\/screen-ls2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6819\" src=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2019\/12\/screen-ls2.png\" alt=\"\" width=\"622\" height=\"116\" srcset=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2019\/12\/screen-ls2.png 622w, https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2019\/12\/screen-ls2-300x56.png 300w\" sizes=\"auto, (max-width: 622px) 100vw, 622px\"><\/a>(Image source: <a href=\"https:\/\/www.networkworld.com\/article\/3441777\/how-the-linux-screen-tool-can-save-your-tasks-and-your-sanity-if-ssh-is-interrupted.html\" rel=\"nofollow\" target=\"_blank\">NetworkWorld.com<img class=\"extlink-icon\" src=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/plugins\/external-links-nofollow-open-in-new-tab-favicon\/images\/extlink.png\"><\/a>)<\/span><\/p>\n<h2><span style=\"font-family: verdana, geneva, sans-serif;\">Customization Options For Linux Screen<\/span><\/h2>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">When the Screen utility starts up, it reads its configuration parameters from\u00a0 the file \/etc\/screenrc and ~\/.screenrc if that file is present on your system. You can edit the .screenrc file to modify the default Screen settings to suit your preferences.<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">The screenshot below gives an example ~\/.screenrc configuration, with a customized status line and some additional options:<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\"><a href=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2019\/12\/startup_message.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6820\" src=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2019\/12\/startup_message.png\" alt=\"\" width=\"624\" height=\"261\" srcset=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2019\/12\/startup_message.png 624w, https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2019\/12\/startup_message-300x125.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\"><\/a>(Image source: <a href=\"https:\/\/linuxize.com\/post\/how-to-use-linux-screen\/\" rel=\"nofollow\" target=\"_blank\">Linuxize.com<img class=\"extlink-icon\" src=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/plugins\/external-links-nofollow-open-in-new-tab-favicon\/images\/extlink.png\"><\/a>)<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">The Linux Screen tool is especially useful if you need to run any time-consuming processes that could get interrupted if your SSH session disconnects prematurely for any reason. This screenshot gives a recap of some of the commands used above:<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\"><a href=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2019\/12\/screen-s.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6821\" src=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2019\/12\/screen-s.png\" alt=\"\" width=\"617\" height=\"95\" srcset=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2019\/12\/screen-s.png 617w, https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2019\/12\/screen-s-300x46.png 300w\" sizes=\"auto, (max-width: 617px) 100vw, 617px\"><\/a>(Image source: <a href=\"https:\/\/www.networkworld.com\/article\/3441777\/how-the-linux-screen-tool-can-save-your-tasks-and-your-sanity-if-ssh-is-interrupted.html\" rel=\"nofollow\" target=\"_blank\">NetworkWorld.com<img class=\"extlink-icon\" src=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/plugins\/external-links-nofollow-open-in-new-tab-favicon\/images\/extlink.png\"><\/a>)<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you\u2019re logging onto a service or running remote command line operations over a network link via the Secure Shell (SSH) protocol, the last thing you need is for your session to be cut off by a faulty connection. This scenario is all too common \u2013 but for Linux users, the Screen utility can prevent [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":6820,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[447,6,9],"tags":[],"class_list":["post-6815","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","category-tech","category-technical"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.poweradmin.com\/blog\/wp-json\/wp\/v2\/posts\/6815","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.poweradmin.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.poweradmin.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.poweradmin.com\/blog\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/www.poweradmin.com\/blog\/wp-json\/wp\/v2\/comments?post=6815"}],"version-history":[{"count":2,"href":"https:\/\/www.poweradmin.com\/blog\/wp-json\/wp\/v2\/posts\/6815\/revisions"}],"predecessor-version":[{"id":6822,"href":"https:\/\/www.poweradmin.com\/blog\/wp-json\/wp\/v2\/posts\/6815\/revisions\/6822"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.poweradmin.com\/blog\/wp-json\/wp\/v2\/media\/6820"}],"wp:attachment":[{"href":"https:\/\/www.poweradmin.com\/blog\/wp-json\/wp\/v2\/media?parent=6815"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.poweradmin.com\/blog\/wp-json\/wp\/v2\/categories?post=6815"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.poweradmin.com\/blog\/wp-json\/wp\/v2\/tags?post=6815"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}