How do I Use Multiple Screens on One Terminal over ssh session?

by Vivek Gite on December 7, 2005 · 15 comments

Most of the time GUI is not available on remote Linux system, you login over ssh and start to work, if you need to run two or three task at a time, you login over ssh two or three times. However, with screen windows manager utility you can run multiple terminals at the same time from single console login over ssh session (UNIX guru uses the term called multiplexing for this concept). Any seasoned Linux admin should be aware of this nifty tool :)

Install screen if it is not installed under Debian Linux

Type the following command:
# apt-get install screen

FreeBSD user can use ports to install screen

# cd /usr/ports/misc/screen
# make install clean

Login to remote server over ssh

$ ssh me@myserver.com

(C) Start screen session. You can name your window/session (1 is name of session):
$ screen -S 1

Let us start pine mail reader on first session or window.
$ pine

(D) Next you would like to download something from ftp/http site while you are reading emails. You need to create another screen window by pressing special key combination. Press CTRL + a followed by c key (first hit CTRL+a, releases both keys and press c). As soon as you hit 'c' you will get new shell prompt at console. Now use wget to download belenix.iso.bz2 (Solaris live CD) from net:
$ wget http://www.genunix.org/distributions/belenix_site/binfiles/belenix.iso.bz2

(E) But how do I switch between these two tasks?

  • Switching between windows is the specialty of screen utility. So to switch between pine and wget window (or session) press CTRL+a followed by n key (first hit CTRL+a, releases both keys and press n).
  • To list all windows use the command CTRL+a followed by " key (first hit CTRL+a, releases both keys and press " ).
  • To switch to window by number use the command CTRL+a followed by ' (first hit CTRL+a, releases both keys and press ' it will prompt for window number).

Common screen commands

screen commandTask
Ctrl+a cCreate new window
Ctrl+a kKill the current window / session
Ctrl+a wList all windows
Ctrl+a 0-9Go to a window numbered 0 9, use Ctrl+a w to see number
Ctrl+a Ctrl+aToggle / switch between the current and previous window
Ctrl+a SSplit terminal horizontally into regions and press Ctrl+a c to create new window there
Ctrl+a :resizeResize region
Ctrl+a :fitFit screen size to new terminal size. You can also hit Ctrl+a F for the the same task
Ctrl+a :removeRemove / delete region. You can also hit Ctrl+a X for the same taks
Ctrl+a tabMove to next region
Ctrl+a D (Shift-d)Power detach and logout
Ctrl+a dDetach but keep shell window open
Ctrl-a Ctrl-\Quit screen
Ctrl-a ?Display help screen i.e. display a list of commands

Suggested readings:

See screen command man page for further details:
man screen

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

We're here to help you make the most of sysadmin work. So, subscribe!

{ 15 comments… read them below or add one }

1 rahul November 8, 2008

was really nice piece of info for this newbie thnx a lot for sharing…

Reply

2 Ameya December 12, 2008

very helpful. Thanx. I always wanted to use screen but was not aware of how to do it.

Reply

3 Diabolic Preacher January 24, 2009

as usual the most interesting tools’ tutorials come from this blog and they are just easy.

btw how do i get rid of the screens once i’m done. i found out -d detaches, but it still lists the screen. how to terminate?

Reply

4 Vivek Gite January 24, 2009

To kill the current window hit – Ctrl-a k
To quit screen hit – Ctrl-a Ctrl-\

I will update tutorial with frequently used commands.

Reply

5 jeffatrackaid.com March 5, 2009

I use screen all of the time. I’ve wrote about it some time ago. I find the disconnect and re-connect feature is great when trying to wrap up that last task at work. You can detach, go home, and then re-connect to see if your job is finished.

Reply

6 Jitendra August 24, 2009

Is there any sh script to open 20 screens using screen command in Linux? I know manually, but would like to know, using sh script. It will save my time to invoke 20 screens at a shot.

Thanks in Advance.

Reply

7 vignesh prabhu May 11, 2010

Well you can write a .screenrc file in your home directory. Here you can configure how many screens you want to open when you start screen. This one of the most important utility I install when I am working on any server. Dont know why it is not installed by default in most of the GNU/Linux systems.

P.S. I would ask the author to share a sample .screenrc file. A customized screen is much easier to use than to use the by-default screen.

Reply

8 Jack September 11, 2009

Jitendra – To invoke 20 at a time with bash:

for i in {1..20}; do screen -dmS $i; done

Reply

9 Ganesh B S December 31, 2009

Here we are using “Ctr+a” command for all the options. Is there any shortcut to change “Ctr+a” to some other user defined commands like F1 (functional keys)etc.

Reply

10 Pj June 23, 2010

Thanks for the Tips!

Reply

11 sankar June 25, 2010

this is very useful to me.
but still i have some doubts about that. how to get other system prompt via ssh in screen mode. normally we are using ssh hostname command. here how to get remote share systems using screen -x ps.tty.host in . this is not working ……. the host will be other…..
please give some tips..

Reply

12 wt0fx66 November 25, 2010

thanks a lot! i have been seeking for this info for a long time!

Reply

13 Jonas February 3, 2011

Is it possible to remote logon from home via ssh, start a session in some way, and disconnect without killing the session?

I want to logon and start a build, but I need to powerdown my client computer at home, and when I get to work, I want to be able to find my running (or finnished) build session.

Reply

14 vignesh prabhu February 3, 2011

@Jonas,
Screen utility can be used for the purpose. Just ssh to the remote machine and spawn a screen session and start the build in it. Now you can also close the ssh session. The screen session will be running even after you close the ssh session.

When you login again, you just need to run “screen -x” to reconnect to the screen session that you had spawned earlier.

Hope it helps.

Reply

15 shailesh March 7, 2011

Thanks for explaining so well about screen command usage.

Reply

Previous post:

Next post: