Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
articles:debian-setup-ufw [2024/11/29 14:56] – créée frater | articles:debian-setup-ufw [2024/12/11 12:44] (Version actuelle) – frater | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | ====== | + | ====== |
'' | '' | ||
Ligne 7: | Ligne 7: | ||
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. | 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. | ||
+ | ===== Requierements ===== | ||
To follow this article, you will need: | To follow this article, you will need: | ||
* A server running debian based distribution, | * A server running debian based distribution, | ||
- | * UFW is installed by default on debian. If it has been uninstalled for some reason, you can install it with '' | + | * UFW is **NOT** |
===== Making Sure IPv6 is Enabled ===== | ===== Making Sure IPv6 is Enabled ===== | ||
Ligne 41: | Ligne 42: | ||
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 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 '' | + | To ensure that the default UFW incoming policy |
<cli bash> | <cli bash> | ||
frater@vulkan: | frater@vulkan: | ||
+ | Default incoming policy changed to ' | ||
+ | (be sure to update your rules accordingly) | ||
</ | </ | ||
+ | To ensure that the default UFW outgoing policy is set to '' | ||
- | Output | + | <cli bash> |
- | Default | + | frater@vulkan: |
+ | Default | ||
(be sure to update your rules accordingly) | (be sure to update your rules accordingly) | ||
+ | </ | ||
- | To set the default UFW outgoing policy | + | These commands |
- | sudo ufw default allow outgoing | + | ===== Allowing SSH Connections ===== |
- | Output | + | If you were to enable |
- | Default outgoing policy changed | + | |
- | (be sure to update | + | |
- | These commands set the defaults | + | This means that you’ll need to create rules that explicitly |
- | Step 3 — Allowing | + | |
- | 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, | + | ==== Allowing the OpenSSH UFW Application Profile |
- | Allowing the OpenSSH UFW Application Profile | + | |
Upon installation, | Upon installation, | ||
- | | + | <cli bash> |
- | + | frater@vulkan: | |
- | Output | + | |
Available applications: | Available applications: | ||
+ | AIM | ||
+ | Bonjour | ||
+ | CIFS | ||
+ | DNS | ||
+ | Deluge | ||
+ | IMAP | ||
+ | IMAPS | ||
+ | IPP | ||
+ | : | ||
OpenSSH | OpenSSH | ||
+ | POP3 | ||
+ | POP3S | ||
+ | PeopleNearby | ||
+ | SMTP | ||
+ | SSH | ||
+ | : | ||
+ | VNC | ||
+ | WWW | ||
+ | WWW Cache | ||
+ | WWW Full | ||
+ | WWW Secure | ||
+ | XMPP | ||
+ | : | ||
+ | </ | ||
To enable the OpenSSH application profile, run: | To enable the OpenSSH application profile, run: | ||
- | | + | <cli bash> |
- | + | frater@vulkan: | |
- | Output | + | |
Rule added | Rule added | ||
Rule added (v6) | 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. | 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 | + | ==== Allowing |
- | sudo ufw allow ssh | + | Another way to configure UFW to allow incoming SSH connections is by referencing its service name: '' |
- | Output | + | <cli bash> |
+ | frater@vulkan: | ||
Rule added | Rule added | ||
Rule added (v6) | Rule added (v6) | ||
+ | </ | ||
+ | |||
+ | UFW knows which ports and protocols a service uses based on the ''/ | ||
- | UFW knows which ports and protocols a service uses based on the / | + | ==== Allowing SSH by Port Number |
- | Allowing SSH by Port Number | + | |
Alternatively, | Alternatively, | ||
- | sudo ufw allow 22 | ||
- | Output | + | <cli bash> |
+ | frater@vulkan: | ||
Rule added | Rule added | ||
Rule added (v6) | 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 | + | <WRAP center round important> |
+ | If you have configured your SSH daemon to use a different port, you will have to specify the appropriate port. | ||
- | Output | + | For example, if your SSH server is listening on port 2222, you can use this command to allow connections on that port: |
+ | |||
+ | <cli bash> | ||
+ | frater@vulkan: | ||
Rule added | Rule added | ||
Rule added (v6) | Rule added (v6) | ||
+ | </ | ||
+ | |||
+ | </ | ||
+ | |||
+ | ==== Commenting rules ==== | ||
+ | |||
+ | It's always a good idea to comment your rules for futur review; especially if you're using non standard tcp port or custom applications: | ||
+ | |||
+ | <cli bash> | ||
+ | frater@vulkan: | ||
+ | Rule added | ||
+ | Rule added (v6) | ||
+ | </ | ||
+ | |||
+ | You can add/modify comment to existing rules: | ||
+ | <cli bash> | ||
+ | frater@vulkan: | ||
+ | Rule updated | ||
+ | Rule updated (v6) | ||
+ | </ | ||
+ | |||
+ | You can add also remove comment to existing rules: | ||
+ | <cli bash> | ||
+ | frater@vulkan: | ||
+ | Rule updated | ||
+ | Rule updated (v6) | ||
+ | </ | ||
Now that your firewall is configured to allow incoming SSH connections, | Now that your firewall is configured to allow incoming SSH connections, | ||
- | Step 4 — Enabling UFW | + | ===== 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: | 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: | ||
- | | + | <cli bash> |
- | + | frater@vulkan: | |
- | Output | + | |
Added user rules (see 'ufw status' | Added user rules (see 'ufw status' | ||
ufw allow OpenSSH | ufw allow OpenSSH | ||
+ | </ | ||
After confirming your have a rule to allow incoming SSH connections, | After confirming your have a rule to allow incoming SSH connections, | ||
- | | + | <cli bash> |
- | + | frater@vulkan: | |
- | Output | + | |
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y | Command may disrupt existing ssh connections. Proceed with operation (y|n)? y | ||
Firewall is active and enabled on system startup | 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, | + | 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, |
- | The firewall is now active. | + | The firewall is now active. |
- | Step 5 — Allowing Other Connections | + | |
- | At this point, you should allow all of the other connections | + | Run the '' |
- | HTTP on port 80, which is what unencrypted web servers use, using sudo ufw allow http or sudo ufw allow 80 | + | ===== Allowing Other Connections ===== |
- | 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. | + | 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 '' |
+ | |||
+ | * HTTP on port 80, which is what unencrypted web servers use, using '' | ||
+ | * HTTPS on port 443, which is what encrypted web servers use, using '' | ||
+ | * Apache with both HTTP and HTTPS, using '' | ||
+ | * Nginx with both HTTP and HTTPS, using '' | ||
+ | |||
+ | Don’t forget to check which application profiles are available for your server with '' | ||
There are several other ways to allow connections, | There are several other ways to allow connections, | ||
- | Specific Port Ranges | + | |
+ | ==== Specific Port Ranges | ||
You can specify port ranges with UFW. Some applications use multiple ports, instead of a single port. | You can specify port ranges with UFW. Some applications use multiple ports, instead of a single port. | ||
- | For example, to allow X11 connections, | + | For example, to allow X11 connections, |
- | | + | <cli bash> |
- | sudo ufw allow 6000: | + | frater@vulkan: |
+ | Rule added | ||
+ | Rule added (v6) | ||
+ | frater@vulkan: | ||
+ | Rule added | ||
+ | Rule added (v6) | ||
+ | </ | ||
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. | 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 | + | ==== Specific |
- | sudo ufw allow from 203.0.113.4 | + | When working with UFW, you can also specify IP addresses within your rules. For example, if you want to allow connections |
- | Output | + | <cli bash> |
+ | frater@vulkan: | ||
Rule added | Rule added | ||
+ | </ | ||
- | You can also specify a port that the IP address | + | <WRAP center round info> |
+ | you're getting only **ONE** '' | ||
+ | </ | ||
- | sudo ufw allow from 203.0.113.4 to any port 22 | ||
- | Output | + | 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 123.45.67.89 to connect to port 22 (SSH), use this command: |
+ | |||
+ | |||
+ | <cli bash> | ||
+ | frater@vulkan: | ||
Rule added | Rule added | ||
+ | </ | ||
- | Subnets | + | ==== 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: | + | 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 '' |
- | sudo ufw allow from 203.0.113.0/ | ||
- | Output | + | <cli bash> |
+ | frater@vulkan: | ||
Rule added | Rule added | ||
+ | </ | ||
- | Likewise, you may also specify the destination port that the subnet | + | Likewise, you may also specify the destination port that the subnet |
- | sudo ufw allow from 203.0.113.0/ | ||
- | Output | + | <cli bash> |
+ | frater@vulkan: | ||
Rule added | Rule added | ||
+ | </ | ||
- | Connections to a Specific Network Interface | + | ==== 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 | + | If you want to create a firewall rule that only applies to a specific network interface, you can do so by specifying |
You may want to look up your network interfaces before continuing. To do so, use this command: | You may want to look up your network interfaces before continuing. To do so, use this command: | ||
- | | + | <cli bash> |
+ | frater@vulkan: | ||
+ | 2: eth0: < | ||
+ | : | ||
+ | 3: eth1: < | ||
+ | : | ||
+ | </ | ||
- | Output Excerpt | + | The output indicates the network interface names; they are typically named something like '' |
- | 2: eth0: < | + | |
- | . . . | + | |
- | 3: eth1: < | + | |
- | . . . | + | |
- | The highlighted output indicates the network interface | + | So, if your server has a public |
- | So, if your server has a public network interface called eth0, you could allow HTTP traffic (port 80) to it with this command: | + | <cli bash> |
- | + | frater@vulkan:$ sudo ufw allow in on eth0 to any port 80 | |
- | | + | |
- | + | ||
- | Output | + | |
Rule added | Rule added | ||
Rule added (v6) | Rule added (v6) | ||
+ | </ | ||
Doing so would allow your server to receive HTTP requests from the public internet. | 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: | + | Or, if you want your MySQL database server (port '' |
- | | + | <cli bash> |
- | + | frater@vulkan: | |
- | Output | + | |
Rule added | Rule added | ||
Rule added (v6) | Rule added (v6) | ||
+ | </ | ||
This would allow other servers on your private network to connect to your MySQL database. | This would allow other servers on your private network to connect to your MySQL database. | ||
- | Step 6 — Denying Connections | + | ===== Denying Connections |
If you haven’t changed the default policy for incoming connections, | If you haven’t changed the default policy for incoming connections, | ||
- | 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), | + | 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), |
- | To write deny rules, you can use the commands previously described, replacing allow with deny. | + | To write '' |
For example, to deny HTTP connections, | For example, to deny HTTP connections, | ||
- | | + | <cli bash> |
- | + | frater@vulkan: | |
- | Output | + | |
Rule added | Rule added | ||
Rule added (v6) | Rule added (v6) | ||
+ | </ | ||
- | Or if you want to deny all connections from 203.0.113.4 you could use this command: | + | Or if you want to deny all connections from 123.45.67.894 you could use this command: |
- | | + | <cli bash> |
- | + | frater@vulkan: | |
- | Output | + | |
Rule added | Rule 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: | + | 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 '' |
- | + | ||
- | sudo ufw deny out 25 | + | |
- | Output | + | <cli bash> |
+ | frater@vulkan: | ||
Rule added | Rule added | ||
Rule added (v6) | Rule added (v6) | ||
+ | </ | ||
This will block all outgoing SMTP traffic on the server. | This will block all outgoing SMTP traffic on the server. | ||
- | Step 7 — Deleting Rules | + | ===== 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). | 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 | + | ==== Deleting |
- | sudo ufw status | + | To delete a UFW rule by its number, first you’ll want to obtain a numbered |
- | Numbered Output: | + | <cli bash> |
+ | frater@vulkan:$ sudo ufw status numbered | ||
Status: active | Status: active | ||
| | ||
| | ||
- | [ 1] 22 ALLOW IN 15.15.15.0/ | + | [ 1] 22 ALLOW IN 15.15.15.0/ |
- | [ 2] 80 ALLOW IN Anywhere | + | [ 2] 80 ALLOW IN Anywhere |
- | + | </ | |
- | If you decide that you want to delete rule number 2, the one that allows port 80 (HTTP) connections, | + | |
- | sudo ufw delete 2 | + | If you decide that you want to delete |
- | Output | + | <cli bash> |
+ | frater@vulkan: | ||
Deleting: | Deleting: | ||
allow 80 | allow 80 | ||
Proceed with operation (y|n)? y | Proceed with operation (y|n)? y | ||
Rule deleted | Rule deleted | ||
+ | </ | ||
- | This will prompt for a confirmation then delete rule 2, which allows HTTP connections. | + | This will prompt for a confirmation then delete rule 2, which allows HTTP connections. |
- | Deleting a UFW Rule By Name | + | |
- | Instead of using rule numbers, | + | <WRAP center round important> |
+ | If you have IPv6 enabled, you would want to delete | ||
+ | </ | ||
- | sudo ufw delete allow " | + | ==== Deleting a UFW Rule By Name ==== |
- | Output | + | Instead of using rule numbers, you may also refer to a rule by its human readable denomination, |
+ | |||
+ | <cli bash> | ||
+ | frater@vulkan: | ||
Rule deleted | Rule deleted | ||
Rule deleted (v6) | 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: | + | The '' |
- | sudo ufw delete allow http | ||
- | Output | + | <cli bash> |
+ | frater@vulkan: | ||
Rule deleted | Rule deleted | ||
Rule deleted (v6) | 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: | + | Because service names are interchangeable with port numbers when specifying rules, you could also refer to the same rule as '' |
- | sudo ufw delete allow 80 | ||
- | Output | + | <cli bash> |
+ | frater@vulkan: | ||
Rule deleted | Rule deleted | ||
Rule deleted (v6) | Rule deleted (v6) | ||
+ | </ | ||
+ | <WRAP center round info> | ||
When deleting UFW rules by name, both IPv4 and IPv6 rules are deleted if they exist. | When deleting UFW rules by name, both IPv4 and IPv6 rules are deleted if they exist. | ||
- | Step 8 — Checking UFW Status and Rules | + | </ |
+ | ===== Checking UFW Status and Rules ===== | ||
At any time, you can check the status of UFW with this command: | At any time, you can check the status of UFW with this command: | ||
- | | + | <cli bash> |
+ | frater@vulkan: | ||
+ | </ | ||
If UFW is disabled, which it is by default, you’ll see something like this: | If UFW is disabled, which it is by default, you’ll see something like this: | ||
- | Output | + | <cli bash> |
Status: inactive | Status: inactive | ||
+ | </ | ||
- | If UFW is active, which it should be if you followed | + | If UFW is active, which it should be if you followed |
+ | <cli bash> | ||
Output | Output | ||
Status: active | Status: active | ||
Ligne 332: | Ligne 421: | ||
-- | -- | ||
22/ | 22/ | ||
+ | </ | ||
Use the status command if you want to check how UFW has configured the firewall. | Use the status command if you want to check how UFW has configured the firewall. | ||
- | Step 9 — Disable or Reset Firewall | + | ===== Disable or Reset Firewall |
If you decide you don’t want to use the UFW firewall, you can deactivate it with this command: | If you decide you don’t want to use the UFW firewall, you can deactivate it with this command: | ||
- | sudo ufw disable | ||
- | Output | + | <cli bash> |
+ | frater@vulkan: | ||
Firewall stopped and disabled on system startup | Firewall 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. | + | Any rules that you created with UFW will no longer be active. You can always run '' |
If you already have UFW rules configured but you decide that you want to start over, you can use the reset command: | 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 | ||
- | Output | + | <cli bash> |
+ | frater@vulkan: | ||
Resetting all rules to installed defaults. This may disrupt existing ssh | Resetting all rules to installed defaults. This may disrupt existing ssh | ||
connections. Proceed with operation (y|n)? y | connections. Proceed with operation (y|n)? y | ||
- | Backing up ' | + | Backing up ' |
- | Backing up ' | + | Backing up ' |
- | Backing up ' | + | Backing up ' |
- | Backing up ' | + | Backing up ' |
- | Backing up ' | + | Backing up ' |
- | Backing up ' | + | Backing up ' |
+ | </ | ||
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. | 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. | ||
- | Deploy your frontend applications from GitHub using DigitalOcean App Platform. Let DigitalOcean focus on scaling your app. | + | ===== Conclusion |
- | 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, | 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, | ||
- | |||
- | To learn about more common UFW configurations, |