像UAC一样自动调用gksudo
这是关于我通过玩游戏“输入命令并记住在前面加上 sudo ,否则你的手指会被打”而感到压力的事情。
我想知道是否可以以某种方式配置我的 Linux 系统或 shell,这样当我忘记输入“sudo apt-get install emacs”时,gksudo 会启动,而不是仅仅告诉我我做错了什么,从而允许我承认我的资格并继续前进。就像 Windows 上的 UAC 一样。
谷歌搜索还没有帮助我..
那么这可能吗?我错过了什么吗?或者我要求一个方圆?
编辑2010年7月25日:感谢大家的关注。不幸的是,Daenyth 和 bmargulies 的答案和解释是我所期望/担心的,因为我不可能在提交这个问题之前通过谷歌搜索解决方案。我希望有一天有个好人能为此提供有效的解决方案。
BR, 基督教
This is about me being stressed by playing the game "type a command and remember to prepend sudo or your fingers will get slapped".
I am wondering if it is possible somehow to configure my Linux system or shell such that when I forget to type e.g. "sudo apt-get install emacs", instead of just telling me that I did something wrong, gksudo would get launched, allowing me to acknowledge my credentials and get on moving. Just like UAC does on windows.
Googling hasn't helped me yet..
So is this possible? Did I miss something? Or am I asking for a square circle?
Edit 2010 July 25th: Thanks everyone for your interrest. Unfortunately, Daenyth and bmargulies answers and explanations are what I anticipated/feared since it was impossible for me to google-up a solution prior to submitting this question. I hope that some nice person will someday provide an effective solution for this.
BR,
Christian
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Linux 不允许这样做。与 Windows 不同,Windows 中任何程序都可以启动对话框,并且 UAC 位于内核中,Linux 程序不一定具有 GUI 功能,从这个意义上说,sudo 并不位于内核中。程序无法调用来提升权限(除非它一开始就以权限启动并有意设置了权限)。 sudo 是一个具有 setuid 权限的独立可执行文件,用于检查权限。如果它喜欢它所看到的内容,它会分叉 shell 来执行命令行。这不能颠倒过来。
正如其他帖子中所建议的,您可能能够想出一些“shell 游戏”来安排为您运行 sudo 来执行一些枚举的命令列表,但这就是您将得到的全部。
Linux doesn't allow for this. Unlike Windows, where any program can launch a dialog box, and UAC is in the kernel, Linux programs aren't necessarily GUI-capable, and sudo is not, in this sense, in the kernel. A program cannot make a call to elevate privilege (unless it was launched with privilege to begin with and intentionally setuid'd down). sudo is a separate executable with setuid privilege, which checks for permission. If it likes what it sees, it forks the shell to execute the command line. This can't be turned inside out.
As suggested in other posts, you may be able to come up with some 'shell game' to arrange to run sudo for you for some enumerated list of commands, but that's all you are going to get.
您可以使用 preexec 挂钩函数执行您想要的操作,类似于 command-not-found 包。
You can do what you want with a preexec hook function, similar to the command-not-found package.
考虑到当前的 Linux 软件堆栈,没有办法做到这一点。此外,MS 对此行为拥有专利 --
呈现一个用户界面,识别有权允许任务的帐户,以响应基于用户当前帐户不具有该权限而被禁止的任务。
There's no way to do this given the current linux software stack. Additionally, MS has a patent on this behavior --
present a user interface identifying an account having a right to permit a task in response to the task being prohibited based on a user's current account not having that right.
我认为这并不能以一般方式真正起作用(自动决定哪个应用程序需要管理员权限)。但是,您可以为每个应用程序创建这样的别名:
如果您现在输入 apt-get install firefox ,则 gnome 会要求输入管理员密码。您可以将命令存储在
~./bashrc
中I don't think this really works in a general way (automatically deciding which application needs admin rights). However you could make aliases like this for every application:
If you now enter
apt-get install firefox
the gnome asks for the admin password. You can store the commands in~./bashrc
您可以使用如下所示的 shell 脚本:
如果命令返回非零返回代码(即如果失败),这将运行参数中给出的带有 sudo 前缀的命令。
例如,将其用作“SCRIPT_NAME apt-get install emacs”。您可以将其保存在 $PATH 中的某个位置,并将其设置为这样的别名(如果您将其保存为 do_sudo):
编辑: 这对于像 这样的程序不起作用 code>synaptic 可以为非 root 用户工作,但会给予他们较少的权限。但是,如果应用程序在没有 root 权限的情况下调用时失败(如 apt-get 所做的那样),则此方法可以正常工作。
You could use a shell script like the following:
This will run a command given in the arguments with a sudo prefix if the command came back with a non-zero return code (i.e. if it failed).
Use it as in "SCRIPT_NAME apt-get install emacs" for example. You may save it somewhere in your $PATH and set it as an alias like this (if you saved it as do_sudo):
Edit: That does not work for programs like
synaptic
which do work for non-root users but will give them less privileges. However, if the application fails when invoked without root privileges (likeapt-get
does) this works fine.如果您希望始终以 root 身份运行命令,但可能已经是 root,则可以通过在其周围封装一个小 bash 脚本来解决此问题:
如果您将此称为
alwaysroot。 bash
并将其放在 PATH 上的正确位置,然后您可以像这样调用其他程序:它甚至可以正确处理带有空格的参数。
In the case where you want to always run a command as root but might already be root, you can solve this by wrapping a little bash script around it:
If you call this something like
alwaysroot.bash
and place it in the right spot on your PATH, then you can call your other program like this:It even handles arguments with spaces in correctly.