Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: How to run as user #9

Open
Witko opened this issue Dec 10, 2017 · 26 comments
Open

Question: How to run as user #9

Witko opened this issue Dec 10, 2017 · 26 comments

Comments

@Witko
Copy link

Witko commented Dec 10, 2017

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?

@legionus
Copy link
Owner

legionus commented Jan 6, 2018

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?

@Witko
Copy link
Author

Witko commented Jan 8, 2018

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.

@legionus
Copy link
Owner

legionus commented Jan 8, 2018

@Witko /proc mounted?

@Witko
Copy link
Author

Witko commented Jan 8, 2018

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

@legionus
Copy link
Owner

legionus commented Jan 8, 2018

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

@Witko
Copy link
Author

Witko commented Jan 9, 2018

Is it something that can be fixed/changed?

@legionus
Copy link
Owner

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

@Witko
Copy link
Author

Witko commented Jan 21, 2018

I dont

@legionus
Copy link
Owner

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

@Witko
Copy link
Author

Witko commented Feb 7, 2018

Cant you use sth like whoami?

@legionus
Copy link
Owner

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

$ sudo whoami
root

@Witko
Copy link
Author

Witko commented Mar 5, 2018

What about this?

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

@legionus
Copy link
Owner

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}'
$

@Witko
Copy link
Author

Witko commented Mar 14, 2018

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.

@legionus
Copy link
Owner

Can you show me output fgconsole and who ?

@Witko
Copy link
Author

Witko commented Mar 23, 2018

[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)

@legionus
Copy link
Owner

and you do not have any process on tty1 ?

@Witko
Copy link
Author

Witko commented Mar 23, 2018

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

@legionus
Copy link
Owner

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 ?

@Witko
Copy link
Author

Witko commented May 14, 2018

@legionus im using xinit

@mprogram
Copy link

mprogram commented Aug 6, 2019

@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

@Nathoufresh
Copy link

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

@renanwp2
Copy link

renanwp2 commented Mar 31, 2022

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?

@legionus
Copy link
Owner

legionus commented Apr 1, 2022

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.

@renanwp2
Copy link

renanwp2 commented Apr 2, 2022

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.

@yars068
Copy link

yars068 commented Nov 13, 2023

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.

Another solution may be make a user be a member of tty group and grants a group members to read proper /dev/tty...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants