Today I come across a requirement where you have to force a user to logout from a terminal. When I contacted the user and he said he did not logged in. But the thing is I can see the login session from w, last commands. I did this checking before asking him. Then how can we force a user session to terminate if you can not control user. To do that follow below steps and make him completely logout(force logout/terminate his session) by using some basic Linux commands. Let us start what we have to use to find users logged in and how to terminate/logout the user session we don't require. For this example purpose I created suri1 user which needs to be forced logout.

Step1: Check how many users logged in by using following commands

last | head

Example output:

surendra at linuxnix in ~
$ uptime
 07:20:24 up 3 days, 11:54,  3 users,  load average: 0.05, 0.08, 0.13
surendra at linuxnix in ~
$ w
 07:20:27 up 3 days, 11:54,  3 users,  load average: 0.05, 0.08, 0.13
USER     TTY      FROM             [email protected]   IDLE   JCPU   PCPU WHAT
suri1    tty1                      07:02   17:47   0.03s  0.02s -bash
surendra :0       :0               Fri19   ?xdm?   2:37m  1.12s init --user
surendra pts/8    :0               Sun13    3.00s  0.24s  0.00s w
surendra at linuxnix in ~
$ last | head
suri1    tty1                          Tue Jan 13 07:02   still logged in   
surendra tty1                          Tue Jan 13 07:01 - 07:02  (00:00)    
surendra pts/26       :0               Sun Jan 11 13:12 - 07:20 (1+18:07)   
surendra pts/8        :0               Sun Jan 11 13:12   still logged in   
surendra pts/0        :0               Sat Jan 10 23:21 - 13:12  (13:50)    
surendra :0           :0               Fri Jan  9 19:26   still logged in   
reboot   system boot  3.13.0-43-generi Fri Jan  9 19:25 - 07:20 (3+11:54)   
surendra :0           :0               Thu Jan  8 00:02 - down  (1+00:09)   
reboot   system boot  3.13.0-43-generi Thu Jan  8 00:02 - 00:12 (1+00:10)   
surendra pts/0        :0               Wed Jan  7 21:24 - 23:04  (01:40)  

From the above three command we get many insights about user sessions as listed below.

Related concept:   How to install Ubuntu 12.10 Linux?

1) From uptime command we come to know that there are three users logged in.

2) From w command we come to know from where they logged in(suri1 logged in from tty1, who is our user to be logged out).

3) From last command we come to know from what time they logged in(suri1 is still logged in.).

From these three things we can say which sessions are long. 

Step2: Now check for the process running by a particular user. For this example I create a test user called suri1 and let us see what he is upon by using ps command.

ps -ef | grep username


surendra at linuxnix in ~
$ ps -ef | grep suri1
suri1    16106 15956  0 07:02 tty1     00:00:00 -bash
surendra 16772 27412  0 07:24 pts/8    00:00:00 grep suri1

From the above output, we can see that he is just using bash command. If we can kill that process(16106) that is it, he will be logged out immediately.

Related concept:   Linux Directory Structure explained: /dev folder

Step3: Now kill his session by using kill command, if kill command is not terminating his session, use kill -9 which is a force kill for a process.

Note: You should be a root user to do below commands.

kill pid


kill -9 pid


surendra at linuxnix in ~
$ sudo su
[sudo] password for surendra: 
[email protected]:/home/surendra# kill 16106
[email protected]:/home/surendra# kill 16106
bash: kill: (16106) - No such process

Hope this helps to terminate user sessions.