What tty number is X using for a certain user?

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
User avatar
Pilosopong Tasyo
Level 6
Level 6
Posts: 1432
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

What tty number is X using for a certain user?

Post by Pilosopong Tasyo »

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.
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
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

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

Post by marcopolo »

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: 1432
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

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

Post by Pilosopong Tasyo »

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:
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: 738
Joined: Fri Nov 19, 2010 4:55 pm
Location: Newport, S Wales

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

Post by Roken »

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 5.12.14-zen1-1-zen, XFCE
Arch
Dual GTX1070 8Gb
AMD Ryzen 1800X
32Gb RAM
Habitual

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

Post by Habitual »

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?
User avatar
nunol
Level 9
Level 9
Posts: 2633
Joined: Sun Mar 06, 2011 9:25 pm
Location: Portugal

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

Post by nunol »

Also works in Mint 9 LTS and Mint 12!

There are a few more Easter egg's here: http://forums.linuxmint.com/viewtopic.php?f=58&t=84725
User avatar
Pilosopong Tasyo
Level 6
Level 6
Posts: 1432
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

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

Post by Pilosopong Tasyo »

Kindly stay on topic. Thanks.
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].
Locked

Return to “Scripts & Bash”