The Uncomplicated Firewall (UFW) is a user-friendly tool for managing firewall rules in Linux. 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.
In this tutorial you will learn:
- How to back up UFW rules and configurations
- How to restore UFW rules and configurations

| Category | Requirements, Conventions or Software Version Used |
|---|---|
| System | Linux-based system with UFW installed |
| Software | UFW (Uncomplicated Firewall) |
| Other | Basic knowledge of Linux command line |
| Conventions | # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command$ – requires given linux commands to be executed as a regular non-privileged user |
How to Backup and Restore UFW on Linux
Follow these steps to create a reliable backup of your UFW configuration and restore it when needed. Each step is explained in detail to ensure clarity.
Backup UFW
- Backup UFW Rules: Create a simple backup of the active UFW rules.
# sudo ufw status > ufw-rules-backup.txt
This command uses ufw status to export 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. - Backup Full UFW Configuration: Save the entire UFW configuration directory.
# sudo tar -cvzf ufw-backup.tar.gz /etc/ufw
The above command compresses the UFW configuration files located in
/etc/ufwinto a tarball namedufw-backup.tar.gz. This is a complete backup that can be restored directly.
Restore UFW
- 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.
- Restore UFW Configuration: Extract the backup tarball to the appropriate location.
$ sudo tar -xvzf ufw-backup.tar.gz -C /
This restores the configuration files to their original location. The
-C /option ensures the files are extracted to the root directory. - Verify File Permissions: Ensure the restored files have the correct ownership and permissions.
$ sudo chmod -R 600 /etc/ufw $ sudo chown -R root:root /etc/ufw
These commands set secure permissions on the restored files, ensuring that only the root user has access.
- Enable UFW: Reactivate UFW after restoring the configuration.
$ sudo ufw enable
This command enables UFW and applies the restored rules to the system.
- Verify Restored Rules: Check the restored rules to ensure they match the original configuration.
$ sudo ufw status
This displays the current active rules, allowing you to confirm that the restoration was successful.
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.