> ## Documentation Index
> Fetch the complete documentation index at: https://docs.edisglobal.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Basic UFW setup guide for Debian/Ubuntu

> Set up and configure UFW (Uncomplicated Firewall) on Debian and Ubuntu, covering basic commands, rule management, and firewall best practices.

## Introduction to UFW

UFW (Uncomplicated Firewall) is a user-friendly interface for managing `iptables` firewall rules on Linux systems. It's particularly well-suited for Debian and Ubuntu servers, providing a simplified way to configure your system's firewall.

## Installation

Install UFW using the following command:

```bash theme={"system"}
sudo apt update
sudo apt install ufw
```

## Basic UFW commands

### Check UFW status

```bash theme={"system"}
sudo ufw status
```

This command shows if UFW is active and lists all current rules.

For a more detailed view, use:

```bash theme={"system"}
sudo ufw status verbose
```

### Enable and disable UFW

<Warning>
  Before enabling UFW, ensure you have configured rules to allow SSH access (port 22)
  to prevent being locked out of your server.
</Warning>

Allow your **actual SSH TCP port first** (replace `22` if needed), then enable UFW:

```bash theme={"system"}
sudo ufw allow 22/tcp
sudo ufw enable
```

To disable UFW:

```bash theme={"system"}
sudo ufw disable
```

## Managing basic rules

### Allow incoming connections

Allow specific ports:

```bash theme={"system"}
sudo ufw allow 22    # Allow SSH
sudo ufw allow 80    # Allow HTTP
sudo ufw allow 443   # Allow HTTPS
```

Allow specific services by name:

```bash theme={"system"}
sudo ufw allow ssh
sudo ufw allow http
sudo ufw allow https
```

After activation, verify a new SSH connection before closing your current session. See the [UFW walkthrough](/advanced-setup-guides/ufw-uncomplicated-firewall-howto) for a complete sequence.

### Deny incoming connections

Deny specific ports:

```bash theme={"system"}
sudo ufw deny 23     # Deny Telnet
```

### Delete rules

Delete rules by number:

1. List rules with numbers:

```bash theme={"system"}
sudo ufw status numbered
```

2. Delete a specific rule:

```bash theme={"system"}
sudo ufw delete [number]
```

Delete rules by specification:

```bash theme={"system"}
sudo ufw delete allow 80
```

## Advanced configuration

### Allow specific IP addresses

Allow a specific IP address:

```bash theme={"system"}
sudo ufw allow from 203.0.113.4
```

Allow an IP address to access a specific port:

```bash theme={"system"}
sudo ufw allow from 203.0.113.4 to any port 22
```

### Configure default policies

Set default policies for incoming and outgoing traffic:

```bash theme={"system"}
sudo ufw default deny incoming
sudo ufw default allow outgoing
```

## Best practices

1. **Always allow SSH first**
   Before enabling UFW, ensure SSH access is allowed:
   ```bash theme={"system"}
   sudo ufw allow ssh
   ```

2. **Use specific rules**
   Instead of allowing all traffic to a port, specify the service and protocol:
   ```bash theme={"system"}
   sudo ufw allow 80/tcp
   ```

3. **Regular audits**
   Regularly review your firewall rules:
   ```bash theme={"system"}
   sudo ufw status verbose
   ```

4. **Back up rules**
   Back up your UFW rules periodically:
   ```bash theme={"system"}
   sudo cp -a /etc/ufw /root/ufw-backup
   ```

## Common configuration examples

### Basic web server setup

```bash theme={"system"}
# Allow SSH, HTTP, and HTTPS
sudo ufw allow ssh
sudo ufw allow http
sudo ufw allow https
```

### Database server setup

```bash theme={"system"}
# Allow MySQL/MariaDB from specific IP
sudo ufw allow from 203.0.113.4 to any port 3306
```

### Rate limiting

Enable rate limiting for SSH to prevent brute force attacks:

```bash theme={"system"}
sudo ufw limit ssh
```

## Troubleshooting

If you encounter issues:

1. **Check UFW status**
   ```bash theme={"system"}
   sudo ufw status verbose
   ```

2. **View UFW logs**
   ```bash theme={"system"}
   sudo tail -f /var/log/ufw.log
   ```

3. **Reset UFW**
   Resetting UFW deletes configured rules and disables it. Use this only when deliberately rebuilding the firewall with console access available:
   ```bash theme={"system"}
   sudo ufw reset
   ```

## Conclusion

UFW provides a straightforward way to manage your firewall rules on Debian and Ubuntu systems. Remember to always configure SSH access before enabling the firewall, and regularly audit your rules to maintain security. For more advanced configurations, consult the [UFW documentation](https://help.ubuntu.com/community/UFW).


## Related topics

- [UFW (Uncomplicated Firewall)](/advanced-setup-guides/ufw-uncomplicated-firewall-howto.md)
- [Post-install scripts (deprecated)](/vps-management/post-install-scripts.md)
- [Configure additional IP on a Linux VPS](/faq/ip-addresses/configuring-additional-ip-address-linux.md)
- [SSH security hardening guide](/getting-started/ssh-security-hardening-guide.md)
- [GUI versions of Ubuntu or Debian](/advanced-setup-guides/gui-versions-of-ubuntu-debian.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.