LinuxCommandLibrary
GitHubF-DroidGoogle Play Store

ufw-prepend

Add an Uncomplicated Firewall rule ahead of existing rules

TLDR

Deny a source address ahead of the current rules
$ sudo ufw prepend deny from [source_address]
copy
Prepend a deny for a subnet
$ sudo ufw prepend deny from 192.168.0.0/24
copy
Reject traffic from a source to one port
$ sudo ufw prepend reject from [source_address] to any port [25]
copy
Prepend an allow for a port and protocol
$ sudo ufw prepend allow [22]/tcp
copy
Prepend a routed rule
$ sudo ufw route prepend deny to [destination_address]
copy
Preview a prepend without applying it
$ sudo ufw --dry-run prepend deny from [source_address]
copy

SYNOPSIS

ufw [--dry-run] prepend allow|deny|reject|limit [rule]ufw [--dry-run] route prepend allow|deny|reject|limit [rule]

DESCRIPTION

ufw prepend adds a rule at the front of the matching ruleset. It is a ufw subcommand, not a separate binary.Unlike ufw insert, prepend does not take a position. An IPv4 rule is placed before every other IPv4 rule. An IPv6 rule is placed before every other IPv6 rule. That is the form used by dynamic firewalls and intrusion-prevention hooks, which must push a block ahead of rules already loaded without looking up a number.The rule body is the same syntax as ufw allow, ufw deny, ufw reject, and ufw limit, including the full from / to / port / proto form. ufw route prepend targets traffic forwarded through the host rather than traffic destined to it.

PARAMETERS

prepend

Insert the rule before every existing user rule of the same IP version. No rule number is required
route prepend
Same placement for a forwarded rule
allow / deny / reject / limit
Action of the new rule. The rest of the line is normal ufw rule syntax
comment 'text'
Attach a comment to the new rule. prepend cannot change the comment on a rule that already exists
--dry-run
Show what would change without applying it

INSTALL

sudo dnf install ufw
copy
sudo pacman -S ufw
copy
sudo apk add ufw
copy
sudo zypper install ufw
copy

CAVEATS

Requires root or sudo. A broad prepended allow can shadow a later, more specific deny, because the first match wins. A prepended deny of the current SSH source can lock you out. Prefer --dry-run.IPv4 and IPv6 lists are ordered separately, so the new rule is not always number 1 in the combined `ufw status numbered` listing.

HISTORY

Part of ufw (Uncomplicated Firewall), the Ubuntu-originated frontend for iptables and nftables.

SEE ALSO

RESOURCES

Copied to clipboard
Kai