如何授予 Linux 用户 sudo 访问权限?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11094826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to give a Linux user sudo access?
提问by Adam Boostani
I am trying to give sudo access to one of my users. What should I type in my terminal?
我正在尝试向我的一位用户授予 sudo 访问权限。我应该在我的终端输入什么?
采纳答案by Igor Chubin
You need run visudo
and in the editor that it opens write:
您需要运行visudo
并在它打开的编辑器中写入:
igor ALL=(ALL) ALL
That line grants all permissions to user igor
.
该行将所有权限授予 user igor
。
If you want permit to run only some commands, you need to list them in the line:
如果您只想允许运行某些命令,则需要在行中列出它们:
igor ALL=(ALL) /bin/kill, /bin/ps
回答by Lev Levitsky
This answerwill do what you need, although usually you don't add specific usernames to sudoers
. Instead, you have a groupof sudoers and just add your user to that group when needed. This way you don't need to use visudo
more than once when giving sudo
permission to users.
这个答案将满足您的需求,尽管通常您不会将特定的用户名添加到sudoers
. 相反,您有一组sudoers,只需在需要时将您的用户添加到该组。这样,您visudo
在sudo
授予用户权限时不需要多次使用。
If you're on Ubuntu, the group is most probably already set up and called admin
:
如果您使用的是 Ubuntu,则该组很可能已经建立并被称为admin
:
$ sudo cat /etc/sudoers
#
# This file MUST be edited with the 'visudo' command as root.
#
...
...
# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL
# Allow members of group sudo to execute any command
%sudo ALL=(ALL:ALL) ALL
# See sudoers(5) for more information on "#include" directives:
#includedir /etc/sudoers.d
On other distributions, like Arch and some others, it's usually called wheel
and you may need to set it up: Arch Wiki
在其他发行版上,例如 Arch 和其他一些发行版,通常会调用它wheel
,您可能需要对其进行设置:Arch Wiki
To give users in the wheel group full root privileges when they precede a command with "sudo", uncomment the following line: %wheel ALL=(ALL) ALL
要在使用“sudo”执行命令时为 wheel 组中的用户提供完全 root 权限,请取消注释以下行:%wheel ALL=(ALL) ALL
Also note that on most systems visudo
will read the EDITOR
environment variable or default to using vi
. So you can try to do EDITOR=vim visudo
to use vim
as the editor.
另请注意,在大多数系统上visudo
会读取EDITOR
环境变量或默认使用vi
. 所以你可以尝试做为编辑器EDITOR=vim visudo
来使用vim
。
To add a user to the group you should run (as root):
要将用户添加到组中,您应该运行(以 root 身份):
# usermod -a -G groupname username
where groupname
is your group (say, admin
or wheel
) and username
is the username (say, john
).
groupname
你的组在哪里(比如,admin
或wheel
),username
是用户名(比如,john
)。
回答by Maciej
Edit /etc/sudoers
file either manually or using the visudo application.
Remember: System reads /etc/sudoers
file from top to the bottom, so you could overwrite a particular setting by putting the next one below.
So to be on the safe side - define your access setting at the bottom.
/etc/sudoers
手动或使用 visudo 应用程序编辑文件。请记住:系统/etc/sudoers
从上到下读取文件,因此您可以通过将下一个设置在下面来覆盖特定设置。所以为了安全起见 - 在底部定义您的访问设置。