Bash – sed: how to insert a comment at the beginning of a matching line

bashscriptingsed

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?

Best Answer

Jay's answer will work almost unchanged with sed:

sed -i 's/^Defaults[ \t]+requiretty/# Defaults requiretty/g' /etc/ssh/sshd_config