Kendi Balazs Kendi Balazs - 1 year ago 160
Linux Question

Modify /etc/sudoers with sed

I'm trying to write a sed, which appends the line Defaults:user !requiretty after the line Defaults requiretty in /etc/sudoers. I tried the following command:

sudo sed -i '/Defaults requiretty/a Defaults:user !requiretty' /etc/sudoers

This is working properly, but only if there are 4 spaces between 'Defaults' and 'requiretty'. I want to modify it in order to work with any number of spaces, so I tried the following:

sudo sed -i '/Defaults\s+requiretty/a Defaults:user !requiretty' /etc/sudoers

I checked the pattern on regexr and it was ok, but still the command does not insert the required line.

Thanks in advance.

Answer Source

try this;

sed '/Defaults.\s\s.requiretty/a Defaults:user !requiretty' /etc/sudoers
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download