ufwctl
Rust library for managing UFW firewall rules through the official ufw CLI.
Linux-only
This crate targets Linux exclusively because UFW (Uncomplicated Firewall) is a Linux-specific tool. The crate will fail to compile on non-Linux targets with a clear compile-time error.
No external dependencies
This library uses only the Rust standard library. No external crates are required.
UFW CLI wrapper
ufwctl wraps the ufw command-line tool via std::process::Command. It does not read or
write /etc/ufw/* files directly. Every operation delegates to the ufw binary.
Why no install / uninstall?
Package management (apt, pacman, dnf, etc.) is distro-specific and outside the scope of a
core firewall library. Use your system package manager to install or remove ufw itself.
Examples
Port rules
use Firewall;
let fw = new;
// Allow TCP on port 22
fw.port.allow.tcp.apply?;
// Deny UDP on port 8080
fw.port.deny.udp.apply?;
// Allow both TCP and UDP on port 5050
fw.port.allow.both.apply?;
// Delete a rule
fw.port.delete.both.apply?;
// Check status
let status = fw.port.status.tcp?;
IPv4 rules
fw.ipv4?.allow.both.apply?;
fw.ipv4?.deny.tcp.apply?;
fw.ipv4?.delete.both.apply?;
let status = fw.ipv4?.status.tcp?;
IPv6 rules
fw.ipv6?.allow.both.apply?;
fw.ipv6?.deny.tcp.apply?;
let status = fw.ipv6?.status.tcp?;
Delete is idempotent
delete().apply() returns Result<bool, UfwError>. It attempts every variant of the rule
(allow/deny × tcp/udp × bare/proto form) and succeeds as long as at least one form actually
deleted a rule.
let deleted = fw.port.delete.tcp.apply?;
if deleted else
Ok(true) means at least one matching rule was found and removed.
Ok(false) means no matching rule existed — safe to call repeatedly.
Error handling
use Firewall;
let fw = new;
match fw.enable
Safety note
This library modifies firewall rules by running sudo ufw ... commands. Depending on your
configuration, this may require root privileges or passwordless sudo for the ufw command.
Use with caution in production environments.