Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to configure the /etc/fail2ban/jail.local config with an bash script. I was thinking to use sed and regular expression to change the sections I need.

sed -i "s/^bantime  = 600/bantime  = $1/" /etc/fail2ban/jail.local
sed -i "s/^destemail = root@localhost/destemail = $2/" /etc/fail2ban/jail.local
sed -i "s/^action = %(action_)s/action = %(action_mwl)s/" /etc/fail2ban/jail.local

As you can see for bantime, destemail and action I'm able to do so. Only if it matches the exactly. I could be that the bantime, or the destemail is changed, then the script won't work. What can i use to make after the = sign flexable. For example if bantime is not 600 but 700 it will also change using the bash script.

Also there is an section to configure SSH and SSHD:


enabled = true
port = ssh
filter = sshd
logpath = /var/log/auth.log
maxretry = 6


enabled = false 
port = ssh
filter = sshd
logpath = /var/log/dropbear
maxretry = 6


enabled = false
port = ssh
filter = sshd-ddos
logpath = /var/log/auth.log
maxretry = 6

I also want to specify the port I use for SSH or enable SSHD with sed and regular expression. But as you can see when i match on enabled I will change all jails or if I match on port I will change all ports in the config. This is not what I want.

Can anybody assist me with an way to configur Fail2Ban with an script and a flexible way?

Many thanks, Raoul

share|improve this question
up vote 0 down vote accepted

For the single-line edits you could use

sed -e "s/^bantime = .*/bantime = $1/"

or, more resiliently

sed -re "s/^bantime[[:blank:]]*=.*/bantime = $1/"

For the multi-line problem, use line selectors to delimit your edits

sed -re "/^\[ssh\]$/,/^\[/s/port[[:blank:]]*=.*/port = $1/"
share|improve this answer
Thank you very much Zwets! Works Like a charme. I had to make one change to get it working and that is changing -re in to -i. – yoRaoul Mar 5 '13 at 23:25

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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