Can Linux do conditional judgment to insert the content at a certain line in Linux
Can Linux do conditional judgment to insert the content at a certain line in Linux?
For example, I would like to add user "test" to /etc/sudoers to let it can switch to root:
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
I want my command to find row 20( if root ALL=(ALL:ALL) ALL ) then add the below content in row 21
test ALL=(ALL:ALL) ALL
Can the Linux command do that or I can only do that by coding such as Perl or Python?
I am very new to Linux, any help is appreciated!
Solution 1:
It doesn't matter where you add the line, sudo doesn't care about the order.
Just append it to the end:
echo 'test ALL=(ALL:ALL) ALL' | sudo tee -a /etc/sudoers
Or, even better, use the include dir for it:
echo 'test ALL=(ALL:ALL) ALL' | sudo tee /etc/sudoers.d/test
This way you don't have to manually update the sudoers file when it changes during package updates.