Dump the code

ufw : Manage firewall rules

Created 8 months ago
Posted By admin
4min read
"ufw" (Uncomplicated Firewall) is a user-friendly command-line utility designed to simplify the management of iptables, the default firewall tool for Linux. Its primary goal is to provide an accessible interface for users who want to set up and control their firewall without dealing with the complexities of iptables.

Installation:

If "ufw" is not already installed, set it up quickly using the following commands:

sudo apt-get update 
sudo apt-get install ufw

Enabling ufw:

Enabling "ufw" also enforces a "default deny" policy, blocking all incoming and outgoing traffic by default. To enable "ufw" and apply this policy, use:

sudo ufw enable

Check the current status of your "ufw" rules with:

sudo ufw status

Basic Rule Management:

"ufw" provides a straightforward syntax for managing firewall rules. Allow or deny specific connections using port numbers. For example, to allow incoming traffic on port 80 (HTTP), use:

sudo ufw allow 80

To deny connections on a specific port, replace 'allow' with 'deny':

sudo ufw deny <port_number>

Remember to reload the firewall after making changes:

sudo ufw reload

ufw and SSH:

If you're using SSH to remotely access your Linux system, "ufw" makes it easy to allow SSH traffic. By default, SSH uses port 22. To enable SSH connections, use:

sudo ufw allow 22

Make sure to reload the firewall:

sudo ufw reload

Allowing Secure Traffic on Port 443:

For secure web traffic using HTTPS, commonly on port 443, you can allow connections with:

sudo ufw allow 443

Remember to reload the firewall after making changes:

sudo ufw reload

Default Configurations:

Many Linux distributions, including Ubuntu, often include default configurations that allow traffic on common ports like 80 for HTTP and 443 for HTTPS. This facilitates the seamless operation of web servers like Apache or Nginx without additional user intervention.

Logging

Enabling logging in `ufw` and reviewing the logs allows you to monitor and identify potentially malicious or unwanted connection attempts, providing valuable information for securing your system.

1. Enable Logging:
To enable logging, use the following command:

sudo ufw logging on
 This command turns on logging for denied connections. The logs will be written to '/var/log/ufw.log' by default.

2. Check the Status:
You can check the status of 'ufw' to ensure that logging is enabled:

sudo ufw status
The output should now include information about logging being enabled.

3. Generate a Denied Connection:
To generate a denied connection for testing purposes, you can try to access a service or port that is not allowed by your firewall. For example, if you've denied incoming SSH connections (port 22), try to SSH into your server:

ssh username@your_server_ip
Since you've denied the connection, it should be logged.

4. Check the Logs:
After attempting the denied connection, you can check the logs:

cat /var/log/ufw.log
This command displays the contents of the `ufw.log` file, showing entries related to denied connections.

5. Review the Log Entries:
The log entries will contain information about the denied connection, including source and destination IP addresses, timestamps, and more. For example:

[UFW BLOCK] IN=eth0 OUT= MAC=xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx SRC=192.168.1.2 DST=192.168.1.1 LEN=40 TOS=0x00 PREC=0x00 TTL=64 ID=12345 PROTO=TCP SPT=12345 DPT=22 WINDOW=65535 RES=0x00 SYN URGP=0
This log entry provides details about a denied connection attempt on port 22 (SSH) from source IP 192.168.1.2 to destination IP 192.168.1.1.

Keep in mind that log formats and locations may vary based on your system's configuration and syslog settings.
Topics

Mastering Nginx

27 articles

Bash script

2 articles

Crontab

2 articles