#### Prerequisites
Before you begin, ensure you have:
- A server running Ubuntu (This guide is tested with Ubuntu 22.04)
- A user account with sudo privileges
- Basic familiarity with the command line
#### Step 1: Check UFW Status
Check if UFW is currently active on your system:
```bash
sudo ufw status
```
If UFW is inactive, you will see "Status: inactive."
#### Step 2: Enable UFW
Enable UFW to activate the firewall:
```bash
sudo ufw enable
```
You will receive a message confirming that UFW is enabled and will be active after the next reboot.
#### Step 3: Understand Default Policies
UFW operates on default policies for incoming and outgoing traffic:
- **Incoming:** By default, UFW denies all incoming connections.
- **Outgoing:** By default, UFW allows all outgoing connections.
These defaults provide a basic level of security. You'll refine these rules in the following steps.
#### Step 4: Allow SSH Connections (Important!)
**Before making any further changes, allow SSH connections to avoid locking yourself out of your server.**
```bash
sudo ufw allow ssh
```
This rule permits incoming connections on port 22, which is the default port for SSH. **If you use a different port for SSH, adjust the rule accordingly.**
#### Step 5: Allow HTTP and HTTPS Traffic (If Necessary)
If your server hosts websites or web applications, allow HTTP (port 80) and HTTPS (port 443) traffic:
```bash
sudo ufw allow http
sudo ufw allow https
```
#### Step 6: Allow Custom Ports (If Needed)
If you need to allow connections on other specific ports for applications like a database server or game server, use the `allow` command with the port number:
```bash
sudo ufw allow
/tcp # For TCP traffic
sudo ufw allow /udp # For UDP traffic
```
**Replace `` with the actual port number.**
#### Step 7: Deny Specific Traffic (Optional)
You can explicitly deny traffic from specific IP addresses or port ranges if needed:
```bash
sudo ufw deny from to any
sudo ufw deny /tcp
```
**Replace `` and `` with the relevant values.**
#### Step 8: Check the Firewall Rules
After configuring your rules, it's good practice to review them:
```bash
sudo ufw status verbose
```
This command provides a detailed output of the active UFW rules.
#### Step 9: Disable UFW (If Necessary)
If you need to temporarily disable UFW, use the following command:
```bash
sudo ufw disable
```
**Caution:** Disabling your firewall exposes your system to security risks. Only disable UFW if absolutely necessary and re-enable it as soon as possible.
#### Step 10: Reset UFW (Use with Caution)
If you need to start fresh and reset UFW to its default settings, use:
```bash
sudo ufw reset
```
**Warning:** This command deletes all existing UFW rules. Use this option with extreme caution.
#### Conclusion
You have learned the basics of UFW firewall configuration, enabling you to enhance your Ubuntu server's security. Regularly review and update your firewall rules to adapt to changing security needs and keep your system protected.
**Additional Resources:**
- **UFW Documentation:** [https://help.ubuntu.com/community/UFW](https://help.ubuntu.com/community/UFW)
- **Ubuntu Security Guide:** [https://ubuntu.com/security](https://ubuntu.com/security)