When automating server provisioning with bash scripts, running sudo ufw enable
presents a common obstacle - the interactive prompt asking for confirmation. This breaks the automation flow and requires manual intervention.
UFW (Uncomplicated Firewall) is designed with safety in mind. The confirmation prompt appears because enabling the firewall could potentially lock you out of the system if not configured properly first.
The standard output looks like:
Command may disrupt existing ssh connections. Proceed with operation (y|n)?
Method 1: Using yes Command
The simplest solution is to pipe the output of yes
command:
yes | sudo ufw enable
Method 2: Using printf
For more control, use printf to send exactly what you need:
printf 'y\\n' | sudo ufw enable
Method 3: Expect Script
For complex interactive prompts, use expect:
#!/usr/bin/expect
spawn sudo ufw enable
expect "Proceed with operation"
send "y\\r"
expect eof
Method 4: UFW Non-Interactive Mode
Some versions support a non-interactive flag:
sudo ufw --force enable
Here's a full bash script example for setting up UFW rules automatically:
#!/bin/bash
# Allow SSH first to prevent lockout
sudo ufw allow 22/tcp
# Auto-confirm the enable command
printf 'y\\n' | sudo ufw enable
# Add additional rules
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw default deny incoming
sudo ufw default allow outgoing
Always ensure you have:
- Allowed SSH access before enabling
- Tested rules in non-production first
- Documented your firewall configuration
If automation fails:
# Check UFW status
sudo ufw status verbose
# View logs
sudo journalctl -u ufw -f
When automating server provisioning, especially with infrastructure-as-code tools like Ansible or plain Bash scripts, encountering interactive prompts can break your automation flow. The ufw enable
command is particularly problematic because it requires manual confirmation:
sudo ufw enable
Command may disrupt existing ssh connections. Proceed with operation (y|n)?
Here are three reliable methods to bypass the interactive prompt:
Method 1: Using yes Command
The simplest approach uses the yes
utility to pipe 'y' automatically:
yes | sudo ufw enable
Method 2: --force Parameter
For newer UFW versions (17.04+), use the non-interactive flag:
sudo ufw --force enable
Method 3: Expect Script
For complex automation scenarios requiring multiple interactions:
#!/usr/bin/expect
spawn sudo ufw enable
expect "Proceed with operation" {send "y\r"}
expect eof
For robust deployment scripts, combine UFW automation with error handling:
#!/bin/bash
enable_ufw() {
if ufw status | grep -q "inactive"; then
echo "Enabling UFW firewall..."
if yes | sudo ufw enable 2>&1 | grep -v "Skipping"; then
echo "UFW enabled successfully"
return 0
else
echo "Failed to enable UFW" >&2
return 1
fi
else
echo "UFW already active"
return 0
fi
}
enable_ufw || exit 1
Sometimes the prompt might still appear due to:
- Custom UFW configurations
- Modified sudoers policies
- Non-standard UFW installations
For these cases, pre-set the default policy:
sudo ufw default deny incoming
sudo ufw default allow outgoing
The order matters - setting defaults first often prevents the confirmation prompt.
Always verify SSH access rules before enabling UFW in automation:
sudo ufw allow OpenSSH
sudo ufw limit 22/tcp # Alternative rate-limiting approach
Test connectivity in a separate terminal before running automation scripts that might lock you out of the server.