I want to edit the sudoers files in Solaris by removing #
or adding #
in it on a specific line, so how can I write the script for this? My sudoer sample file given below:
# The following line allows su without options/arguments and sux to user root
Cmnd_Alias SU_ROOT = /usr/bin/su "",\
/usr/local/bin/sux - root
# Defaults specification
Defaults:%saptb !authenticate
# User privilege specification
%saptb ALL=(root)SU_SAP
#Uncomment this line when SAP requires root access
%saptb ALL=(root)SU_ROOT
##### END SAP-TB specific ######
#
#
#Tivoli ITM Tools Team Sudo Right
#
%cgtools ALL=(root) NOPASSWD: /opt/IBM/ITM/bin/*
In this above sudoers file i want add #
before the only line of %saptb ALL=(root)SU_ROOT
Do a substitution with sed
:
# Comment out the line %saptb ALL=(root)SU_ROOT
sudo sed -Ei 's/^(%saptb.*SU_ROOT.*)/#\1/' /etc/sudoers
Explanation:
-E
use extended regex
-i
edit the file in place.
s/ # Substitution
^ # Match the start of the line
( # Capture the following
%saptb # Followed by %saptb
.* # Followed by anything
SU_ROOT # Followed by SU_ROOT
.* # Followed by anything
) # Close capture
/ # Replace with
# # A hash
\1 # Followed by the captured line
To uncomment lines the principle is the same:
#
#
).So:
# Uncomment the line %saptb ALL=(root)SU_ROOT
sudo sed -Ei 's/^#(%saptb.*SU_ROOT.*)/\1/' /etc/sudoers
You can use the following script to comment/uncomment by running sudo ./toggle.sh
#!/bin/bash
# Is the line already commented out
grep -q '#%saptb ALL=(root)SU_ROOT' /etc/sudoers
if [ $? -eq 0 ]; then
# Uncomment the line %saptb ALL=(root)SU_ROOT
sed -Ei 's/^#(%saptb.*SU_ROOT.*)/\1/' /etc/sudoers
else
# Comment out the line %saptb ALL=(root)SU_ROOT
sed -Ei 's/^(%saptb.*SU_ROOT.*)/#\1/' /etc/sudoers
fi
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.