Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I am attempting to write a script that will comment out the following line in /etc/sudoers every time I launch a new EC2 instance:

Defaults requiretty

I'm assuming the best way to do this is with an inline sed search and replace or search and insert. The issue is there could be 1+ spaces/tabs between Defaults and requiretty also. What's an efficient sed solution?

share|improve this question

2 Answers 2

up vote 4 down vote accepted

Jay's answer will work almost unchanged with sed:

sed -i 's/^Defaults[ \t]+requiretty/# Defaults requiretty/g' /etc/ssh/sshd_config
share|improve this answer

I'd use Perl, personally

perl -pi -e 's/(^Defaults\s+requiretty)/# $1/g' /etc/ssh/sshd_config

(Line must start with Defaults to match it. If you don't want that, just remove the ^. The \s+ will catch any number of spaces between Defaults and requiretty.)

share|improve this answer
1  
You can remove the code dup with () and $1 –  Clayton Stanley Jun 19 '12 at 3:19
    
Excellent point, thanks. I have revised the answer accordingly. –  Jay Jun 19 '12 at 11:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.