What is the definition of a "session" in linux?
When I run the "w" command, I see two users logged in (both are me). A quick search here tells that this is normal because "A single user can have multiple active sessions at any one time."
What is a session? Why do I have two of them when I've only logged into my system once?
Session
usually refers to shell sessions. A shell is what allows you to interact with the computer. It acts as a bridge between the user and the kernel. Whenever you run a command, it is the shell that captures your intent and tells the kernel to do its thing.
In most Linux flavors, the default shell is bash
and a new bash
session will be launched every time you open a new terminal. In the output of w
you posted, you seem to have your graphical login session (looks like you're sshing o the machine):
argento :0 18set13 ?xdm? 2days 1.58s gdm-session-worker [pam/gdm-password]
A few open terminal emulators:
argento pts/0 18set13 29:26m 5:26 5:26 rdesktop -g 1200x700 -u administrator -p XXXXXXXXX -d DOMAIN -K srv-wsus
argento pts/1 18set13 9days 0.16s 0.16s /bin/bash
argento pts/2 lun10 5.00s 0.09s 7.55s /usr/bin/python /usr/bin/terminator
argento pts/3 14:40 33:41 0.09s 0.03s vim notes.txt
argento pts/4 gio12 26:04m 0.10s 7.55s /usr/bin/python /usr/bin/terminator
argento pts/5 14:56 17:33 0.11s 0.11s /bin/bash
And you also seem to have logged in from a non-graphical tty:
argento tty2 15:11 1:01 0.09s 0.09s -bash
Each of these is a separate instance of your shell and each counts as its own session.
1. Although user login into single process(shell which is their command line interface), users ends up running many process as result of action like:
-> Running non-interactive task in background.
-> Switching among interactive task via job control.
-> Starting multiple process that work together through pipes.
-> Running windowing system such as X window system which allow multiple terminal windows to opened.
2. In order to handle all these processes, kernel need to form process groups comprise of these processes.
3. To mange these process groups further according to user, these groups are divided according to user into sessions.(Note here user is not linux user, but shell or terminal stands for user. Two terminals on same machine logged in by same linux user stands for 2 different user and thus 2 different user session here. A terminal can be controlling terminal for only single session).
The process that is running shell for that “user” or session is known as session leader.
4. Also note closing terminal by pressing ‘x’ button not make you out of that terminal. That terminal keeps running in background. If you want to over session from terminal, you have to logout from there by pressing CTRL+D or exit.
5. As you logout from terminal, kernel sends SIGHUP signal(similar to kill -1) to process that is session leader. Now every other process that is running in terminal is child of this session leader. These child processes had formed process groups that are controlled by this session leader process(parent process).
6. Session leader process send SIGHUP to each child process, some process terminate, while some can choose not to terminate(those suspended or running in background). Those choose not to terminate become orphaned as session overs and immediately adopted by init process(parent of all process in linux machine).
For more Refer Link The Process Model of Linux Development