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

by on December 7, 2005 · 24 comments· LAST UPDATED January 24, 2009

in , ,

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

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 24 comments… read them below or add one }

1 rahul November 8, 2008 at 8:27 pm

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

Reply

2 Ameya December 12, 2008 at 5:52 am

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 at 5:58 am

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 nixCraft January 24, 2009 at 12:55 pm

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 at 5:11 am

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 at 11:42 am

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 at 11:41 am

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 at 2:33 pm

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 at 11:35 am

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 at 9:39 pm

Thanks for the Tips!

Reply

11 sankar June 25, 2010 at 7:23 am

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 at 9:46 pm

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

Reply

13 Jonas February 3, 2011 at 6:28 am

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 at 4:03 pm

@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 at 3:38 pm

Thanks for explaining so well about screen command usage.

Reply

16 Amernath February 9, 2012 at 10:11 am

Very useful one!

Reply

17 Mark March 27, 2012 at 2:22 pm

Here’s the .screenrc file I use, it will start three terminal screens within the session. I like a status line so this shows my host name, the screens and the date/time. I use a terminal with a white on black color scheme, you may want to alter these hardstatus string colors if your terminal colors are different.

hardstatus alwayslastline
hardstatus string ‘%{= kg}[ %{G}%H %{g}][%= %{= kg}%?%-Lw%?%{= kw}(%{= kW}%n*%f%t%?(%u)%?%{= kw})%{= kg}%?%+Lw%?%?%= %{g}][%{W}%Y-%m-%d %{W}%c %{g}]’

# Default screens
screen -t shell1 0
screen -t shell2 1
screen -t shell3 2

Reply

18 anirudh beria May 26, 2012 at 8:08 pm

nice.. thnx :)

Reply

19 Himanshu jain September 18, 2012 at 8:53 pm

very helpful. Thanks……………

Reply

20 Jose Tapia March 14, 2013 at 9:16 pm

Thanks for these tips, very useful bro

Reply

21 Tasos K April 16, 2013 at 3:49 pm

try tmux

Reply

22 prakash June 7, 2013 at 6:02 am

How to run multiple command on remote server using single ssh conection.

[root@nismaster ~]# ssh 192.168.122.169 “uname -a;hostname”
root@192.168.122.169‘s password:
Linux nisclient2 2.6.18-164.el5 #1 SMP Tue Aug 18 15:51:54 EDT 2009 i686 i686 i386 GNU/Linux
nisclient2

OR

[root@nismaster ~]# ssh 192.168.122.169 “uname -a && hostname”
root@192.168.122.169‘s password:
Linux nisclient2 2.6.18-164.el5 #1 SMP Tue Aug 18 15:51:54 EDT 2009 i686 i686 i386 GNU/Linux
nisclient2

Reply

23 Harneet Singh June 24, 2013 at 7:54 am

How do i embedd command such as ‘ctrl-a’ and ” :multiuser on ” in a script !!!
I am trying to embedd the script in my .profile file so that everytime I login , it creates a screen session , grants user access.
How do i embed “ctrl-a and multiuser on ” commands ?

Reply

24 abhimanyu October 31, 2014 at 1:27 am

thnx….
the the point information

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , , , , , , , , , , , , , ,

Previous post:

Next post: