Tuesday 1 September 2020

How To Use Screen Utility in Linux

Introduction:

Screen or GNU Screen is a terminal multiplexer. In other words, it means that you can start a screen session and then open any number of windows (virtual terminals) inside that session. Processes running in Screen will continue to run when their window is not visible even if you get disconnected.

Screen is a powerful program for the command line that allows you to run multiple terminals similar to the way you would have multiple tabs in a web browser. Plus, you can connect and detach from the session like you would a remote desktop session (like VNC or Windows Remote Desktop).

Install Linux GNU Screen

The screen package is pre-installed on most Linux distros nowadays. You can check if it is installed on your system by typing:

#screen --version
Screen version 4.06.02 (GNU) 23-Oct-17

If you don't have screen installed on your system, you can easily install it using the package manager of your distro.

Install Linux Screen on Ubuntu and Debian

#sudo apt install screen

Install Linux Screen on Oracle Linux

#yum install screen

Starting Linux Screen

To start a screen session, simply type screen in your console:

#screen

This will open a screen session, create a new window, and start a shell in that window.

Now that you have opened a screen session, you can get a list of commands by typing:

Ctrl+a ?

Starting Named Session

Named sessions are useful when you run multiple screen sessions. To create a named session, run the screen command with the following arguments:

#screen -S session_name

It’s always a good idea to choose a descriptive session name.

Working with Linux Screen Windows

When you start a new screen session, it creates a single window with a shell in it.

You can have multiple windows inside a Screen session

To create a new window with shell type Ctrl+a c ,the first available number from the range 0......9 will be assigned to it.

Below are some most common commands for managing Linux Screen Windows:

Ctrl+a c  Create a new window (with shell)

Ctrl+a "  List all window

Ctrl+a 0  Switch to window 0 (by number )

Ctrl+a A  Rename the current window

Ctrl+a S  Split current region horizontally into two regions

Ctrl+a |  Split current region vertically into two regions

Ctrl+a tab Switch the input focus to the next region

Ctrl+a Ctrl+a Toggle between the current and previous region

Ctrl+a Q  Close all regions but the current one

Ctrl+a X Close the current region 

screen is a much under utilised program, which provides the following functionality:

  • unlimited windows (unlike the hardcoded number of Linux virtual consoles)
  • unlimited windows (unlike the hardcoded number of Linux virtual consoles)
  • scrollback buffer (not limited to video memory like Linux virtual consoles)
  • scrollback buffer (not limited to video memory like Linux virtual consoles)
  • copy/paste between windows
  • copy/paste between windows
  • notification of either activity or inactivity in a window
  • notification of either activity or inactivity in a window
  • split terminal (horizontally and vertically) into multiple regions
  • split terminal (horizontally and vertically) into multiple regions
  • locking other users out of terminal
  • locking other users out of terminal
See also my screen settings
Note for nested screen sessions, use "Ctrl+a a" to send commands to the inner screen,
and the standard "Ctrl+a" to send commands to the outer screen.

KeyActionNotes
Ctrl+a cnew window 
Ctrl+a nnext windowI bind F12 to this
Ctrl+a pprevious windowI bind F11 to this
Ctrl+a "select window from listI have window list in the status line
Ctrl+a Ctrl+aprevious window viewed 
   
Ctrl+a Ssplit terminal horizontally into regionsCtrl+a c to create new window there
Ctrl+a |split terminal vertically into regionsRequires screen >= 4.1
Ctrl+a :resizeresize region 
Ctrl+a :fitfit screen size to new terminal sizeCtrl+a F is the same. Do after resizing xterm
Ctrl+a :removeremove regionCtrl+a X is the same
Ctrl+a tabMove to next region 
   
Ctrl+a ddetach screen from terminalStart screen with -r option to reattach
Ctrl+a Aset window title 
Ctrl+a xlock sessionEnter user password to unlock
Ctrl+a [enter scrollback/copy modeEnter to start and end copy region. Ctrl+a ] to leave this mode
Ctrl+a ]paste bufferSupports pasting between windows
Ctrl+a >write paste buffer to fileuseful for copying between screens
Ctrl+a <read paste buffer from fileuseful for pasting between screens
   
Ctrl+a ?show key bindings/command namesNote unbound commands only in man page
Ctrl+a :goto screen command promptup shows last command entered

Detach from Linux Screen Session

You can detach from the screen session at any time by typing:

Now detach from this screen session by pressing Ctrl-a then d. This means "detach".

Ctrl+a d

The program running in the screen session will continue to run after you detach from the session.

Reattach to a Linux Screen

To resume your screen session use the following command:

#screen -r

In case you have multiple screen sessions running on your machine,you will need to append the screen session ID after the r switch.

To find the session ID list the current running screen sessions with:

#screen -ls

There are screens on:

    10835.pts-0.linuxize-desktop   (Detached)

    10366.pts-0.linuxize-desktop   (Detached)

2 Sockets in /run/screens/S-linuxize.

If you want to restore screen 10835.pts-0, then type the following command:

#screen -r 10835

Customize Linux Screen

When screen is started, it reads its configuration parameters from /etc/screenrc and ~/.screenrc  if the file is present We can modify the default Screen settings according to our preferences using the .screenrc file.

Here is a sample ~/.screenrc configuration with customized status line and few additional options:

~/.screenrc

# Turn off the welcome message

startup_message off

# Disable visual bell

vbell off

# Set scrollback buffer to 10000

defscrollback 10000

# Customize the status line

hardstatus alwayslastline

hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %m-%d %{W}%c %{g}]'

Basic Linux Screen Usage

Below are the most basic steps for getting started with screen:

On the command prompt, type screen

Run the desired program

Use the key sequence Ctrl-a + Ctrl-a to detach from the screen session.

Reattach to the screen session by typing screen -r.

Common problems

Multiple sessions running

One thing that people new to screen often will do is start up multiple screen sessions and forget about them. Probably because they just ran screen without the -x option to reattach to the already running session. You can recover from this by running screen -x and either it will reattach to a single session or if you have multiple sessions open, it will list them out like this:

There are several suitable screens on: 25317.pts-39.ludwig (11/02/2009 04:49:52 PM) (Detached) 25214.pts-39.ludwig (11/02/2009 04:49:50 PM) (Detached) 25091.pts-39.ludwig (11/02/2009 04:49:46 PM) (Detached) Type "screen [-d] -r [pid.]tty.host" to resume one of them.

Now you need to choose which session you want to reattach to. The number you need to specify is the first number shown on the line. So if I wanted to attach to the second one, I would run:

screen -x 25214

Then I could do whatever I need to in that session.

Dead sessions

Another common problem that usually is the result of a server reboot is when your screen sessions are dead and some files need to be wiped before you can continue. To fix this, you just need to run screen -wipe

screen -wipe

Conclusion

In this tutorial, you learned how to use Gnu Screen. Now you can start using the Screen utility and create multiple screen windows from a single session, navigate between windows, detach and resume screen sessions and personalize your screen terminal using the .screenrc file.

Migration Large Amount of data then we can use screen utility.

Step 1: Install Screen Utility.

[root@@erptstdb01 ~]#rpm –ivh  screen-4.0.3-16.el6.x86_64

Step 2: Run Screen Command.

[root@@erptstdb01 ~]#screen

Step 3(a): Put your command whatever you want.

[root@@erptstdb01 ~]#cd /oradata1

[root@erptstdb01 oradata1]#rsync  -avzh /oradata1/app   /oradata1_temp/

Step 3(b): Detaching session

Ctrl + a + d

[root@erptstdb01 oradata1]#rsync -avzh /oradata1/oracle   /oradata1_temp/

Step 3(c): Detaching session

Ctrl + a + d

Step 4: List current attached screen

[root@erptstdb01 oradata1]#screen -ls

There are screens on:

    10835.pts-0. erptstdb01 (Detached)

    10366.pts-0. erptstdb01 (Detached)

2 Sockets in /run/screens/S-linuxize.

 Step 5: Reattach a session

[root@erptstdb01 oradata1]#screen -rx  10835

How to Setup NFS (Network File System) on RHEL/CentOS/Fedora and Debian/Ubuntu

NFS (Network File System) is basically developed for sharing of files and folders between Linux/Unix systems by Sun Microsystems in 1980. It allows you to mount your local file systems over a network and remote hosts to interact with them as they are mounted locally on the same system. With the help of NFS, we can set up file sharing between Unix to Linux system and Linux to Unix system.

Benefits of NFS

             NFS allows local access to remote files.

             It uses standard client/server architecture for file sharing between all *nix based machines.

             With NFS it is not necessary that both machines run on the same OS.

             With the help of NFS we can configure centralized storage solutions.

             Users get their data irrespective of physical location.

             No manual refresh needed for new files.

             Newer version of NFS also supports acl, pseudo root mounts.

             Can be secured with Firewalls and Kerberos.

Important Files for NFS Configuration

    /etc/exports: It’s a main configuration file of NFS, all exported files and directories are defined in this file at the NFS Server end.

    /etc/fstab: To mount a NFS directory on your system across the reboots, we need to make an entry in /etc/fstab.

    /etc/sysconfig/nfs: Configuration file of NFS to control on which port rpc and other services are listening.

Setup and Configure NFS Mounts on Linux Server

To setup NFS mounts, we’ll need at least two Linux/Unix machines. Here in this tutorial, I’ll be using two servers.

     NFS Server: root@erpcloneappl with IP-10.24.248.39

     NFS Client: root@erpclonedb with IP-10.24.248.40

Installing NFS Server and NFS Client

We need to install NFS packages on our NFS Server as well as on NFS Client machine. We can install it via “yum” (Red Hat Linux) and “apt-get” (Debian and Ubuntu) package installers.

Server Side: 10.24.248.39

[root@erpcloneappl ~]# rpm -qa *nfs*

nfs-utils-1.2.3-39.el6.x86_64

nfs-utils-lib-1.1.5-6.el6.x86_64

nfs4-acl-tools-0.3.3-6.el6.x86_64

Note: If Required Package not installed then first installed.

[root@erpcloneappl ~]# yum install nfs-utils nfs-utils-lib

Now start the services on both machines.

Note: Client server does not require NFS services to be running.

[root@erpcloneappl ~]# /etc/init.d/nfs start

[root@erpcloneappl ~]# chkconfig --level 35 nfs on

After installing packages and starting services on both the machines, we need to configure both the machines for file sharing.

Setting Up the NFS Server

First we will be configuring the NFS server.

Configure Export directory

For sharing a directory with NFS, we need to make an entry in “/etc/exports” configuration file. Here I’ll be creating a new or already existing directory named but we have share the existing directory  named as “/cloaneappl/apps/apps_st/comn/webapps/oacore/html/nicsi/attachment” in “/” partition

Now we need to make an entry in “/etc/exports” and restart the services to make our directory shareable in the network. to share with client server, you can also share an already existing directory with NFS.

[root@erpcloneappl ~]# vi /etc/exports

Add the following new line as shown below.

/cloaneappl/apps/apps_st/comn/webapps/oacore/html/nicsi/attachment 10.24.248.40/255.255.255.255(rw,sync)

In the above example, there is a directory in / partition named “/cloaneappl/apps/apps_st/comn/webapps/oacore/html/nicsi/attachment” is being shared with client IP “10.24.248.40” with read and write (rw) privilege, you can also use hostname of the client in the place of IP in above example.

root@erpcloneappl ~]# /etc/init.d/nfs stop

root@erpcloneappl ~]# /etc/init.d/nfs start

NFS Options

Some other options we can use in “/etc/exports” file for file sharing is as follows.

    ro: With the help of this option we can provide read only access to the shared files i.e client will only be able to read.

    rw: This option allows the client server to both read and write access within the shared directory.

    sync: Sync confirms requests to the shared directory only once the changes have been committed.

    no_subtree_check: This option prevents the subtree checking. When a shared directory is the subdirectory of a larger file system, nfs performs scans of every directory above it, in order to verify its permissions and details. Disabling the subtree check may increase the reliability of NFS, but reduce security.

    no_root_squash: This phrase allows root to connect to the designated directory.

For more options with “/etc/exports“, you are recommended to read the man pages for export.

[root@erpcloneappl ~]# vi /etc/fstab

Add the following new line as shown below.

10.24.248.39:/cloaneappl/apps/apps_st/comn/webapps/oacore/html/nicsi/attachment /ATTACHMENT nfs rw,vers=4,addr=10.24.248.39,clientaddr=10.24.248.40                                 0 0

Refresh the server’s list after modifying /etc/exports

[root@erpcloneappl ~]#  exportfs –r

Displays a list of shares files and options on a server

[root@erpcloneappl ~]#  exportfs –v

[root@erpcloneappl ~]# showmount -e 10.24.248.39

Above command shows that a directory named “cloaneappl/apps/apps_st/comn/webapps/oacore/html/nicsi/attachment” is available at “10.24.248.39” to share with your server.

Setting Up the NFS Client

After configuring the NFS server, we need to mount that shared directory or partition in the client server.

Mount Shared Directories on NFS Client

Now at the NFS client end, we need to mount that directory in our server to access it locally. To do so, first we need to find out that share available on the remote server or NFS Server.

Client Side: 10.24.248.40

Note: Client server does not require NFS services to be running.

[root@erpclonedb ~]# rpm -qa *nfs*

nfs-utils-1.2.3-39.el6.x86_64

nfs-utils-lib-1.1.5-6.el6.x86_64

nfs4-acl-tools-0.3.3-6.el6.x86_64

First of all, create a directory which will be mount point for the NFS share.

[root@erpclonedb ~]# mkdir /ATTACHMENT

[root@erpclonedb ~]# chmod 777 /ATTACHMENT

 [root@erpclonedb ~]# chown applmgr:dba /ATTACHMENT

[root@erpclonedb ~]# showmount -e 10.24.248.39

Above command shows that a directory named “cloaneappl/apps/apps_st/comn/webapps/oacore/html/nicsi/attachment” is available at “10.24.248.39” to share with your server.

[root@erpclonedb ~]# cat /etc/exports

Add the following new line as shown below.

/ATTACHMENT 10.24.248.39/255.255.255.192 (rw)

Mount Shared NFS Directory

To mount that shared NFS directory we can use following mount command.

[root@erpclonedb ~]# mount -t nfs 10.24.248.39:/cloaneappl/apps/apps_st/comn/webapps/oacore/html/nicsi/attachment /ATTACHMENT

The above command will mount that shared directory in “/ATTACHMENT” on the client server. You can verify it following command.

[root@erpclonedb ~]# mount | grep nfs

The above mount command mounted the nfs shared directory on to nfs client temporarily, to mount an NFS directory permanently on your system across the reboots, we need to make an entry in “/etc/fstab“.

[root@erpclonedb ~]# vi /etc/fstab

Add the following new line as shown below.

10.24.248.39:/cloaneappl/apps/apps_st/comn/webapps/oacore/html/nicsi/attachment /ATTACHMENT nfs rw,vers=4,addr=10.24.248.39,clientaddr=10.24.248.40 0 0

Removing the NFS Mount

If you want to unmount that shared directory from your server after you are done with the file sharing, you can simply unmount that particular directory with “umount” command. See this example below.

[root@erpclonedb ~]# umount /ATTACHMENT

You can see that the mounts were removed by then looking at the filesystem again.

Important commands for NFS

Some more important commands for NFS.

             showmount -e : Shows the available shares on your local machine

             showmount -e <server-ip or hostname>: Lists the available shares at the remote server

             showmount -d : Lists all the sub directories

             exportfs -v : Displays a list of shares files and options on a server

             exportfs -a : Exports all shares listed in /etc/exports, or given name

             exportfs -u : Unexports all shares listed in /etc/exports, or given name

             exportfs -r : Refresh the server’s list after modifying /etc/exports

This is it with NFS mounts for now, this was just a start, I’ll come up with more option and features of NFS in our future articles. Till then, Stay connected with Tecmint.com for more exciting and interesting tutorials in future. Do leave your comments and suggestions below in the comment box.

Create Directory:

CREATE OR REPLACE DIRECTORY XXNICSI_ATTACHMENT AS '/cloaneappl/apps/apps_st/comn/webapps/oacore/html/nicsi/attachment';

GRANT EXECUTE, READ, WRITE ON DIRECTORY SYS.XXNICSI_ATTACHMENT TO APPS WITH GRANT OPTION;

GRANT EXECUTE, READ, WRITE ON DIRECTORY SYS.XXNICSI_ATTACHMENT TO PUBLIC WITH GRANT OPTION;