kbd
kbd copied to clipboard
Question: How to run as user
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?
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?
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 /proc mounted?
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
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
Is it something that can be fixed/changed?
@Witko Do you have /var/run/console/console.lock
in your system ?
I dont
In this case I do not know how to find owner of current console. Sorry.
Cant you use sth like whoami?
@Witko ehhh... No. If you run openvt under sudo, then the whoami response is root
.
$ sudo whoami
root
What about this?
#!/bin/bash
TTY=$(tty | grep -oP "/dev/\Kpts/.*")
who | grep "${TTY}" | awk '{print $1}'
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}'
$
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.
Can you show me output fgconsole
and who
?
[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)
and you do not have any process on tty1 ?
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
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 im using xinit
@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
$ sudo who am i | awk '{print $1}'
nathan
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?
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.
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.