How to use Screen on Linux

韦衡
2023-12-01

GNU Screen is a terminal multiplexer. You can have multiple sessions / terminal from single Screen instance. Using screen your process is not interrupted when your connection is disconnected.

You might want to compile an application or Linux kernel on remote machine and it takes quite some time to finish, the first option is to login to remote machine, start the compile and wait until the compilation has finished without disconnecting your remote SSH connection. Of course, if your internet connection is interrupted or your client machine crashes the process will be stopped on the server and you'll have to start over.

The other option is to use Screen. You can login to the remote server, open Screen, do the complication, detach the server and you can disconnect your SSH connections. You can even go somewhere else and return to your SSH and return to the Screen you left before.

Another scenario is when you want to download large files on the server but you don't want to put the download on background so you can start your download inside Screen instead.

Installing GNU Screen

Screen might already installed by default on your system. If it's not installed yet you can use command mentioned in this section to install Screen

If you are using Ubuntu / Debian you can use command below to install screen :

$ sudo apt-get install screen

If you are using Red Hat / CentOS / Fedora you can use command below to install screen :

$ sudo yum install screen

Basic Screen Commands

To use screen, after connecting to your server, you can type screen on your terminal. It will open a new screen, the default will show the information screen. You can press space or return to close the information screen, it will get you back to terminal but this one is inside screen.

To detach screen you can use ctrl+a+d command. Detaching screen means exit from screen but you can still resume the screen later.

To resume screen you can use screen -r commmand from the terminal. you will get the screen where you left before.

To exit from this screen you can use ctrl+d command or type exit on command line.

That is the most basic command to start, detach and exit from screen. Now let's learn how to use screen further.

let's check whether we have running screen or not :

$ screen -lsNo Sockets found in /var/run/screen/S-panji.

No screen found. Let's create new screen by simply typing :

$ screen

The new screen window will be opened. On this window, press enter to close welcome screen and type top to run top monitoring utility

Let's navigate to the next window by pressing Ctrl+a n. It will show No other window notification on bottom left of your screen.

Type Ctrl+a c to create new window. On the second window we will run command vmstat

$ vmstat 5 5Let's have the third window by typing Ctrl+a c again. On the third window just type ls /, you can use another command of course, just make sure you can identify that it is the third window.

Now when we type Ctrl+a n it will go to the first window where we run top. If we type Ctrl+a n one more time it will go to the second window where we run vmstat. From here if you type Ctrl+a p you will go back to the first window where we run top command, but if you type Ctrl+a n again you will go to the third window.

As alternative you can also you Ctrl+a [N] where N is window number from 0 to 9.

Let's detach from the current screen by pressinng Ctrl+a d

When we list the screen again this time it will show the previously detached screen :

$ screen -lsThere is a screen on: 6779.pts-0.one (07/14/15 22:35:54) (Detached)1 Socket in /var/run/screen/S-panji.

To resume the previously detached screen you can run command :

$ screen -r

This will be successful since we only have one screen instance. Let's detach this screen one more time and start another screen.

$ screen

On the second screen session lets run vmstat command again. this time without option so it will only give result once.

Now, we'll detach the second screen by typing Ctrl+a d

If we list the available screen this time we'll see two screen session :

$ screen -lsThere are screens on: 6866.pts-0.one (07/14/15 22:52:25) (Detached) 6779.pts-0.one (07/14/15 22:35:54) (Detached)2 Sockets in /var/run/screen/S-panji.

When we do screen -r it will fail because we don't specify which screen that we want to resume :

$ screen -rThere are several suitable screens on: 6866.pts-0.one (07/14/15 22:52:25) (Detached) 6779.pts-0.one (07/14/15 22:35:54) (Detached)Type "screen [-d] -r [pid.]tty.host" to resume one of them.To resume to this to the second screen session we can run command :

$ screen -r 6866

We should see the vmstat output now. Lets terminate this screen session by typing Ctrl+d

If you want to terminate the first screen session you have to exit from each window by exiting from currently running application like top and vmstat and press Ctrl+d

Configuring Screen

You can configure Screen by editing /etc/screenrc file. If you want to make a local configuration to your account you can create your own configuration on ~/.screenrc

In this tutorial we'll use the password option to be added on our local .screenrc config file. The configuration option that you have to add is :

password crypt_password

To generate hashed password you can use command mkpasswd on Linux. On the command below we'll generate hash for secretpassword

$ mkpasswd secretpasswordnErsv1b8.UpSU

So the final line that you have to add to your ~/.screenrc file is :

password nErsv1b8.UpSUYou can now start screen as usual but when you resume a screen it will asks for password :

$ screen -rScreen password:

Using Screen Directly From SSH

The basic flow using screen is connecting to the server via SSH, then run screen on remote server. But you can run screen directly with one SSH command from your terminal.

$ ssh -t username@server screen

To directly resume the screen after connecting via SSH, you can use command below :

$ ssh -t username@server screen -r

If you have multiple screens running on the server you will also have to provide the screen PID as a value for option -r

$ ssh -t username@server screen -rusername@server's password:There are several suitable screens on: 1752.pts-0.one (07/13/15 03:34:41) (Detached) 1691.pts-0.one (07/13/15 03:32:58) (Detached)Type "screen [-d] -r [pid.]tty.host" to resume one of them.Connection to server closed.

To resume the first screen session directly you can use command :

$ ssh -t username@server screen -r 1752

References

Screen Options

screen to start screen

screen -r to resume screen

screen -dr to force detached screen, and resume screen

screen -x attached to running a screen session without detaching another user currently using screen. We can use this to share screen session.

screen -DDR is similar to -dr option. You can use this when using the -dr option doesn't work.

screen -X [command] When you use -X option you can directly run arbitrary commands inside screen.

screen -A force screen and all its windows to resize to the current window when attached.

screen -L Using -L will activate logging of the commands you type on the screen to a file. The file will be created on the directory where you started screen. It will be named screenlog.x where x is the window number.

Inside Screen

  • Ctrl+a c Create new window
  • Ctrl+a [0-9] Navigate between screens, you can use this navigation up to 10 windows (0-9).
  • Ctrl+a x Lock your terminal window. you will be asked to enter and confirm a password and also enter a password to go back to the window.
  • Ctrl+a n Navigate to the next window.
  • Ctrl+a p Navigate to the previous window.
  • Ctrl+a k Kill current the window. A confirmation of y/n will be snowed before the window really killed.
  • Ctrl+a A Enter title for the window.
  • Ctrl+a d Detach the current screen.
  • Ctrl+a ? Show screen help pages that list all command available inside screen.

Conclusion

In this tutorial we learned how-to use GNU screen. You can now do basic navigation on screen, create new screen, move between screen, detach and resume screen. You have additional tool on your belt that you can use on Linux command line which will be useful in all sorts of situations over your server administration lifetime.



https://hostpresto.com/community/tutorials/how-to-use-screen-on-linux/

 类似资料:

相关阅读

相关文章

相关问答