| 346 | |
| 347 | This output indicates that the '''Nginx Full''' application profile is currently enabled, allowing any and all connections to the web server both via HTTP as well as via HTTPS. If you’d want to only allow HTTPS requests from and to your web server, you’d have to first enable the most restrictive rule, which in this case would be '''Nginx HTTPS''', and then disable the currently active '''Nginx Full''' rule: |
| 348 | {{{ |
| 349 | sudo ufw allow "Nginx HTTPS" |
| 350 | sudo ufw delete allow "Nginx Full" |
| 351 | }}} |
| 352 | |
| 353 | '''Allow SSH''' |
| 354 | |
| 355 | When working with remote servers, you’ll want to make sure that the SSH port is open to connections so that you are able to log in to your server remotely. |
| 356 | |
| 357 | The following command will enable the OpenSSH UFW application profile and allow all connections to the default SSH port on the server: |
| 358 | {{{ |
| 359 | sudo ufw allow OpenSSH |
| 360 | }}} |
| 361 | Although less user-friendly, an alternative syntax is to specify the exact port number of the SSH service, which is typically set to 22 by default: |
| 362 | {{{ |
| 363 | sudo ufw allow 22 |
| 364 | }}} |
| 365 | '''Allow Incoming SSH from Specific IP Address or Subnet''' |
| 366 | |
| 367 | To allow incoming connections from a specific IP address or subnet, you’ll include a from directive to define the source of the connection. This will require that you also specify the destination address with a to parameter. To lock this rule to SSH only, you’ll limit the proto (protocol) to tcp and then use the port parameter and set it to 22, SSH’s default port. |
| 368 | |
| 369 | The following command will allow only SSH connections coming from the IP address 203.0.113.103: |
| 370 | {{{ |
| 371 | sudo ufw allow from 203.0.113.103 proto tcp to any port 22 |
| 372 | }}} |
| 373 | You can also use a subnet address as from parameter to allow incoming SSH connections from an entire network: |
| 374 | {{{ |
| 375 | sudo ufw allow from 203.0.113.0/24 proto tcp to any port 22 |
| 376 | }}} |
| 377 | '''Allow Nginx HTTP / HTTPS''' |
| 378 | |
| 379 | Upon installation, the Nginx web server sets up a few different UFW profiles within the server. Once you have Nginx installed and enabled as a service, run the following command to identify which profiles are available: |
| 380 | {{{ |
| 381 | sudo ufw app list | grep Nginx |
| 382 | }}} |
| 383 | {{{ |
| 384 | Output |
| 385 | Nginx Full |
| 386 | Nginx HTTP |
| 387 | Nginx HTTPS |
| 388 | }}} |
| 389 | To enable both HTTP and HTTPS traffic, choose '''''Nginx Full'''''. Otherwise, choose either '''''Nginx HTTP''''' to allow only HTTP or '''''Nginx HTTPS''''' to allow only HTTPS. |
| 390 | |
| 391 | The following command will allow both HTTP and HTTPS traffic on the server (ports 80 and 443): |
| 392 | {{{ |
| 393 | sudo ufw allow "Nginx Full" |
| 394 | }}} |
| 395 | '''Allow All Incoming HTTP and HTTPS''' |
| 396 | |
| 397 | If you want to allow both HTTP and HTTPS traffic, you can create a single rule that allows both ports. This usage requires that you also define the protocol with the proto parameter, which in this case should be set to tcp. |
| 398 | |
| 399 | To allow all incoming HTTP and HTTPS (ports 80 and 443) connections, run: |
| 400 | {{{ |
| 401 | sudo ufw allow proto tcp from any to any port 80,443 |
| 402 | }}} |
| 403 | |
| 404 | '''Allow MySQL Connection from Specific IP Address or Subnet''' |
| 405 | |
| 406 | MySQL listens for client connections on port 3306. If your MySQL database server is being used by a client on a remote server, you’ll need to create a UFW rule to allow that access. |
| 407 | |
| 408 | To allow incoming MySQL connections from a specific IP address or subnet, use the from parameter to specify the source IP address and the port parameter to set the destination port 3306. |
| 409 | |
| 410 | The following command will allow the IP address 203.0.113.103 to connect to the server’s MySQL port: |
| 411 | {{{ |
| 412 | sudo ufw allow from 203.0.113.103 to any port 3306 |
| 413 | }}} |
| 414 | To allow the entire 203.0.113.0/24 subnet to be able to connect to your MySQL server, run: |
| 415 | {{{ |
| 416 | sudo ufw allow from 203.0.113.0/24 to any port 3306 |
| 417 | }}} |