Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I would like to use a script to find out the username - of the user who is currently on the physical console (who has command of the keyboard, mouse and graphical display)?

I do not just want to know who runs X (as the current console can be switched to another terminal and another user).


To clarify:
Commands like w, who etc. show who is logged at which terminal. This is simple. I would like to know, however, who owns currently the physical console (display, keyboard etc.). For example, if two users run displays :0 and :1, and I can switch between the displays with ctrl+alt+7 and ctrl+alt+8, I would like to know which user currently has the connection between his or her display and the actual, physical screen.


One more clarification:
I am not interested in knowing my own username. I have a script, running as another user or root, that needs to know who uses the physical display. There can be several X sessions running by different users, some of them virtualized, some of them connected to a remote X server, but I am only interested in the one that is actually displaying output on the monitor.


Yet another clarification:
Whatever the command is, the output must be different after pressing Ctrl+Alt+F1 (switching to another virtual console).

share|improve this question
    
Is this a self Q&A? –  jokerdino Sep 29 '12 at 11:23
    
No, unfortunately. –  January Sep 29 '12 at 12:16
add comment

4 Answers 4

up vote 5 down vote accepted
+50

I'm afraid I can't give you a specific answer (because I don't know the tech well), but I believe you can find it out using D-Bus and ConsoleKit.

For example, when I switch from X to a VT and back, I get this listening on dbus-monitor:

$ dbus-monitor --system | grep ConsoleKit
signal sender=:1.16 -> dest=(null destination) serial=19039 path=/org/freedesktop/ConsoleKit/Session2; interface=org.freedesktop.ConsoleKit.Session; member=ActiveChanged
signal sender=:1.16 -> dest=(null destination) serial=19040 path=/org/freedesktop/ConsoleKit/Seat1; interface=org.freedesktop.ConsoleKit.Seat; member=ActiveSessionChanged
signal sender=:1.16 -> dest=(null destination) serial=19041 path=/org/freedesktop/ConsoleKit/Session2; interface=org.freedesktop.ConsoleKit.Session; member=ActiveChanged
signal sender=:1.16 -> dest=(null destination) serial=19042 path=/org/freedesktop/ConsoleKit/Seat1; interface=org.freedesktop.ConsoleKit.Seat; member=ActiveSessionChanged
   string "/org/freedesktop/ConsoleKit/Session2"

Specifically, you could use the GetActiveSession method. Here's how with dbus-send (may require sudo):

$ dbus-send --system --type=method_call --print-reply --dest=org.freedesktop.ConsoleKit /org/freedesktop/ConsoleKit/Seat1 org.freedesktop.ConsoleKit.Seat.GetActiveSession

Then, depending on your application, you'll want to check out the returned object's properties, which should be an object path to a Session.

share|improve this answer
2  
Very helpful, I think. When I switch sessions the reply switches correspondingly. In particular I could then enter the following command: dbus-send --system --type=method_call --print-reply --dest=org.freedesktop.ConsoleKit /org/freedesktop/ConsoleKit/Session2 org.freedesktop.ConsoleKit.Session.GetUser which replies with the current user-id. From there the library function getpwuid can get the username. –  John S Gruber Oct 5 '12 at 4:34
1  
This is definitely the answer I was looking for. Thank you very much! –  January Oct 8 '12 at 10:37
add comment
[geek@atremis ~]$ w

 20:02:02 up 2 days,  8:37,  3 users,  load average: 0.00, 0.01, 0.00

USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT

geek     tty1     :0               Thu11    2days 35.11s  0.18s pam: gdm-passwo

geek     pts/0    athena           Fri22   21:04m  0.03s  0.03s -bash

geek     pts/1    blackbeauty      20:02    0.00s  0.14s  0.10s w

[geek@atremis ~]$

You can use the w command to see who is logged on. The idle time and what should tell you what is being run - in this case gdm indicates an active log in, and the idle time indicates the user has not been at the system in 2 days. Contrast this with the login from blackbeauty, which has a 0.00 idle time, which indicates its currently in use.

share|improve this answer
    
No, this only tells me -- as you wrote -- who is logged in at which terminal. To use with a script, I would like to know which user is owning the physical console (keyboard, display etc.) –  January Sep 29 '12 at 12:12
    
P.S. idle time is also non-informative; I'm working on a script fiddling with a screensaver, but I want to know which user is running the screensaver that is actually displayed. –  January Sep 29 '12 at 12:21
    
the TTY and 'what' might work here, you'd just need to extract the necessay bits. –  Journeyman Geek Sep 29 '12 at 12:22
    
Um, no, because it can run wherever (in background, on another X server, in a virtual machine) –  January Sep 29 '12 at 12:26
add comment

There are many ways:

  • whoami
  • w
  • echo $USER
  • logname
share|improve this answer
add comment

Another command is

whoami

It shows current logeed username

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.