0

I want to edit the sshd_config file using bash script. However, I keep getting an error. I would appreciate any help given.

This is my script:

if ! grep "IgnoreRhosts" "/etc/ssh/sshd_config"; then
    sed -i /etc/ssh/sshd_config -e '/# Don't read the user's ~/.rhosts and ~/.shosts files/a IgnoreRhosts yes'
    printf "\e[32m IGNORERHOSTS YES ADDED\e[0m\n"
elif grep "#IgnoreRhosts yes" "/etc/ssh/sshd_config"; then
    sed -i 's/^#IgnoreRhosts yes/IgnoreRhosts yes/' /etc/ssh/sshd_config
    printf "\e[32mSUCCESSFULLY CHANGED\e[0m\n"
else
    printf "\e[32mNO CHANGES NEEDED\e[0m\n"
fi

This is the error I got:

sed: -e expression #1, char 7: unterminated address regex

Script error line:

 sed -i /etc/ssh/sshd_config -e '/# Don't read the user's ~/.rhosts and ~/.shosts files/a IgnoreRhosts yes'
renek
  • 1

1 Answers1

0

This one should work:

sed -i /etc/ssh/sshd_config -e "\|# Don't read the user's ~/.rhosts and ~/.shosts files|a IgnoreRhosts yes"

Explanation: In this script I'm using | as a delimiter to make it more readable. This delimiter is defined by first \ (more info here)

Then, I'm using double quotes instead of single quotes to avoid escaping apostrophes in words Don't and user's

alexK
  • 963
  • 1
  • 7
  • 17