Monday, August 6, 2007

create virtual terminals with single ssh connection

How to create virtual terminal from a single ssh connection?
How to have multiple terminal screen from a single ssh connection?
How to disconnect / reconnect back with running download jobs after disconnecting from ssh?
How to re-establish pending jobs and restore screen after exiting ssh connection?
How to detach and reattach ssh connections?
How to know which tty does your current ssh connection is connected to?
How to restrict root logins via ttys and vcs?

Here's a quick and brief way on how to do this stuff.

Screen is nice linux terminal manager and utility on accomplishing simple above mentioned functions. This terminal screen manager is installed along with default server configuration tools group package.

In redhat-based distro like Fedora and CentOS, there is a file called /etc/securetty which is root writable and readable only. This file lists down all valid and allowed ttys and virtual consoles where the root user is only allowed to login into. Usually, atleast one tty should be present with this file to give way for physical and direct server root logins. That means, if you only have tty2 listed from this file, root cannot login directly via tty1 or to any other known ttys or even pressing key combinations Ctrl+F1 to Ctrl+F6. Ctrl+F7 brings you to X.

Now, how to know which tty your currently logged in user logs into?

# tty

Querying linux for all logged in users would give you a hint like so

# who
# w

How to know if there is an existing ssh screen session active inside the linux. Here's how to verify it, issue as root:

# screen -ls
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
No Sockets found in /var/run/screen/S-root.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Now, from the server, create a new screen terminal session USING root for example, simply issue and follow the following root commands:

# screen
# clear
# echo REMEMBER THIS SCREEN VERTITO?

and verify for any active screen terminal session again by doing so

# screen -ls
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
There is a screen on:
2374.pts-3.ver (Attached)
1 Socket in /var/run/screen/S-root.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

As you can see, there is an existing single attached screen terminal currently active, which is your current screen session at this point.

Now, close your current terminal session by hitting X button for closing the terminal button or hitting Ctrl+F4 forcibly. DO NOT HIT Ctrl+D or even typing logout and exit from your ssh connection. Doing so, would disconnect your screen session and your ssh session.

Try to login back via ssh to same remote host. Issue the following as root:

# screen -ls
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
There is a screen on:
3300.pts-3.ver (Detached)
1 Socket in /var/run/screen/S-root.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Since you forcibly closed your ssh session, you have left your screen terminal session in a detached mode, as you can see from above results.

How to re-establish back the detached screen session using current ssh connection?

You are already aware of an existing detached screen session, all you need to do is to reattached you ssh session with that screen session. Just follow these steps:

# screen -d -r

You should be back with your previous screen session and terminal. Noticed the lines

REMEMBER THIS SCREEN VERTITO?

which was echoed to the screen before you forcibly closed your ssh connection?

This can be handy on different occassion and situations. Let me give you some ideas.

Supposed that you are currently downloading 4GB of DVD ISO image file from the office and your wife called you over the phone to come home earlier for a sweet dinner outside? Use screen utility before issuing your download command. This way, you left the process of downloading active. And when you try to ssh back from your house remotely, you can reattach back to that same screen session (with same screen display) like the above demonstration.

Another instance.

You are a compile geek trying to modify your kernel source settings from your office area. Assuming that it would take 2 hours for the kernel compilation to be completed. This is another way to launch a screen session from your office desktop one step before you issue your compilation commands scripts. Later on and from a different location, simply reattach back to that detached and active screen session.

Screen command makes use of /etc/screenrc as its conf file.

Some screen shortcut keys:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Ctrl+a is the control key for screen

Ctrl+a c = creates a nwe screen session
Ctrl+a n = next screen session
Ctrl+a p = previous screen session
Ctrl+a " = list all screen session for selection
= close the current list of screen sessions
Ctrl+a A = assign a name for current screen session
Ctrl+a w = shows terminal session names using terminal title bar
Ctrl+a = enables copy mode ;
= when using keyboard , arrow buttons,pg down/up, hit space to begin copy,and space
to end buffer copy. alternatively, use mouse right click copy/paste.
= returns you back to normal screen mode
Ctrl+a d = detaches current screen session
Ctrl+a 1 = first active screen session
Ctrl+a 2 = second active screen session and so on..
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# screen -ls = list out screen sessions
# screen -d -r = reattach a session

See the man page for more.

# man screen

Hope you enjoy this screen utility session like I do.

Did you try these during screen session? Noticed something different?

# who
# w

Cheers

0 comments:

Sign up for PayPal and start accepting credit card payments instantly.
ILoveTux - howtos and news | About | Contact | TOS | Policy