Regular expressions and sed can help with things like this.
sed -re 's/^(PasswordAuthentication)([[:space:]]+)no/\1\2yes/' -i.`date -I` /etc/ssh/sshd_config
On my Debian system, this will toggle
PasswordAuthentication no to
PasswordAuthentication yes, regardless of the number of spaces between key and value.
It will replace the option in the file (in-place editing) while creating a backup of the original file, with a suffix named by the date (
Switch places for yes and no to toggle the other way.
The expression for removing a leading
# is something like this:
sed -re 's/^(\#)(PasswordAuthentication)([[:space:]]+)(.*)/\2\3\4/'
-and for inserting a leading
sed -re 's/^(PasswordAuthentication)([[:space:]]+)(.*)/#\1\2\3/'
To toggle a line with an optional
#-sign in front (Thanks Barlop):
sed -re 's/^(\#?)(PasswordAuthentication)([[:space:]]+)no/\2\3yes/'
In all these expressions you can change
PasswordAuthentication to any other option you want to change - probably even make it a key in a shell script, and make a "sshd option toggle tool".
Sed and regex is all fun and games, but I'm sure Valentin is right about configuration management is the way to go if you have lots of systems. Personally I just use etckeeper/bazaar on a couple of systems that very rarely change - it's not configuration management, but it gives me versioning, so I can get back a working config after I screwed up with regex and
sed -i. :)