首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Linux能否进行有条件的判断,在Linux的某一行插入内容?

Linux能否进行有条件的判断,在Linux的某一行插入内容?
EN

Server Fault用户
提问于 2021-11-29 04:07:44
回答 1查看 39关注 0票数 0

Linux可以根据条件判断在Linux中的某一行插入内容吗?

例如,我想将用户"test“添加到/etc/sudoers,让它可以切换到root:

代码语言:javascript
复制
  1 #
  2 # This file MUST be edited with the 'visudo' command as root.
  3 #
  4 # Please consider adding local content in /etc/sudoers.d/ instead of
  5 # directly modifying this file.
  6 #
  7 # See the man page for details on how to write a sudoers file.
  8 #
  9 Defaults        env_reset
 10 Defaults        mail_badpass
 11 Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"
 12 
 13 # Host alias specification
 14 
 15 # User alias specification
 16 
 17 # Cmnd alias specification
 18 
 19 # User privilege specification
 20 root    ALL=(ALL:ALL) ALL
 21 
 22 # Members of the admin group may gain root privileges
 23 %admin ALL=(ALL) ALL
 24 
 25 # Allow members of group sudo to execute any command
 26 %sudo   ALL=(ALL:ALL) ALL
 27 
 28 # See sudoers(5) for more information on "#include" directives:
 29 
 30 #includedir /etc/sudoers.d

我希望我的命令找到第20行(如果根ALL=( ALL :ALL) ALL),然后在第21行中添加以下内容

代码语言:javascript
复制
test    ALL=(ALL:ALL) ALL

Linux命令可以这样做吗?或者我只能通过编写诸如Perl或Python之类的代码来做到这一点?

我对Linux非常陌生,任何帮助都是非常感谢的!

EN

回答 1

Server Fault用户

回答已采纳

发布于 2021-11-29 06:03:55

在哪里添加行并不重要,sudo不关心订单。

把它附加到结尾:

代码语言:javascript
复制
echo 'test    ALL=(ALL:ALL) ALL' | sudo tee -a /etc/sudoers

或者,更好的是,对它使用包含dir:

代码语言:javascript
复制
echo 'test    ALL=(ALL:ALL) ALL' | sudo tee /etc/sudoers.d/test

这样,当sudoers文件在包更新期间发生更改时,不必手动更新它。

票数 2
EN
页面原文内容由Server Fault提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://serverfault.com/questions/1084818

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档