Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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
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


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.