I'm writing a bash script to provision my server using vagrant. my provision script is an external bash script.
Currently i'm trying to set the banner inside my ssh_config file but it doesn't seem to work.
I was looking on the internet on how to replace a string within a file: the default in ssh_config is '#Banner none' which needs 'Banner /etc/issue.net'.
My problem is that I always get a permission denied on /etc/issue.net, but i'm thinking that it sees the string as a file to access rather than the string to replace.
my command
text_to_find="#Banner none"
text_to_replace= '"Banner /etc/issue.net"'
sed -i 's/$text_to_find/$text_to_replace/g' /etc/ssh/sshd_config
First I tried without the single quotes, I tried to escape the string but none of the things I tried was helpfull.
Your second assignment is not an assignment; with a space after the =
, it tries to run a command named "Banner /etc/issue.net"
, which probably doesn't exist, with the variable set to the empty string inside that command's environment.
Also, your quotes in the sed
command are wrong, so once you do get the assignment right, you'll be asking sed
to look for the literal text $text_to_find
and replace it with $text_to_replace
, instead of the strings you stored in those variables.
Finally, you can't use /
as the delimiter for the sed s
command if there are /
s inside the search or replacement text. Once you fix the assignment and quotes and get the actual text you want into the sed, what you'll wind up with is this:
sed -i "s/$text_to_find/$text_to_replace/g"
which expands into this:
sed -i 's/#Banner none/"#Banner /etc/issue.net"/g'
With five /
s in it. How is sed
supposed to know which ones separate the arguments to s
, and which ones are meant to be part of those arguments? Fortunately you don't have to use /
as the delimiter; you can pick any character. For example:
sed -i "s|$text_to_find|$text_to_replace|g"
You could alternatively use some fancy expansion shenanigans to insert a backslash in front of all the slashes:
sed -i "s/${text_to_find//\//\\\/}/${text_to_replace//\//\\\/}/g"
but who wants leaning toothpicks all over their shell script?
Also, your assignment to text_to_replace
includes literal quotation marks which will wind up in the file; make sure that's what you want.