kbd icon indicating copy to clipboard operation
kbd copied to clipboard

Question: How to run as user

Open Witko opened this issue 6 years ago • 26 comments

Hi, im trying to run command with openvt from running X session. When i run: openvt bash i get: openvt: Unable to open /dev/tty5: Permission denied when i run: sudo openvt bash i end up running the bash as root. Ultimately i want to be able to run openvt xinit... as logged user. This should be possible to do with: sudo openvt -u bash but then i get: openvt: Couldn't find owner of current tty! if i try: sudo openvt -- sudo -i -u ${user} xinit ... then the x will crash on not having access to the vt.

Is it possible to do this?

Witko avatar Dec 10 '17 00:12 Witko

To open new shell and switch to it run: sudo openvt -s -u -l -w /bin/bash. @Witko Please explain, what exactly do you want to do?

legionus avatar Jan 06 '18 00:01 legionus

Hi @legionus , first of all thanks for reply. im trying to run X in a new tty from X. So when in Plasma i want to run this command to spawn a new X and switch to it. When i run the command you provided it writes: openvt: Couldn't find owner of current tty! And thats it.

Witko avatar Jan 08 '18 12:01 Witko

@Witko /proc mounted?

legionus avatar Jan 08 '18 13:01 legionus

proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)

Witko avatar Jan 08 '18 15:01 Witko

Hm... it means that we could not find the process on this tty.

https://github.com/legionus/kbd/blob/master/src/openvt.c#L131-L139

legionus avatar Jan 08 '18 21:01 legionus

Is it something that can be fixed/changed?

Witko avatar Jan 09 '18 14:01 Witko

@Witko Do you have /var/run/console/console.lock in your system ?

legionus avatar Jan 19 '18 10:01 legionus

I dont

Witko avatar Jan 21 '18 12:01 Witko

In this case I do not know how to find owner of current console. Sorry.

legionus avatar Jan 21 '18 21:01 legionus

Cant you use sth like whoami?

Witko avatar Feb 07 '18 09:02 Witko

@Witko ehhh... No. If you run openvt under sudo, then the whoami response is root.

$ sudo whoami
root

legionus avatar Feb 26 '18 10:02 legionus

What about this?

#!/bin/bash
TTY=$(tty | grep -oP "/dev/\Kpts/.*")
who | grep  "${TTY}" | awk '{print $1}'

Witko avatar Mar 05 '18 20:03 Witko

No. This looks ugly. Also, it doesn't work on my laptop:

$ TTY=$(tty | grep -oP "/dev/\Kpts/.*")
$ echo $TTY
pts/10
$ who | grep  "${TTY}" | awk '{print $1}'
$

legionus avatar Mar 14 '18 00:03 legionus

It is only an idea which worked for me and potentially could be a different approach how to resolve the owner of tty and solve the problems with openvt. So please look at it from this side and don't judge the beauty.

Witko avatar Mar 14 '18 08:03 Witko

Can you show me output fgconsole and who ?

legionus avatar Mar 22 '18 14:03 legionus

[user@host ~]$ fgconsole 
1
[user@host ~]$ who
user    tty1         2018-03-23 09:46 (:0)
user    pts/0        2018-03-23 09:46 (:0)
user    pts/1        2018-03-23 09:47 (:0)
user    pts/2        2018-03-23 09:57 (:0)

Witko avatar Mar 23 '18 08:03 Witko

and you do not have any process on tty1 ?

legionus avatar Mar 23 '18 09:03 legionus

when running ps i can see:

2284 tty1     Ssl+   0:42 /usr/lib/xorg-server/Xorg -nolisten tcp -auth /var/run/sddm/{...} -background none -noreset -displayfd 17 -seat seat0 vt1

Witko avatar Mar 23 '18 09:03 Witko

im trying to run X in a new tty from X. So when in Plasma i want to run this command to spawn a new X and switch to it.

Wait, why you don't use startx <program> to open another X ?

legionus avatar Mar 27 '18 08:03 legionus

@legionus im using xinit

Witko avatar May 14 '18 11:05 Witko

@Witko ehhh... No. If you run openvt under sudo, then the whoami response is root.

$ sudo whoami
root

As per this answer https://stackoverflow.com/a/25281113 (the last third example), running script with sudo does not mask the underlying user if used like: sudo cat /proc/$$/loginuid

mprogram avatar Aug 06 '19 11:08 mprogram

$ sudo who am i | awk '{print $1}'
nathan

Nathoufresh avatar May 10 '20 10:05 Nathoufresh

im trying to run X in a new tty from X. So when in Plasma i want to run this command to spawn a new X and switch to it.

Wait, why you don't use startx <program> to open another X ?

He has made a user-friendly application for laptops which has two graphics card, where at least one is NVIDIA Graphics card. NVIDIA calls these type of laptops as Optimus laptop. Nowadays, these type of laptops accounts for most "gaming laptops". His program is called nvidia-xrun, see his repository. It's the only universal way -- working on all distros -- of playing games on Linux in these gaming laptops.

Now, let us build up a situation. Imagine that you are a new Linux user and want to play some cool games, but you, unfortunately, have an Optimus laptop. In such case, it would be intuitive not to log out your session in order to play your game, right? Changing TTY to, then, execute in the new command line code for playing the cool game would be even weirder. It's not about his concern, it's about a concern of a lot of Linux users, as such the ones here. Hence, being able to execute

openvt bash

without root permissions, have good practical applications. That's why he can't just on another TTY

startx

After this explanation, does this is being further investigated? What could be done to have the functionality work?

renanwp2 avatar Mar 31 '22 16:03 renanwp2

Guys, openvt opens the terminal as a privileged process. What problems can you have to drop privileges and switch to another user? Yes, it won't be a one liner.

Something like that:

read -r uid < /proc/self/loginuid ||:
chown -h "$uid" "$(tty)";
exec sudo -u "#$uid" "$@"

Instead of sudo in this script, you can use unshare(1) for example or any other way to switch to user.

legionus avatar Apr 01 '22 12:04 legionus

Guys, openvt opens the terminal as a privileged process. What problems can you have to drop privileges and switch to another user? Yes, it won't be a one liner.

Something like that:

read -r uid < /proc/self/loginuid ||:
chown -h "$uid" "$(tty)";
exec sudo -u "#$uid" "$@"

Instead of sudo in this script, you can use unshare(1) for example, or any other way to switch to user.

Wow! Quite involved command. Thanks for that. I will try to use the same ideas in the script.

renanwp2 avatar Apr 02 '22 03:04 renanwp2