Here in this article you came to know how to backup and restore UFW.

What is UFW?

UFW (Uncomplicated Firewall) is a user-friendly command-line interface for managing firewall rules on Linux systems. It acts as a front-end for the iptables framework, which is powerful but can be complex for new users. UFW simplifies the process of setting up and managing firewall rules, making it ideal for users who need straightforward firewall management.

Whether you are migrating servers, need to reinstall your operating system, or simply want to safeguard your configurations, backing up and restoring UFW is an essential skill. This tutorial provides step-by-step instructions for ensuring your firewall rules are safely backed up and restored when needed.

Backing up and restoring UFW (Uncomplicated Firewall) rules is a straightforward process. UFW stores its rules in a configuration file, so saving and restoring these rules involves working with these files. Here’s how you can do it:

Backup and Restore UFW

Backup UFW Rules

Backup UFW configuration files: UFW stores its main configuration and rules in the following files:

  • /etc/ufw/ufw.conf: Main UFW configuration.
  • /etc/ufw/before.rules and /etc/ufw/after.rules: Pre- and post-rule sets.
  • /etc/ufw/user.rules and /etc/ufw/user6.rules: Custom IPv4 and IPv6 rules.

Backup UFW

Backup UFW Rules: Create a simple backup of the active UFW rules.

# sudo ufw status > ufw-rules-backup.txt

This command exports the current UFW status and rules into a text file named ufw-rules-backup.txt. While this backup is for reference only and cannot be directly restored, it helps document your rules.

To back up all these files, run:

sudo cp -r /etc/ufw /path/to/backup/directory

Backup active rules: To save the active rules, list them in a readable format:

sudo ufw status numbered > ufw-rules-backup.txt

This saves the current rules in a text format for reference.

Restore UFW Rules

Stop UFW Before Restoring: Temporarily disable UFW to avoid conflicts during restoration

$ sudo ufw disable

Stopping UFW ensures that restoring configurations does not cause issues with active firewall rules.

  1. Restore UFW configuration files: Copy the backed-up files back to their original locations:
sudo cp -r /path/to/backup/directory/ufw /etc/

Make sure the file permissions are correct:

sudo chmod -R 644 /etc/ufw/*

Restore rules from a backup file: If you backed up using ufw status numbered:

  • Edit the file to ensure the rules are in the proper ufw syntax.
  • Reapply the rules one by one using:
sudo ufw allow <rule>

Or use the deny or reject commands as needed.

Reload UFW to apply restored settings: After restoring the configuration files, reload UFW to apply the changes:

sudo ufw reload

Automate Backup and Restore

You can use a script to automate the backup or restore process. For example:

Backup Script:

#!/bin/bash
backup_dir="/path/to/backup"
sudo mkdir -p "$backup_dir"
sudo cp -r /etc/ufw "$backup_dir"
sudo ufw status numbered > "$backup_dir/ufw-rules-backup.txt"
echo "UFW rules and configuration backed up to $backup_dir"

Restore Script:

#!/bin/bash
backup_dir="/path/to/backup"
sudo cp -r "$backup_dir/ufw" /etc/
sudo ufw reload
echo "UFW rules and configuration restored from $backup_dir"

Notes:

  • Ensure the backup location is secure to prevent unauthorized access to firewall rules.
  • Test the restore process on a non-production system to ensure correctness.
  • For advanced scenarios, consider using version control for /etc/ufw.

Conclusion

Backing up and restoring UFW is a straightforward but crucial process for safeguarding your firewall configurations. By following the steps in this tutorial, you can ensure your UFW rules are always protected and easily recoverable. Regular backups are a good practice, especially when making significant changes to your firewall or planning system migrations.