通过shell脚本将用户添加到sudoers
是否可以通过 shell 脚本将用户添加到 sudoers 文件中? 我环顾四周,仍然没有发现任何东西。
Is it possible to add users to the sudoers file through a shell script?
I've been looking around, still can't find anything.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
您可以简单地
echo
(当然,具有提升的权限)直接到/etc/sudoers
文件:(注意用户名和第一个
ALL之间的制表符
)或者,对于脚本:
然后保存到
somefile.sh
,chmod a+rx
它,然后运行 sudo ./somefile.sh< /code> 从终端窗口。
要添加多个用户,请将脚本更改为:
然后,像这样运行脚本(假设您将其保存为
addsudousers.sh
):即以空格分隔。
要从文件中读取名称:
listofusers.txt
也应该以空格分隔。编辑:贾皮·柯克正确地指出您不能直接调用
sudo echo ... >>> /etc/sudoers
因为>>
重定向是由 shell 处理的,此时 shell 已经删除了超级用户权限。但是,如果您运行包含 echo ... >> 的脚本/etc/sudoers 并且脚本本身具有超级用户权限,一切都应该正常工作。You could simply
echo
(with elevated privileges, of course) directly to the/etc/sudoers
file:(note the tab character between the username and the first
ALL
)Or, for a script:
Then save to
somefile.sh
,chmod a+rx
it, and runsudo ./somefile.sh
from a terminal window.To add multiple users, change the script to this;
Then, run the script like this (assuming you saved it as
addsudousers.sh
):that is, space-separated.
To read the names from a file:
listofusers.txt
should also be space-separated.Edit: Jappie Kirk rightly points out that you can't directly call
sudo echo ... >> /etc/sudoers
because the>>
redirection is handled by the shell, which has by that point dropped the superuser privileges. However, if you run a script that containsecho ... >> /etc/sudoers
and the script itself has superuser privileges, everything should work just fine.不,直接 echo 不起作用,您必须在子 shell 中运行它。请尝试以下操作:
sudo sh -c "echo \"group ALL=(user) NOPASSWD: ALL\" >> /etc/sudoers"
No, a straight echo won't work, you have to run it in a subshell. Try this instead:
sudo sh -c "echo \"group ALL=(user) NOPASSWD: ALL\" >> /etc/sudoers"
还有
sudo
组,您可以向其中添加用户(用于/etc/sudoers
的常见配置)There is also the
sudo
group, and you could add users to it (for common configurations of/etc/sudoers
)在基于 RedHat 的发行版上,使用:
并输入您的密码,然后:
在 sudoers 文件中添加用户。
on RedHat Based Distributions use:
and enter your password, then :
to add the user in sudoers file.
为了在 shell 脚本中授予用户 sudo 权限 (Unix/Linux),请使用 usermod 函数:
示例:
用于验证: 使用组功能(显示组成员身份)并在正确的用户下验证 sudo 组。
示例:
linux 文档的解释:
In order to grant to user sudo permission in shell script (Unix/Linux) use the usermod function:
example:
For Verification: use the groups function ( which show the group membership ) and verify the sudo group us under the right user.
example:
Explanation from linux documentation:
其他答案(例如生成子 shell)将起作用,但如果您想使用环境变量,则可能不起作用。我发现另一种选择对我来说非常好:
话虽这么说,事后看来是 20/20...如果通过脚本而不是通过 visudo 修改 sudoers,我会认真建议首先创建一个具有正确文件权限和内容的备份,因为您可以如果没有 pkexec、物理访问或重新启动等,将失去对任何 sudo 权限的访问。
Other answers such as spawning a subshell will work, but may not work if you want to use environmental vars. One alternative I found played really nicely for me:
This being said, hindsight is 20/20... If modifying sudoers via a script and not via visudo I would seriously recommend creating a backup with the right file permissions and contents first since you can lose access to any sudo rights without pkexec, physical access or a reboot etc.
单行创建带有密码的用户并在 sudo 组中。
useradd -p $(openssl passwd -1 密码) 用户名 -s /bin/bash -G sudo
Single line to create user with password and in sudo group.
useradd -p $(openssl passwd -1 PASSWORD) USERNAME -s /bin/bash -G sudo
在 Debian 和 Ubuntu 中,您可以将用户添加到
/etc/sudoers.d
目录。该目录有一个 README 文件。创建一个名为99_sudo_include_file
的文件并将其放入/etc/sudoers.d/
目录中。删除用户或添加用户很容易,只需创建一个新文件并覆盖旧文件即可。您只需回显新文件并在每次想要更改时覆盖旧文件即可。这样您就不会触及原始的
/etc/sudoers
文件In Debian and Ubuntu you can add users to the
/etc/sudoers.d
directory. The directory has a README file. Create a file called99_sudo_include_file
and drop it in the/etc/sudoers.d/
directory. It's easy to remove users or add users, just create a new file and overwrite the old file. You can simply echo your new file and overwrite the old file each time you want to change it.This way you don't touch your original
/etc/sudoers
file在不更改 /etc/sudoers 文件的情况下,您还可以通过将用户添加到wheel组(在Fedora、Centos ..上)和sudo组(在Ubuntu ecc..上)来授予sudoer权限。
以下是 Centos 中用户 jdoe 的示例:
$sudo usermod jdoe -G wheel
Without changing the /etc/sudoers file, you can also grant the sudoer privileges by adding the user to the wheel group (on Fedora, Centos ..) and the sudo group (on Ubuntu ecc..).
Here an example with Centos for the user jdoe:
$sudo usermod jdoe -G wheel
以 root 身份登录到您的计算机。 root 用户是唯一有权添加新用户的用户。
登录后,您现在可以尝试以下命令:
创建新用户。
adduser [用户名]
为用户添加密码
passwd [用户名]
为用户授予 root 权限
只需键入
即可编辑 visudo 文件
在此处输入代码
即可编辑 visudo 文件找到以下代码行:
root ALL=(ALL) ALL
然后添加以下代码:
原始帖子将在此链接上找到 Centos 6 – 创建 sudoers 用户
Login as root to your machine. The root user are the only one who has privilege to add new user.
Once you logged-in, you may now try the following commands below:
Create a new user.
adduser [username]
Add password to user
passwd [username]
Grant root privileges to user
Edit the visudo file by simply typing
enter code here
Find the following line of code:
root ALL=(ALL) ALL
Then add this code below:
The original post will find on this link Centos 6 – Creating sudoers user
我想继续将用户添加到 sudoers。
我已经创建了,但问题是当我运行两次 shell 脚本时它会再次添加。
请看下面我的脚本
=============
这是结果
如何检查或验证用户是否已经存在,因此不需要再次添加?
谢谢大家师父
需要您的建议
I want continue about add user to sudoers.
I already create, but the problem is when I run twice the shell script it will add again.
Please see below my script
=============
this is the result
How to check or verify if the user already exist, so don't need add again ?
Thank you All Master
Need your advice