shaggy999
April 25th, 2010, 07:04 PM
I am trying to figure out how to edit some configuration files from a script. As an example I'll use the /etc/ssh/sshd_config file. It has the following lines I would like to change:
Port 22
PermitRootLogin yes
#PasswordAuthentication yes
My current method looks like this:
sed -i 's/Port 22/Port 2222/g' /etc/ssh/sshd_config
sed -i 's/PermitRootLogin yes/PermitRootLogin no/g' /etc/ssh/sshd_config
sed -i 's/#PasswordAuthentication yes/PasswordAuthentication no/g' /etc/ssh/sshd_config
The thing I don't like about this method is that I have to search for the entire line followed by replacing the entire line and it has to match perfectly. For example, what if the Port value was set to 23 or I forgot there was a # sign on the PasswordAuthentication line? That value would then not be changed. I would like to be able to say something like "find the line that starts with 'Port', then change the entire line to 'Port 2222'" -- that seems more flexible to me.
What say you?
Port 22
PermitRootLogin yes
#PasswordAuthentication yes
My current method looks like this:
sed -i 's/Port 22/Port 2222/g' /etc/ssh/sshd_config
sed -i 's/PermitRootLogin yes/PermitRootLogin no/g' /etc/ssh/sshd_config
sed -i 's/#PasswordAuthentication yes/PasswordAuthentication no/g' /etc/ssh/sshd_config
The thing I don't like about this method is that I have to search for the entire line followed by replacing the entire line and it has to match perfectly. For example, what if the Port value was set to 23 or I forgot there was a # sign on the PasswordAuthentication line? That value would then not be changed. I would like to be able to say something like "find the line that starts with 'Port', then change the entire line to 'Port 2222'" -- that seems more flexible to me.
What say you?