sed replace #PermitRootLogin yes

I can't seem to get this to work. I am trying this:

sed -e 's/#PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config

I am not super familiar with sed. Any help?

Hi.

This won't update the file, if your sed supports it, use sed -i

Otherwise you can use ed.

ed /etc/ssh/sshd_config << !
,s/#PermitRootLogin yes/PermitRootLogin no/
w
q
!

(someone will also, probably give you a nice Perl one-liner...)

Is commenting out PermitRootLogin yes not the same as an uncommented PermitRootLogin no anyway?

No, it is not the same because that is the default in SSHD_config

Also, I know that command won't update the file, but it doesn't even replace it in the output. I would end up using -i to actually replace it, but I am looking at the output.

I need it to uncomment and then replace the yes with a no.

Then perhaps the expression you are using does not match the content in the file.

It does matchup, I just dont' think the sed command is working right.

Here part of the file:

#LoginGraceTime 2m
#PermitRootLogin yes
#StrictModes yes
#MaxAuthTries 6

Your ed command did work, but I'd liek to get it working with sed so I can add it to a bash script if possible.

I know that in sed you can have comments using #. My sed supports this too, but it doesn't cause any problem with the substitution.

Does it make any difference if you escape the # (i.e. like \#)?

Otherwise, perhaps your input file has a hidden or control character somewhere?

Sorry for the delayed response. I will try the escape. In theory my command up in the first post should work right?

I am running RHEL 5.3 so it should have a recent version of sed. I will try the results tomorrow!

Hi Mark.

There is nothing wrong with your original code. There's nothing fancy about it!

Good luck!

All, I just ran this using a \# and it worked, but without the escape it didn't work.

I also ran it without the \ escape and it worked. no idea what I was seeing before that could have caused the issue.

sed -e 's/#PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config