bash 用于编辑 /etc/sudoers 文件的正则表达式模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10420713/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:10:47  来源:igfitidea点击:

Regex pattern to edit /etc/sudoers file

regexlinuxbashsedawk

提问by Satish

I want to remove (uncommnet #) wheel group in /etc/sudoers file so what would be the Regex pattern i should use?

我想删除 /etc/sudoers 文件中的(uncommnet #)轮组,那么我应该使用什么正则表达式模式?

#cat /etc/sudoers
....
....
## Allows members of the 'sys' group to run networking, software,
## service management apps and more.
# %sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS

## Allows people in group wheel to run all commands
# %wheel        ALL=(ALL)       ALL

## Same thing without a password
# %wheel  ALL=(ALL)       NOPASSWD: ALL

....
....

I want to remove # from following line.

我想从以下行中删除 #。

...
# %wheel  ALL=(ALL)       NOPASSWD: ALL
...

回答by Tim Pote

You shouldn't edit the /etc/sudoersfile with any sort of script. There's a reason for the visudocommand. Edits to the sudoersfile should be rare and well-controlled.

您不应该/etc/sudoers使用任何类型的脚本编辑文件。visudo命令是有原因的。对sudoers文件的编辑应该很少并且得到很好的控制。

That being said, if your editor for the visudocommand is vi, you can run something like :%s/^# %wheel/%wheel/to uncomment all of the lines what start with %wheel.

话虽如此,如果你的visudo命令编辑器是 vi,你可以运行类似的东西:%s/^# %wheel/%wheel/来取消所有以%wheel.

Or, if you reeeeeeally think it's necessary:

或者,如果您确实认为有必要:

sudo sed --in-place 's/^#\s*\(%wheel\s\+ALL=(ALL)\s\+NOPASSWD:\s\+ALL\)//' /etc/sudoers

Run it without the --in-placefirst to check the output. Use it at your own risk.

在没有第--in-place一个检查输出的情况下运行它。需要您自担风险使用它。

回答by tsh

No need to use sed. On centos you can simply create file at /etc/sudoers.d/

无需使用 sed。在centos上,您可以简单地在以下位置创建文件/etc/sudoers.d/

echo 'myuser ALL=(ALL) NOPASSWD: ALL' >> ./myuser
sudo chown root:root ./myuser
sudo mv ./myuser /etc/sudoers.d/

This will enable sudo without password for myuser. Check this for more information: https://unix.stackexchange.com/questions/375433/etc-sudoers-vs-etc-sudoers-d-file-for-enabling-sudo-for-a-user

这将启用无需密码的 sudo myuser。查看更多信息:https: //unix.stackexchange.com/questions/375433/etc-sudoers-vs-etc-sudoers-d-file-for-enabling-sudo-for-a-user

回答by Andrew Clark

The following should work:

以下应该工作:

sed -i 's/^#\s*\(%wheel\s*ALL=(ALL)\s*NOPASSWD:\s*ALL\)//' /etc/sudoers