Linux Commands Examples

A great documentation place for Linux commands


program for managing a netfilter firewall

see also : iptables - ip6tables - iptables-restore - ip6tables-restore - sysctl

add an example, a script, a trick and tips

: email address (won't be displayed)
: name

Step 2

Thanks for this example ! - It will be moderated and published shortly.

Feel free to post other examples
Oops ! There is a tiny cockup. A damn 404 cockup. Please contact the loosy team who maintains and develops this wonderful site by clicking in the mighty feedback button on the side of the page. Say what happened. Thanks!



Delete a previously added rule in ufw

If you just want to delete the rule, the syntax is

sudo ufw delete allow MyApp



Deny all access to port 53:

ufw deny 53

Allow all access to tcp port 80:

ufw allow 80/tcp

Allow all access from RFC1918 networks to this host:

ufw allow from
ufw allow from
ufw allow from

Deny access to udp port 514 from host

ufw deny proto udp from to any port 514

Allow access to udp port 5469 from port 5469:

ufw allow proto udp from port 5469 to port 5469


Open up firewall automatically to anybody who has successfully connected via SSH

You can put commands in ~/.bashrc, anything in there is executed each time a user logs in.

For your commands to only run when logging in via ssh (and not when logging in physically), you can test for the presence of the SSH_CONNECTION environment variable.


UFW rules for specific host / dynamic dns

I found a very useful script that creates rules dynamically for specific hosts in this blog. The script needs to be run with cron, so it can lookup a hostname and add/delete rules in case the IP changed.



add_rule() {
  local proto=$1
  local port=$2
  local ip=$3
  local regex="${port}\/${proto}.*ALLOW.*IN.*${ip}"
  local rule=$(ufw status numbered | grep $regex)
  if [ -z "$rule" ]; then
      ufw allow proto ${proto} from ${ip} to any port ${port}
      echo "rule already exists. nothing to do."

delete_rule() {
  local proto=$1
  local port=$2
  local ip=$3
  local regex="${port}\/${proto}.*ALLOW.*IN.*${ip}"
  local rule=$(ufw status numbered | grep $regex)
  if [ -n "$rule" ]; then
      ufw delete allow proto ${proto} from ${ip} to any port ${port}
      echo "rule does not exist. nothing to do."

sed '/^[[:space:]]*$/d' ${HOSTS_ALLOW} | sed '/^[[:space:]]*#/d' | while read line
    proto=$(echo ${line} | cut -d: -f1)
    port=$(echo ${line} | cut -d: -f2)
    host=$(echo ${line} | cut -d: -f3)

    if [ -f ${IPS_ALLOW} ]; then
      old_ip=$(cat ${IPS_ALLOW} | grep ${host} | cut -d: -f2)

    ip=$(dig +short $host | tail -n 1)

    if [ -z ${ip} ]; then
        if [ -n "${old_ip}" ]; then
            delete_rule $proto $port $old_ip
        echo "Failed to resolve the ip address of ${host}." 1>&2
        exit 1

    if [ -n "${old_ip}" ]; then
        if [ ${ip} != ${old_ip} ]; then
            delete_rule $proto $port $old_ip
    add_rule $proto $port $ip
    if [ -f ${IPS_ALLOW} ]; then
      sed -i.bak /^${host}*/d ${IPS_ALLOW}
    echo "${host}:${ip}" >> ${IPS_ALLOW}

The content of /etc/ufw-dynamic-hosts.allow could look like this:

and a crontab entry for executing the script every five minutes could look like this:

*/5 * * * * /usr/local/sbin/ufw-dynamic-host-update > /dev/null

Opening firewall to incoming port 443

The packets you are seeing are response packets:


Note the SPT - source port - is 443. When you are accessing a remote https site, you are sending packets with a DPT - destination port - of 443, any replies you get from that site will originate from their IP and from source port 443.

By far and away the most common reason for seeing these packets is after you close a session to the remote site, and your firewall observes this and clears the session from its table of active connections. Sometime due to timing, or poor implementation of TCP at the remote end, or duplicate packets, or loadbalancers sending the same reply, you can get extra packets for a session after the close sequence has completed.

Your firewall doesn't have an active session for these packets to match any more, and so they are dropped, and logged as you are seeing them.

They can be safely ignored. Do not adjust your firewall to permit these packets, as it opens unnecessary holes in your security.


Ubuntu Linux UFW Firewall, Local Intranet Access

I would guess something like: sudo ufw allow from to port 80


This program is for managing a Linux firewall and aims to provide an easy to use interface for the user.



show program’s version number and exit

-h, --help

show help message and exit


don’t modify anything, just show the changes


reloads firewall and enables firewall on boot.


unloads firewall and disables firewall on boot


reloads firewall

default allow|deny|reject DIRECTION

change the default policy for traffic going DIRECTION, where DIRECTION is one of incoming or outgoing. Note that existing rules will have to be migrated manually when changing the default policy. See RULE SYNTAX for more on deny and reject.

logging on|off|LEVEL

toggle logging. Logged packets use the LOG_KERN syslog facility. Systems configured for rsyslog support may also log to /var/log/ufw.log. Specifying a LEVEL turns logging on for the specified LEVEL. The default log level is ’low’. See LOGGING for details.


Disables and resets firewall to installation defaults. Can also give the --force option to perform the reset without confirmation.


show status of firewall and ufw managed rules. Use status verbose for extra information. In the status output, ’Anywhere’ is synonymous with ’any’ and ’’.


display information about the running firewall. See REPORTS

allow ARGS

add allow rule. See RULE SYNTAX

deny ARGS

add deny rule. See RULE SYNTAX

reject ARGS

add reject rule. See RULE SYNTAX

limit ARGS

add limit rule. Currently only IPv4 is supported. See RULE SYNTAX

delete RULE|NUM

deletes the corresponding RULE

insert NUM RULE

insert the corresponding RULE as rule number NUM

application integration

ufw supports application integration by reading profiles located in /etc/ufw/applications.d. To list the names of application profiles known to ufw, use:

ufw app list

Users can specify an application name when adding a rule (quoting any profile names with spaces). For example, when using the simple syntax, users can use:

ufw allow <name>

Or for the extended syntax:

ufw allow from to any app <name>

You should not specify the protocol with either syntax, and with the extended syntax, use app in place of the port clause.

Details on the firewall profile for a given application can be seen with:

ufw app info <name>

where ’<name>’ is one of the applications seen with the app list command. User’s may also specify all to see the profiles for all known applications.

After creating or editing an application profile, user’s can run:

ufw app update <name>

This command will automatically update the firewall with updated profile information. If specify ’all’ for name, then all the profiles will be updated. To update a profile and add a new rule to the firewall automatically, user’s can run:

ufw app update --add-new <name>

The behavior of the update --add-new command can be configured using:

ufw app default <policy>

The default application policy is skip, which means that the update --add-new command will do nothing. Users may also specify a policy of allow or deny so the update --add-new command may automatically update the firewall. WARNING: it may be a security to risk to use a default allow policy for application profiles. Carefully consider the security ramifications before using a default allow policy.


ufw supports multiple logging levels. ufw defaults to a loglevel of ’low’ when a loglevel is not specified. Users may specify a loglevel with:

ufw logging LEVEL

LEVEL may be ’off’, ’low’, ’medium’, ’high’ and full. Log levels are defined as:


disables ufw managed logging


logs all blocked packets not matching the default policy (with rate limiting), as well as packets matching logged rules


log level low, plus all allowed packets not matching the default policy, all INVALID packets, and all new connections. All logging is done with rate limiting.


log level medium (without rate limiting), plus all packets with rate limiting


log level high without rate limiting

Loglevels above medium generate a lot of logging output, and may quickly fill up your disk. Loglevel medium may generate a lot of logging output on a busy system.

Specifying ’on’ simply enables logging at log level ’low’ if logging is currently not enabled.


On installation, ufw is disabled with a default incoming policy of deny and a default outgoing policy of allow, with stateful tracking for NEW connections.

Rule ordering is important and the first match wins. Therefore when adding rules, add the more specific rules first with more general rules later.

ufw is not intended to provide complete firewall functionality via its command interface, but instead provides an easy way to add or remove simple rules. It is currently mainly used for host-based firewalls.

The status command shows basic information about the state of the firewall, as well as rules managed via the ufw command. It does not show rules from the rules files in /etc/ufw. To see the complete state of the firewall, users can ufw show raw. This displays the filter, nat, mangle and raw tables using:

iptables -n -L -v -x -t <table>
ip6tables -n -L -v -x -t <table>

See the iptables and ip6tables documentation for more details.

If the default policy is set to REJECT, ufw may interfere with rules added outside of the ufw framework. See README for details.

IPV6 is allowed by default. To change this behavior to only accept IPv6 traffic on the loopback interface, set IPV6 to ’no’ in /etc/default/ufw and reload ufw. When IPv6 is enabled, you may specify rules in the same way as for IPv4 rules, and they will be displayed with ufw status. Rules that match both IPv4 and IPv6 addresses apply to both IP versions. For example, when IPv6 is enabled, the following rule will allow access to port 22 for both IPv4 and IPv6 traffic:

ufw allow 22

IPv6 over IPv4 tunnels and 6to4 are supported by using the ’ipv6’ protocol (’41’). This protocol can only be used with the full syntax. For example:

ufw allow to proto ipv6
ufw allow to from proto ipv6

IPSec is supported by using the ’esp’ (’50’) and ’ah’ (’51’) protocols. These protocols can only be used with the full syntax. For example:

ufw allow to proto esp
ufw allow to from proto esp
ufw allow to proto ah
ufw allow to from proto ah

In addition to the command-line interface, ufw also provides a framework which allows administrators to take full advantage of netfilter. See the ufw-framework manual page for more information.

remote management

When running ufw enable or starting ufw via its initscript, ufw will flush its chains. This is required so ufw can maintain a consistent state, but it may drop existing connections (eg ssh). ufw does support adding rules before enabling the firewall, so administrators can do:

ufw allow proto tcp from any to any port 22

before running ’ufw enable’. The rules will still be flushed, but the ssh port will be open after enabling the firewall. Please note that once ufw is ’enabled’, ufw will not flush the chains when adding or removing rules (but will when modifying a rule or changing the default policy). By default, ufw will prompt when enabling the firewall while running under ssh. This can be disabled by using ’ufw --force enable’.


The following reports are supported. Each is based on the live system and with the exception of the listening report, is in raw iptables format:


The raw report shows the complete firewall, while the others show a subset of what is in the raw report.

The listening report will display the ports on the live system in the listening state for tcp and the open state for udp, along with the address of the interface and the executable listening on the port. An ’*’ is used in place of the address of the interface when the executable is bound to all interfaces on that port. Following this information is a list of rules which may affect connections on this port. The rules are listed in the order they are evaluated by the kernel, and the first match wins. Please note that the default policy is not listed and tcp6 and udp6 are shown only if IPV6 is enabled.

The added report displays the list of rules as they were added on the command-line. This report does not show the status of the running firewall (use ’ufw status’ instead). Because rules are normalized by ufw, rules may look different than the originally added rule. Also, ufw does not record command ordering, so an equivalent ordering is used which lists IPv6-only rules after other rules.

rule syntax

Users can specify rules using either a simple syntax or a full syntax. The simple syntax only specifies the port and optionally the protocol to be allowed or denied on the host. For example:

ufw allow 53

This rule will allow tcp and udp port 53 to any address on this host. To specify a protocol, append ’/protocol’ to the port. For example:

ufw allow 25/tcp

This will allow tcp port 25 to any address on this host. ufw will also check /etc/services for the port and protocol if specifying a service by name. Eg:

ufw allow smtp

ufw supports both ingress and egress filtering and users may optionally specify a direction of either in or out for either incoming or outgoing traffic. If no direction is supplied, the rule applies to incoming traffic. Eg:

ufw allow in http
ufw reject out smtp

Users can also use a fuller syntax, specifying the source and destination addresses and ports. This syntax is based on OpenBSD’s PF syntax. For example:

ufw deny proto tcp to any port 80

This will deny all traffic to tcp port 80 on this host. Another example:

ufw deny proto tcp from to port 25

This will deny all traffic from the RFC1918 Class A network to tcp port 25 with the address

ufw deny proto tcp from 2001:db8::/32 to any port 25

This will deny all traffic from the IPv6 2001:db8::/32 to tcp port 25 on this host. Note that IPv6 must be enabled in /etc/default/ufw for IPv6 firewalling to work.

ufw allow proto tcp from any to any port 80,443,8080:8090

The above will allow all traffic to tcp ports 80, 443 and 8080-8090 inclusive. Note that when specifying multiple ports, the ports list must be numeric, cannot contain spaces and must be modified as a whole. Eg, in the above example you cannot later try to delete just the ’443’ port. You cannot specify more than 15 ports (ranges count as 2 ports, so the port count in the above example is 4).

ufw supports connection rate limiting, which is useful for protecting against brute-force login attacks. ufw will deny connections if an IP address has attempted to initiate 6 or more connections in the last 30 seconds. See for details. Typical usage is:

ufw limit ssh/tcp

Sometimes it is desirable to let the sender know when traffic is being denied, rather than simply ignoring it. In these cases, use reject instead of deny. For example:

ufw reject auth

By default, ufw will apply rules to all available interfaces. To limit this, specify DIRECTION on INTERFACE, where DIRECTION is one of in or out (interface aliases are not supported). For example, to allow all new incoming http connections on eth0, use:

ufw allow in on eth0 to any port 80 proto tcp

To delete a rule, simply prefix the original rule with delete. For example, if the original rule was:

ufw deny 80/tcp

Use this to delete it:

ufw delete deny 80/tcp

You may also specify the rule by NUM, as seen in the status numbered output. For example, if you want to delete rule number ’3’, use:

ufw delete 3

If you have IPv6 enabled and are deleting a generic rule that applies to both IPv4 and IPv6 (eg ’ufw allow 22/tcp’), deleting by rule number will delete only the specified rule. To delete both with one command, prefix the original rule with delete.

To insert a rule, specify the new rule as normal, but prefix the rule with the rule number to insert. For example, if you have four rules, and you want to insert a new rule as rule number three, use:

ufw insert 3 deny to any port 22 from proto tcp

To see a list of numbered rules, use:

ufw status numbered

ufw supports per rule logging. By default, no logging is performed when a packet matches a rule. Specifying log will log all new connections matching the rule, and log-all will log all packets matching the rule. For example, to allow and log all new ssh connections, use:

ufw allow log 22/tcp

See LOGGING for more information on logging.


ufw [--dry-run] enable|disable|reload
ufw [--dry-run] default allow|deny|reject [incoming|outgoing]
ufw [--dry-run] logging on|off|LEVEL
ufw [--dry-run] reset
ufw [--dry-run] status [verbose|numbered]
ufw [--dry-run] show REPORT
ufw [--dry-run] [delete] [insert NUM] allow|deny|reject|limit [in|out]
[log|log-all] PORT[/protocol]
ufw [--dry-run] [delete] [insert NUM] allow|deny|reject|limit [in|out
] [log|log-all] [proto protocol] [from ADDRESS [port PORT]]
[to ADDRESS [port PORT]]
ufw [--dry-run] delete NUM
ufw [--dry-run] app list|info|default|update

see also

ufw-framework, iptables , ip6tables , iptables-restore , ip6tables-restore , sysctl , sysctl.conf


ufw is Copyright 2008-2009, Canonical Ltd.

ufw and this manual page was originally written by Jamie Strandboge <jamie[:at:]canonical[:dot:]com>

How can this site be more helpful to YOU ?

give  feedback