SpiffberryPI | UFW Firewall Settings *

YOU MUST SET UP SSH / FTP / PASSAGE THROUGH FIREWALL BEFORE YOU LOGOUT / ENABLE UFW –

or you will lock yourself out.


AVOID LOCKING YOURSELF OUT - set up ssh access 1stDeeper Config for UFW rulesports 80 / 443 w/ ufw

https://www.cyberciti.biz/faq/ufw-allow-incoming-ssh-connections-from-a-specific-ip-address-subnet-on-ubuntu-debian/

How to open ssh 22/TCP port using ufw on Ubuntu/Debian Linux

H do I allow incoming SSH connections from a specific IP address or subnet on a Ubuntu or Debian Linux server using ufw? How do I open ssh 22/TCP port using ufw on Ubuntu or Debian server?

 

Tutorial details 
Difficulty levelEasy
Root privilegesYes
Requirementsufw on Ubuntu/Debian Linux
Est. reading time3 mintues

UFW is an acronym for uncomplicated firewall. It is used for managing a Linux firewall and aims to provide an easy to use interface for the user. In this tutorial, you will learn how to use UFW a frontend to iptables for open incoming SSH port / connection on Ubuntu Linux 16.04/18.04/20.04 LTS or Debian Linux server.

Fig.01: Allow Incoming SSH from Specific IP Address or Subnet

ssh is a client program for logging into a remote machine and for executing commands on a remote Linux or Unix computer. SSHD is the daemon program for ssh. Bots and unwanted people often target SSHD. Hence, you must protect your server.

Open SSH port using ufw

The syntax is as follows to open ssh port using ufw command: sudo ufw allow ssh
OR
$ sudo ufw allow 22/tcp
One can add the comment as follows:
$ sudo ufw allow 22/tcp comment 'Open port ssh tcp port 22'
If you are running ssh on TCP port # 2222, enter:
$ sudo ufw allow 2222/tcp


Allowing SSH connections


In this example, I am going to configure my server to allow incoming SSH connections but only from IP address 192.168.1.100 and sub/net (CIDR) 192.168.2.0/24:
sudo ufw allow from 192.168.1.100 to any port 22
sudo ufw allow from 192.168.2.0/24 to any port 22 proto tcp

Verify it:
sudo ufw status


Status: active
 
To                         Action      From
--                         ------      ----
22/tcp                     ALLOW       192.168.2.0/24

How to allow incoming SSH from specific IP address

The syntax is same as above but we are going restrict destionation IP address too:
$ sudo ufw allow from {IP_ADDRESS_HERE} to any port 22
To allow incoming SSH connections from a specific IP address named 202.54.1.1, enter:
$ sudo ufw allow from 202.54.1.1 to any port 22
In this example, allow from 10.105.28.0/24 to SSH server running at 10.105.29.1 and TCP port 22:
sudo ufw allow from 10.105.28.0/24 to 10.105.29.1 port 22 proto tcp comment 'Allow SSH'

Opening ssh port using ufw for VPN IP

Say you have a VPN with a public IPv4 address 139.1.2.3 and only wanted to ssh access from that IP. Then you would run:

export VPN_IP="139.1.2.3"  # VPN server/client address
export SERVER_PUB_IP="198.74.55.33"  # server IPv4 address
export SSH_PUB_PORT="22"   # server ssh port number 
sudo ufw allow from "$VPN_IP" to "$SERVER_PUB_IP" port "$SSH_PUB_PORT" proto tcp comment 'Only allow VPN IP to access SSH port'

Verify it:

sudo ufw status
sudo ufw show added

Here is the confirmation:

Added user rules (see 'ufw status' for running firewall):
ufw allow from 139.1.2.3 to 198.74.55.33 port 22 proto tcp comment 'Only allow VPN IP to access SSH port'

How to allow incoming SSH from specific subnets

The syntax is:
$ sudo ufw allow from {IP_SUB/net} to any port 22
Alternatively:
$ sudo ufw allow from {IP_SUB/net} to any port 22 proto tcp
We can state destination sshd server IP too using the ufw:
$ sudo ufw allow from {IP_SUB/net} to {ssh-server-ip-address} port 22 proto tcp


https://www.digitalocean.com/community/tutorials/how-to-set-up-a-firewall-with-ufw-on-ubuntu-18-04

How To Set Up a Firewall with UFW on Ubuntu 18.04

Introduction

UFW, or Uncomplicated Firewall, is an interface to iptables that is geared towards simplifying the process of configuring a firewall. While iptables is a solid and flexible tool, it can be difficult for beginners to learn how to use it to properly configure a firewall. If you’re looking to get started securing your network, and you’re not sure which tool to use, UFW may be the right choice for you.

This tutorial will show you how to set up a firewall with UFW on Ubuntu 18.04.

Prerequisites

To follow this tutorial, you will need:

UFW is installed by default on Ubuntu. If it has been uninstalled for some reason, you can install it with sudo apt install ufw.

Step 1 — Making Sure IPv6 is Enabled

In recent versions of Ubuntu, IPv6 is enabled by default. In practice that means most firewall rules added to the server will include both an IPv4 and an IPv6 version, the latter identified by v6 within the output of UFW’s status command. To make sure IPv6 is enabled, you can check your UFW configuration file at /etc/default/ufw. Open this file using nano or your favorite command line editor:

sudo nano /etc/default/ufw

Then make sure the value of IPV6 is set to yes. It should look like this:

/etc/default/ufw excerpt

IPV6=yes

Save and close the file. If you’re using nano, you can do that by typing CTRL+X, then Y and ENTER to confirm.

When UFW is enabled in a later step of this guide, it will be configured to write both IPv4 and IPv6 firewall rules.

Step 2 — Setting Up Default Policies

If you’re just getting started with UFW, a good first step is to check your default firewall policies. These rules control how to handle traffic that does not explicitly match any other rules.

By default, UFW is set to deny all incoming connections and allow all outgoing connections. This means anyone trying to reach your server would not be able to connect, while any application within the server would be able to reach the outside world. Additional rules to allow specific services and ports are included as exceptions to this general policy.

To make sure you’ll be able to follow along with the rest of this tutorial, you’ll now set up your UFW default policies for incoming and outgoing traffic.

To set the default UFW incoming policy to deny, run:

sudo ufw default deny incoming

OutputDefault incoming policy changed to 'deny'
(be sure to update your rules accordingly)

To set the default UFW outgoing policy to allow, run:

sudo ufw default allow outgoing

OutputDefault outgoing policy changed to 'allow'
(be sure to update your rules accordingly)

These commands set the defaults to deny incoming and allow outgoing connections. These firewall defaults alone might suffice for a personal computer, but servers typically need to respond to incoming requests from outside users. We’ll look into that next.

Step 3 — Allowing SSH Connections

If you were to enable your UFW firewall now, it would deny all incoming connections. This means that you’ll need to create rules that explicitly allow legitimate incoming connections — SSH or HTTP connections, for example — if you want your server to respond to those types of requests. If you’re using a cloud server, you will probably want to allow incoming SSH connections so you can connect to and manage your server.

Allowing the OpenSSH UFW Application Profile

Upon installation, most applications that rely on network connections will register an application profile within UFW, which enables users to quickly allow or deny external access to a service. You can check which profiles are currently registered in UFW with:

sudo ufw app list

OutputAvailable applications:
  OpenSSH

To enable the OpenSSH application profile, run:

sudo ufw allow OpenSSH

OutputRule added
Rule added (v6)

This will create firewall rules to allow all connections on port 22, which is the port that the SSH daemon listens on by default.

Allowing SSH by Service Name

Another way to configure UFW to allow incoming SSH connections is by referencing its service name: ssh.

sudo ufw allow ssh

OutputRule added
Rule added (v6)

UFW knows which ports and protocols a service uses based on the /etc/services file.

Allowing SSH by Port Number

Alternatively, you can write the equivalent rule by specifying the port instead of the application profile or service name. For example, this command works the same as the previous examples:

sudo ufw allow 22

OutputRule added
Rule added (v6)

If you configured your SSH daemon to use a different port, you will have to specify the appropriate port. For example, if your SSH server is listening on port 2222, you can use this command to allow connections on that port:

sudo ufw allow 2222

OutputRule added
Rule added (v6)

Now that your firewall is configured to allow incoming SSH connections, you can enable it.

Step 4 — Enabling UFW

Your firewall should now be configured to allow SSH connections. To verify which rules were added so far, even when the firewall is still disabled, you can use:

sudo ufw show added

OutputAdded user rules (see 'ufw status' for running firewall):
ufw allow OpenSSH

After confirming your have a rule to allow incoming SSH connections, you can enable the firewall with:

sudo ufw enable

OutputCommand may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startup

You will receive a warning that says the command may disrupt existing SSH connections. You already set up a firewall rule that allows SSH connections, so it should be fine to continue. Respond to the prompt with y and hit ENTER.

The firewall is now active. Run the sudo ufw status verbose command to see the rules that are set. The rest of this tutorial covers how to use UFW in more detail, like allowing or denying different kinds of connections.

Step 5 — Allowing Other Connections

At this point, you should allow all of the other connections that your server needs to respond to. The connections that you should allow depend on your specific needs. You already know how to write rules that allow connections based on an application profile, a service name, or a port; you already did this for SSH on port 22. You can also do this for:

  • HTTP on port 80, which is what unencrypted web servers use, using sudo ufw allow http or sudo ufw allow 80
  • HTTPS on port 443, which is what encrypted web servers use, using sudo ufw allow https or sudo ufw allow 443
  • Apache with both HTTP and HTTPS, using sudo ufw allow ‘Apache Full’
  • Nginx with both HTTP and HTTPS, using sudo ufw allow ‘Nginx Full’

Don’t forget to check which application profiles are available for your server with sudo ufw app list.

There are several other ways to allow connections, aside from specifying a port or known service name. We’ll see some of these next.

Specific Port Ranges

You can specify port ranges with UFW. Some applications use multiple ports, instead of a single port.

For example, to allow X11 connections, which use ports 60006007, use these commands:

sudo ufw allow 6000:6007/tcp
sudo ufw allow 6000:6007/udp

When specifying port ranges with UFW, you must specify the protocol (tcp or udp) that the rules should apply to. We haven’t mentioned this before because not specifying the protocol automatically allows both protocols, which is OK in most cases.

Specific IP Addresses

When working with UFW, you can also specify IP addresses within your rules. For example, if you want to allow connections from a specific IP address, such as a work or home IP address of 203.0.113.4, you need to use the from parameter, providing then the IP address you want to allow:

sudo ufw allow from 203.0.113.4

OutputRule added

You can also specify a port that the IP address is allowed to connect to by adding to any port followed by the port number. For example, If you want to allow 203.0.113.4 to connect to port 22 (SSH), use this command:

sudo ufw allow from 203.0.113.4 to any port 22

OutputRule added

Subnets

If you want to allow a subnet of IP addresses, you can do so using CIDR notation to specify a netmask. For example, if you want to allow all of the IP addresses ranging from 203.0.113.1 to 203.0.113.254 you could use this command:

sudo ufw allow from 203.0.113.0/24

OutputRule added

Likewise, you may also specify the destination port that the subnet 203.0.113.0/24 is allowed to connect to. Again, we’ll use port 22 (SSH) as an example:

sudo ufw allow from 203.0.113.0/24 to any port 22

OutputRule added

Connections to a Specific Network Interface

If you want to create a firewall rule that only applies to a specific network interface, you can do so by specifying “allow in on” followed by the name of the network interface.

You may want to look up your network interfaces before continuing. To do so, use this command:

ip addr

Output Excerpt2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state
. . .
3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default
. . .

The highlighted output indicates the network interface names. They are typically named something like eth0 or enp3s2.

So, if your server has a public network interface called eth0, you could allow HTTP traffic (port 80) to it with this command:

sudo ufw allow in on eth0 to any port 80

OutputRule added
Rule added (v6)

Doing so would allow your server to receive HTTP requests from the public internet.

Or, if you want your MySQL database server (port 3306) to listen for connections on the private network interface eth1, for example, you could use this command:

sudo ufw allow in on eth1 to any port 3306

OutputRule added
Rule added (v6)

This would allow other servers on your private network to connect to your MySQL database.

Step 6 — Denying Connections

If you haven’t changed the default policy for incoming connections, UFW is configured to deny all incoming connections. Generally, this simplifies the process of creating a secure firewall policy by requiring you to create rules that explicitly allow specific ports and IP addresses through.

However, sometimes you will want to deny specific connections based on the source IP address or subnet, perhaps because you know that your server is being attacked from there. Also, if you want to change your default incoming policy to allow (which is not recommended), you would need to create deny rules for any services or IP addresses that you don’t want to allow connections for.

To write deny rules, you can use the commands previously described, replacing allow with deny.

For example, to deny HTTP connections, you could use this command:

sudo ufw deny http

OutputRule added
Rule added (v6)

Or if you want to deny all connections from 203.0.113.4 you could use this command:

sudo ufw deny from 203.0.113.4

OutputRule added

In some cases, you may also want to block outgoing connections from the server. To deny all users from using a port on the server, such as port 25 for SMTP traffic, you can use deny out followed by the port number:

sudo ufw deny out 25

OutputRule added
Rule added (v6)

This will block all outgoing SMTP traffic on the server.

Step 7 — Deleting Rules

Knowing how to delete firewall rules is just as important as knowing how to create them. There are two different ways to specify which rules to delete: by rule number or by its human-readable denomination (similar to how the rules were specified when they were created).

Deleting a UFW Rule By Number

To delete a UFW rule by its number, first you’ll want to obtain a numbered list of all your firewall rules. The UFW status command has an option to display numbers next to each rule, as demonstrated here:

sudo ufw status numbered

Numbered Output:Status: active

     To                         Action      From
     --                         ------      ----
[ 1] 22                         ALLOW IN    15.15.15.0/24
[ 2] 80                         ALLOW IN    Anywhere

If you decide that you want to delete rule number 2, the one that allows port 80 (HTTP) connections, you can specify it in a UFW delete command like this:

sudo ufw delete 2

OutputDeleting:
 allow 80
Proceed with operation (y|n)? y
Rule deleted

This will prompt for a confirmation then delete rule 2, which allows HTTP connections. Note that if you have IPv6 enabled, you would want to delete the corresponding IPv6 rule as well.

Deleting a UFW Rule By Name

Instead of using rule numbers, you may also refer to a rule by its human readable denomination, which is based on the type of rule (typically allow or deny) and the service name or port number that was the target for this rule, or the application profile name in case that was used. For example, if you want to delete an allow rule for an application profile called Apache Full that was previously enabled, you can use:

sudo ufw delete allow "Apache Full"

OutputRule deleted
Rule deleted (v6)

The delete command works the same way for rules that were created referencing a service by its name or port. For example, if you previously set a rule to allow HTTP connections with sudo ufw allow http, this is how you could delete said rule:

sudo ufw delete allow http

OutputRule deleted
Rule deleted (v6)

Because service names are interchangeable with port numbers when specifying rules, you could also refer to the same rule as allow 80, instead of allow http:

sudo ufw delete allow 80

OutputRule deleted
Rule deleted (v6)

When deleting UFW rules by name, both IPv4 and IPv6 rules are deleted if they exist.

Step 8 — Checking UFW Status and Rules

At any time, you can check the status of UFW with this command:

sudo ufw status verbose

If UFW is disabled, which it is by default, you’ll see something like this:

OutputStatus: inactive

If UFW is active, which it should be if you followed Step 3, the output will say that it’s active and it will list any rules that are set. For example, if the firewall is set to allow SSH (port 22) connections from anywhere, the output might look something like this:

OutputStatus: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW IN    Anywhere

Use the status command if you want to check how UFW has configured the firewall.

Step 9 — Disabling or Resetting UFW (optional)

If you decide you don’t want to use UFW, you can disable it with this command:

sudo ufw disable

OutputFirewall stopped and disabled on system startup

Any rules that you created with UFW will no longer be active. You can always run sudo ufw enable if you need to activate it later.

If you already have UFW rules configured but you decide that you want to start over, you can use the reset command:

sudo ufw reset

OutputResetting all rules to installed defaults. This may disrupt existing ssh
connections. Proceed with operation (y|n)? y
Backing up 'user.rules' to '/etc/ufw/user.rules.20210729_170353'
Backing up 'before.rules' to '/etc/ufw/before.rules.20210729_170353'
Backing up 'after.rules' to '/etc/ufw/after.rules.20210729_170353'
Backing up 'user6.rules' to '/etc/ufw/user6.rules.20210729_170353'
Backing up 'before6.rules' to '/etc/ufw/before6.rules.20210729_170353'
Backing up 'after6.rules' to '/etc/ufw/after6.rules.20210729_170353'

This will disable UFW and delete any rules that were previously defined. This should give you a fresh start with UFW. Keep in mind that the default policies won’t change to their original settings, if you modified them at any point.

Conclusion

Your firewall is now configured to allow (at least) SSH connections. Be sure to allow any other incoming connections that your server requires, while limiting any unnecessary connections, so your server will be functional and secure.

To learn about more common UFW configurations, check out the UFW Essentials: Common Firewall Rules and Commands tutorial.

https://linuxconfig.org/ubuntu-20-04-open-http-port-80-and-https-port-443-with-ufw

 

Ubuntu 20.04 Focal Fossa open HTTP port 80 and HTTPS port 443 step by step instructions

  1. Check the status of your firewall.

    # ufw status verbose
    Status: active
    Logging: on (low)
    Default: deny (incoming), allow (outgoing), disabled (routed)
    New profiles: skip
    

    Based on the above output all incomming ports are blocked by default.



  2. We have multiple options on how to open ports 80 and 443. First we can directly specify the port number or the service we wish to open the port for. Example:

    $ sudo ufw allow 80
    $ sudo ufw allow 443
    OR
    $ sudo ufw allow http
    $ sudo ufw allow https
    

    Alternatively, if we wish to open ports for a specific webserver such as Apache or Nginx we can execute the bellow commands:

    $ sudo ufw allow in "Apache Full"
    $ sudo ufw allow in "Nginx Full"
    
  3. Check you current firewall configuration settings:

    # ufw status verbose
    Status: active
    Logging: on (low)
    Default: deny (incoming), allow (outgoing), disabled (routed)
    New profiles: skip
    
    To                         Action      From
    --                         ------      ----
    80                         ALLOW IN    Anywhere                  
    443                        ALLOW IN    Anywhere                  
    80 (v6)                    ALLOW IN    Anywhere (v6)             
    443 (v6)                   ALLOW IN    Anywhere (v6)
    
  4. In case you will later on decide to remove the port 80,443 rules you can do so by executing the bellow commands:

    $ sudo ufw delete allow 80
    $ sudo ufw delete allow 443
    OR
    $ sudo ufw delete allow http
    $ sudo ufw delete allow https
    

    Alternatively, if we wish to open ports for a specific webserver such as Apache or Nginx we can execute the bellow commands:

    $ sudo ufw delete allow in "Apache Full"
    $ sudo ufw delete allow in "Nginx Full"
    

 

If you have a port forward – add that as well

 

me@spiffberrypi:/var/www/html$ sudo ufw allow **** (enter your port forward #)

 

Check status

$ sudo ufw status verbose
Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip

To                         Action      From
--                         ------      ----
137,138/udp (Samba)        ALLOW IN    Anywhere
139,445/tcp (Samba)        ALLOW IN    Anywhere
10000                      ALLOW IN    Anywhere
80                         ALLOW IN    Anywhere
443                        ALLOW IN    Anywhere
80,443/tcp (Nginx Full)    ALLOW IN    Anywhere
3330                       ALLOW IN    Anywhere
137,138/udp (Samba (v6))   ALLOW IN    Anywhere (v6)
139,445/tcp (Samba (v6))   ALLOW IN    Anywhere (v6)
10000 (v6)                 ALLOW IN    Anywhere (v6)
80 (v6)                    ALLOW IN    Anywhere (v6)
443 (v6)                   ALLOW IN    Anywhere (v6)
80,443/tcp (Nginx Full (v6)) ALLOW IN    Anywhere (v6)
**** - your port forward (v6)                  ALLOW IN    Anywhere (v6)

 

 

https://linuxconfig.org/firewall-ufw-status-inactive-on-ubuntu-20-04-focal-fossa-linux

 

 

How to activate an inactive UFW on Ubuntu 20.04 step by step instructions

  1. First, check your current UFW status:

    $ sudo ufw status
    Status: inactive
    


  2. Activate UFW:

    $ sudo ufw enable
    Firewall is active and enabled on system startup
    
  3. (optional) Check UFW status:

    $ sudo ufw status
    Status: active
    

    Once the UFW is enabled, this settings should persists after system reboots.

Scroll to Top