Screen - The Path For Beginners

Discussion in 'Shell / Command Line' started by steelmanronald06, Jul 9, 2013.

  1. steelmanronald06

    steelmanronald06 Administrator Staff Member Staff Writer

    Messages:
    32
    Likes Received:
    12
    Trophy Points:
    8
    Overview & Uses

    Screen is a great tool, and highly recommended, tool for any Linux user. It provides a Terminal environment that can withstand network disconnects, random computer restarts, power outages at home and more. With screen you can be assured that if PuTTY, or whatever SSH Client you use, closes you will almost never lose your work. Sadly this is not true for a server restart, as far as I'm aware, but it still provides amazing possibilities. In this wiki you'll find instructions for installation and customization that will not only enhance screen for the best, but will also make it more enjoyable to use. If you're like me, once you start to use screen you'll never go back to mundane old terminal. Please note that all of these steps were done on my system, which I will provide screenshots for, but this is not the only way to set your screen environment up. To add to that, there are also numerous other alterations you may make to your screen environment. Feel free to play around with it and see what you come up with.

    Installation

    Installation of screen is quite simple, especially if you use some type of package manager. Once screen is set up, configuration is pretty seamless across all platforms. Here I will show you how to install screen on Debian using Apt-Get and on CentOS using YUM.

    Debian Installation
    Code:
    # su -
    # apt-get install screen
    
    CentOS/Red Hat Installation
    Code:
    # su -
    # yum install screen
    Hardstatus Line

    The hardstatus line is a line along the bottom of the screen that can be customized many different ways. It is a most useful feature of screen, when customized properly. It can list your current terminal, all open terminals, your system time clock, your hostname and more. Below you'll find a simple hardstatus line that I use on my system.
    Code:
    # vim ~/.screenrc
    Code:
    1. hardstatus alwayslastline
    2. hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{=kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B}%Y-%m-%d %{W}%c
    %{g}]'
    Line 1 ensures that the hardstatus line shows up at the bottom and line 2 tells the system what to actually include in the hardstatus line. That's it! You've successfully customized a hardstatus line for your screen.

    Default Screens on Startup

    Using screen is great, and all, but one of my favorite features is the ability to have screens auto-started. That way I'm not duplicating multiple windows and running repeat commands. Startup screens are easy to configure and you can have as many as you like. Add the following to the bottom of your ~/.screenrc:
    Code:
    screen -t top        0      top
    screen -t irssi 1 irssi
    screen -t workserver 2 ssh me@myworkserver
    That's all you need. I'll go through and explain what is going on. The screen -t tells it to start a new screen. The second portion, top for example, is the name of the screen that will appear in the hardstatus line. The number, 0 for example, is what number the screen will be. Duplicate numbers can cause you problems. The last portion, top on the far right, is the actual command to execute.
    screen -t ScreenName ScreenNumber ShellCommand

    Shortcuts

    The following are some of the most used shortcuts that lets you navigate through your screen environment. Note that unless modified by your .screenrc, by default every screen shortcut is preceded by Ctrl+a. Note that these shortcuts are case-sensitive.
    Code:
    0 through 9 � Switches between windows
    Ctrl+n � Switches to the next available window
    Backspace � Switches to the previous available
    Ctrl+a � Switches back to the last window you were on
    A � Changes window session name
    K � Kills a window session
    c � Creates a new window
    [ - Then use arrows to scroll up and down terminal
    Find out about more shortcuts in Screen's man pages. In your terminal, run: man screen.
    Sharing Screen

    Another great application of Screen is to allow other people to login to your station and to watch the work you are doing. It is a great way to teach someone how to do things on the shell.

    Allowing A Screen To Be Shared

    Allowing a screen to be shared by others is a simple process, as shown below:

    Code:
    # su -
    # chmod u+s /usr/bin/screen (Screen has to be SUID if you want to share a term between two users.)
    # chmod 755 /var/run/screen
    # exit
    # screen -DR
    Ctrl+a
    # :multiuser
    Ctrl+a
    # :acladd <usernameOfPersonConnectionToYourScreenSession>
    
    That's all you need to know to allow your screen to be shared.

    Connecting To A Shared Screen

    Connecting to a shared screen is even easier than sharing it. Simple have the person log into SSH using their username and do the following:

    Code:
    # screen -x <usernameofpersonsharingscreen>/ (please note that the trailing / is important)
    
    And now both users (from the host and guest) will be sharing a screen session and can run commands on the terminal.

    Work From More Than One Location

    Let's say you have a screen session open at work with X number of windows on it. Within those screens you may be running an IRC client, an SSH connection to the web server, and your favorite text-based email client. It's 5 p.m. and you have to go home, but you still have work left to do.

    Without Screen you would probably go home, VPN into your company's network, and fire up all the shells you need to keep working from home. With Screen, life gets a little easier.




    You can simply SSH into your workstation at work and list your available screen sessions with the command:

    Code:
    # screen -ls
    
    And connect to the sessions you were running at work with the command:

    Code:
    # screen -x screen_session_name
    
    This way screen will let you pick things up exactly from where you left off.

    Screen As Your Primary Window Manager

    Now that you have seen what Screen can do for you, you probably are wondering how to make it your main interaction point, like a terminal window manager. In the subsections below, I go over three different methods that you can use to help make this possible.

    irssi

    Let's start with IRC, a very common and popular chat system. Instead of using a graphical client like Pidgin, install Irssi. Irssi sports a slick console interface, tons of add-ons and scripts, and can be enhanced with Perl. It's even theme-able! In my Irssi tutorial I show you how to install irssi with screen, theme it and customize the windows. After you get used to using irssi+screen, you'll never go back to a graphical client again.

    Mutt

    Another important part of any user's setup is email. Today most people use graphical clients such as Thunderbird, Evolution, or Sylpheed. My favorite client happens to run in a terminal: Mutt. While Mutt isn't the easiest client in the world to set up, it sure is a joy to use. You can even use your favorite console text editor for doing emails.

    VIM

    Speaking of favorite text editors, there is a good chance that you work on some code projects or configurations. Instead of using gedit/kedit or powering up a heavy IDE such as Eclipse, you can pick up on Vim. Vim is a powerful text editor which, as is stated on the Vim website, could be considered an entire IDE in itself and sports syntax coloring in over 200 programming languages. If Vim doesn't fit your style, there is always emacs, nano, or JOE.
    Flash Taskbar in PuTTY on Screen Activity

    Thanks go to steviewdr for this contribution!

    Step 1: Putty Config

    Terminal -> Bell -> Taskbar/caption indication on bell: Flashing

    #Might want to save this to the default profile in PuTTY.

    Step 2: Screen Config This is the important step I had overlooked a few times.

    Code:
    vi .screenrc
    vbell off
    bell_msg '^GBell in window %n'
    #The ^G (CTRL G) is the key)
    
    Step 3: irssi config

    Code:
    vi .irssi/config
    settings = {
    "fe-common/core" = {
    beep_msg_level = "MSG HILIGHT";
    beep_when_away = "yes";
    beep_when_window_active = "yes";
    bell_beeps = "yes";
    };
    };
    
    After all this, screen will have to be restarted to take effect. Now, upon getting an email to pine or pm to irssi, PuTTY should flash on the taskbar.

    Final Configuration Example


    After all that work, you might come out with a final configuration page that looks like the one below. Please keep in mind that this is not the only way to do things, and that you might come up with a better solution.
    Code:
    hardstatus alwayslastline
    hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f %t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B}%Y-%m-%d
    %{W}%c %{g}]�
     
    # Default screens
    screen -t shell1 0
    screen -t shell2 1
    screen -t server 2 sh me@myserver
    screen -t IRC 7 irssi
    screen -t Mail 8 mutt
     
    vbell off
    bell_msg '^GBell in window %n'
    #The ^G (CTRL G) is the key)
    Conclusion

    Well that's all I've got for you. I hope that after going through this you have a better understanding of screen, and a greater appreciation for it. I hope that you not only come to love screen as I do, but can take what I have taught you and build upon it. If you have any suggestions, recommendations or would like to feature one of your ideas on this page, please feel free to E-Mail me!

    Ronald Steelman

    MySQLExchange.com
    Amit_*nix likes this.
  2. Amit_*nix

    Amit_*nix New Member

    Messages:
    15
    Likes Received:
    4
    Trophy Points:
    3

Share This Page