LinuxCommandLibrary
GitHubF-DroidGoogle Play Store

ufw-insert

Insert an Uncomplicated Firewall rule at a numbered position

TLDR

List rules with numbers
$ sudo ufw status numbered
copy
Insert a deny rule as rule number 1
$ sudo ufw insert 1 deny from [source_address]
copy
Insert an allow for a port and protocol at a chosen position
$ sudo ufw insert [3] allow [22]/tcp
copy
Insert a full-syntax rule at a position
$ sudo ufw insert 3 deny to any port 22 from 10.0.0.135 proto tcp
copy
Insert a rate limit rule
$ sudo ufw insert [2] limit [22]/tcp
copy
Preview an insertion without applying it
$ sudo ufw --dry-run insert [3] deny from [source_address]
copy

SYNOPSIS

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

DESCRIPTION

ufw insert adds an Uncomplicated Firewall rule at a specific position instead of appending it. It is a ufw subcommand, not a separate binary.The first matching rule wins, so position is the point of the command. With four rules already loaded, `ufw insert 3 deny to any port 22 from 10.0.0.135 proto tcp` makes the new rule number 3 and shifts the previous third and fourth rules down.prepend is the form that does not take a number: it places the rule ahead of every existing rule of the same IP version. route insert uses the same numbering for traffic forwarded through the host.

PARAMETERS

insert NUM

Place the new user rule at that 1-based position. Later rules shift down. Read positions from `ufw status numbered`
route insert NUM
Same positioning for a forwarded rule. See ufw route
allow / deny / reject / limit
Action of the inserted rule. The rest of the line is normal ufw rule syntax: a port, an application profile, or the full from / to / port / proto / on form
comment 'text'
Attach a comment to the new rule. insert 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. Check `ufw status numbered` immediately before inserting. Deletes and earlier inserts renumber the list. Inserting a rule that blocks the current SSH session can lock you out. Prefer --dry-run on a remote host.When IPv6 is enabled, a generic rule such as `ufw allow 22/tcp` creates paired IPv4 and IPv6 entries. insert adds a rule; it does not rewrite rules already in the list.

HISTORY

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

SEE ALSO

RESOURCES

Copied to clipboard
Kai