What tty number is X using for a certain user?

Forum rules
Before you post please read how to get help
User avatar
Pilosopong Tasyo
Level 6
Level 6
Posts: 1363
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

What tty number is X using for a certain user?

Postby Pilosopong Tasyo » Thu Nov 24, 2011 1:43 am

Basically, I'm looking for a better way to find out which particular tty number the GUI is running on for a certain user. So far, I'm able to accomplish this using the following (for illustration purposes, the target info being sought after is root's tty8):

Code: Select all

administrator@dg31pr ~ $ who
administrator tty7         2011-11-24 08:15 (:0)
root     pts/0        2011-11-24 11:59 (:1.0)
administrator pts/1        2011-11-24 11:06 (:0.0)
root     tty8         2011-11-24 11:59 (:1)

administrator@dg31pr ~ $ who | grep "root"
root     pts/0        2011-11-24 11:59 (:1.0)
root     tty8         2011-11-24 11:59 (:1)

administrator@dg31pr ~ $ who | grep "root" | grep "tty"
root     tty8         2011-11-24 11:59 (:1)

administrator@dg31pr ~ $ who | grep "root" | grep "tty" | tr -s ' '
root tty8 2011-11-24 11:59 (:1)

administrator@dg31pr ~ $ROOT_GUI_TTY=`who | grep "root" | grep "tty" | tr -s ' ' | cut -d ' ' -f2` ; echo $ROOT_GUI_TTY
tty8

administrator@dg31pr ~ $ ROOT_GUI_VT=`expr substr $ROOT_GUI_TTY 4 1` ; echo $ROOT_GUI_VT
8

administrator@dg31pr ~ $ sudo chvt $ROOT_GUI_VT
[sudo] password for administrator:

<display changes to show root's gui screen>

Is there a better way to do this? Say, a command I haven't read about that exactly tells me which virtual terminal number X is using for a certain user? I know switching between virtual terminals can easily be accomplished using Ctrl+Alt+F# keyboard combo. But that's not what I'm looking for. Thank you for the assist.
Image
o Give a man a fish and he will eat for a day. Teach him how to fish and he will eat for a lifetime!
o If an issue has been fixed, please edit your first post and add the word [SOLVED].

marcopolo
Level 1
Level 1
Posts: 6
Joined: Sun Dec 25, 2011 8:27 pm
Location: West Virginia

Re: What tty number is X using for a certain user?

Postby marcopolo » Sun Dec 25, 2011 8:44 pm

You may want to try the w command. I believe it is part of the procps package and should be included with LM. The FROM field will tell you where each user is logged in from. Here's an example:

Code: Select all

mark@flounder:~$ w
 19:40:32 up 2 days, 9 min,  3 users,  load average: 0.12, 0.05, 0.05
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
mark     tty1                      19:40   12.00s  0.40s  0.39s -bash
mark     pts/0    :0               10:47    0.00s  0.44s  0.00s w
mark     pts/1    :0               19:39   44.00s  0.42s  0.42s bash


BTW; it is generally considered bad practice to run X as root.

HTH,
mark

User avatar
Pilosopong Tasyo
Level 6
Level 6
Posts: 1363
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

Re: What tty number is X using for a certain user?

Postby Pilosopong Tasyo » Mon Dec 26, 2011 3:38 am

Hi Mark,

Thanks for replying. I considered the w command before, but the output wasn't any much different from the who command. For instance, the FROM field (3rd column) in w corresponds to the last field (5th column) in who. The w command also gave a few extras that I didn't need, that's why I settled with the who command before creating this thread.

I'm well aware of the implications of root, don't worry about it. I used it merely for illustration purposes. :mrgreen:
Image
o Give a man a fish and he will eat for a day. Teach him how to fish and he will eat for a lifetime!
o If an issue has been fixed, please edit your first post and add the word [SOLVED].

User avatar
Roken
Level 5
Level 5
Posts: 740
Joined: Fri Nov 19, 2010 4:55 pm
Location: Oldham, UK
Contact:

Re: What tty number is X using for a certain user?

Postby Roken » Mon Dec 26, 2011 8:52 am

If all that you are after is the actual tty number, this will do it:

Code: Select all

who | grep root | sed "s/^.*\tty//" | cut -c 1-2

You may need the extra grep, too:

Code: Select all

who | grep root | grep tty | sed "s/^.*\tty//" | cut -c 1-2
Kernel Linux Tex 3.2.2-1 (64 bit) xfce , burg
Arch
ATI Radeon HD 4670, 1GB
OSS4
AMD Phenom II X4 (965BE) @ 3.6 Ghz (Overclocked)
8Gb RAM
Image

User avatar
Habitual
Level 12
Level 12
Posts: 4083
Joined: Sun Nov 21, 2010 8:31 pm
Location: Youngstown, Ohio

Re: What tty number is X using for a certain user?

Postby Habitual » Wed Dec 28, 2011 4:21 pm

Just found this gem....

Code: Select all

who mom loves


worked on Ubuntu 10.10, CentOS 5.5, and OpenSUSE 11.4

Scary, isn't it?
If you want the things you've never had,
You have to do things you've never done.

User avatar
nunol
Level 9
Level 9
Posts: 2653
Joined: Sun Mar 06, 2011 9:25 pm
Location: Portugal

Re: What tty number is X using for a certain user?

Postby nunol » Tue Feb 28, 2012 12:08 pm

Also works in Mint 9 LTS and Mint 12!

There are a few more Easter egg's here: viewtopic.php?f=58&t=84725

User avatar
Pilosopong Tasyo
Level 6
Level 6
Posts: 1363
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

Re: What tty number is X using for a certain user?

Postby Pilosopong Tasyo » Sun Mar 04, 2012 2:53 am

Kindly stay on topic. Thanks.
Image
o Give a man a fish and he will eat for a day. Teach him how to fish and he will eat for a lifetime!
o If an issue has been fixed, please edit your first post and add the word [SOLVED].


Return to “Scripts & Bash”

Who is online

Users browsing this forum: No registered users and 4 guests