0

Let's say I want to edit the /etc/ssh/sshd_config file in RHEL 7 to secure our ssh configuration.

I want to replace let's take the Ciphers line for example, keeping the original in place and commenting it out. I also want to be able to key off of the smallest part of the string as possible to keep from having Red Hat break it every time they update the rpm / install iso.

Ciphers aes128-ctr,aes192-ctr,aes256-ctr,aes128-cbc,3des-cbc,aes192-cbc,aes256-cbc

should become...

#Ciphers aes128-ctr,aes192-ctr,aes256-ctr,aes128-cbc,3des-cbc,aes192-cbc,aes256-cbc
Ciphers abc-123,def-456,ghi-789 etc...

I have tried to do a find / add newline in a test file where the filename is testfile, and the contents are This is my test file. I tried adding No it's not as a new line and have miserably failed.

sed -i '/This is my test file./aNo it's not.' testfile

How would I go about that and more importantly, what are the key concepts behind it?

  • Are those the only places in the file where Ciphers appears? – Nasir Riley Jun 3 at 18:43
0

I have tried with below sed command and it worked like charm

command

sed -i '/^Ciphers aes128-ctr,aes192-ctr,aes256-ctr,aes128-cbc,3des-cbc,aes192-cbc,aes256-c/s/.*/#&\nCiphers abc-123,def-456,ghi-789/g' filename

output

#Ciphers aes128-ctr,aes192-ctr,aes256-ctr,aes128-cbc,3des-cbc,aes192-cbc,aes256-cbc
Ciphers abc-123,def-456,ghi-789
0
sed -i -n 'p; s/^Ciphers/#&/p' /etc/ssh/sshd_config

-i in place

-n prints nothing unless explicitly printed

/p at the end of the search string forces the print, effectively overriding -n on the matched string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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