Useful screen command examples for SysAdmins

Useful screen commands examples

In this article, we are going to understand about the usage of screen command. We will see some practical screen command examples.

We will cover below points to understand about the screen command.

1. About Screen
2. Benefits of screen command
3. Screen command examples

1. About screen command

screen command is used to run the multiple processes in different shells as a background process.

It is a very useful command for system admins. Screen will offers multiple terminal processes inside a one single terminal manager.

2. Benefits of screen command

  • screen command will help to create a process which actually runs in the background and if you are network got fluctuated and if you are running any important command then normal window gets disconnected but in the screen, you can again login on the machine and reattached screen with running process.
  • It helps sysadmins to run rsync command with big data transfer, restoring or dumping large MySQL file etc this time screen command plays a very important part.

3. Screen command examples

Let's see some of the important screen command examples which are practically used by sysadmins. Most of the Linux distros come with the by default screen command.

If you don't have screen install on your system then run below command to install screen.

$ apt-get update
$ apt-get install screen

1. start screen session

This command will create a new terminal session for you. On which you can work like any other normal terminal.

To come out of screen you can use "cntrl+AD".

$ screen

2. List running or active screen sessions

To list out no. of screen session currently active on the system.

Use below command which will provide you a complete list of running screen sessions.

prashant@local ~ $ screen -ls
There is a screen on:
7765.pts-0.prashant-local (Friday 21 October 2016 07:47:49 IST) (Detached)
1 Socket in /var/run/screen/S-prashant.

3. Attach screen session

To reattach any one of screen session from a list of the session.

Use below command with option '-r' and screen id.

prashant@local ~ $ screen -ls
There is a screen on:
7765.pts-0.prashant-local (Friday 21 October 2016 07:47:49 IST) (Detached)
1 Socket in /var/run/screen/S-prashant.

prashant@local ~ $ screen  -r  7765.pts-0.prashant-local

4. Switch between screens

You can manage multiple screens at a time by just using simple shortcut keys.

If you have multiple screens running and you want to go through the screens one by one without detaching and reattaching screen then the first attached to any one of your screen using "screen -r 7765".

Now use "ctrl-A" and "n" to switch between the running screens. It will take you to the next screen.

When you want to go to previous screen use "Ctrl-A" and "p".

If you want to create new screen window use "Cntrl-A" and "c".

5. Terminate or Kill Screen

If you don't want screen session then you can terminate it by using "Cntrl+D". It will help to terminate unused screen from the system.

You can also use "ctrl-A" and "k" to kill running screen.

6. Protect Screen Session

The screen will provide a feature to lock or protect your running screen from others. You can use password protection on running screen session.

To make password protection on screen. First, create file "$HOME/.screenrc" on your system. and add below syntax to this file.

prashant@local ~ $ vim $HOME/.screenrc
password crypt_password

To create crypt_password you need to use below command which helps you to create a crypted password. 

prashant@local ~ $ mkpasswd abcd@123
mOKJoNzzC2rvU

Now open the .screenrc file and replaced the generated password with "crypt_password".

When you next time create a new session and reattach it will prompt you for the password.

prashant@local ~ $ screen -r 9288
Screen password:

Now your screen is password protected. 

I hope you like the article if you find any difficulties using screen command then please do comment your queries or problem via the comment section, till then stay tuned to techthings.org for more such valuable articles

 

Read : Useful rsync command examples for SysAdmins

Read : Useful MySql Commands Every SysAdmin Should Know

Leave a Reply

Your email address will not be published. Required fields are marked *