Best way elevate the privileges programmatically u

2020-03-31 03:45发布

There is a standard way (working across Linux distributions) to launch a process (from another application) asking for the root password in order to elevate privileges?

I tried to use gksudo (it is installed in ubuntu by default), but under other distributions (or under other desktop manager) it may not be installed.

标签: linux root
4条回答
Lonely孤独者°
2楼-- · 2020-03-31 03:46

The only default thing is text mode su. Most distros have also sudo installed.

Now, in KDE based distros you'll have kdesu, while in GNOME based it'll be gksu and gksudo. Machines in Kerberized domains have ksu.

You might try to use /etc/sysconfig/desktop to see which is the default desktop.

查看更多
相关推荐>>
3楼-- · 2020-03-31 03:52

I would recommend looking at PolicyKit which is what most modern distros are using to accomplish this.

查看更多
淡お忘
4楼-- · 2020-03-31 03:55

That works everywhere but does not cache the password and asks for the root and not the user password (as sudo does):

su - -c command

EDIT: Not on ubuntu where the root-account is disabled. Probably you need something like that:

test -x /usr/bin/sudo && sudo command || su - -c command
查看更多
可以哭但决不认输i
5楼-- · 2020-03-31 04:10

Traditionally, if your application needs to allow a user to elevate privileges, it installs its own single-purpose setuid executable -- single-purpose meaning that it performs the task needed, instead of acting as a general-purpose launcher.

$ su -
# cp `type -p id` /usr/local/bin/root-id
# chown root:users /usr/local/bin/root-id
# chmod 4750 /usr/local/bin/root-id
$ /usr/local/bin/root-id
... euid=0(root) ...

OTOH setuid executables have also been a common source of security holes too, so exercise care.

查看更多
登录 后发表回答