Skip to main content

UFW (Uncomplicated Firewall) Command Reference

Basic UFW Operations

  • Enable the Firewall
    sudo ufw enable
    
  • Disable the Firewall
    sudo ufw disable
    
  • Check Firewall Status
    sudo ufw status
    

Allowing Traffic

  • Allow Incoming Traffic on a Specific Port (e.g., SSH)
    sudo ufw allow <port_number>/tcp
    
  • Allow Incoming Traffic on a Specific Port and Protocol (e.g., UDP)
    sudo ufw allow <port_number>/udp
    
  • Allow Incoming Traffic from a Specific IP Address
    sudo ufw allow from <ip_address>
    
  • Allow Incoming Traffic from a Specific IP Address and Port
    sudo ufw allow from <ip_address> to any port <port_number>
    
  • Allow Incoming Traffic from a Specific IP Range
    sudo ufw allow from <ip_range>
    
  • Allow Outgoing Traffic on a Specific Port (e.g., HTTP)
    sudo ufw allow out <port_number>/tcp
    
  • Allow a Specific Application/Service (e.g., Apache Full)
    sudo ufw allow 'Apache Full'
    

Denying Traffic

  • Deny Incoming Traffic on a Specific Port (e.g., FTP)
    sudo ufw deny <port_number>/tcp
    
  • Deny Incoming Traffic from a Specific IP Address
    sudo ufw deny from <ip_address>
    
  • Limit the Rate of Incoming Connections (e.g., SSH)
    sudo ufw limit <port_number>/tcp
    

Managing Rules

  • Delete a Specific Rule by Its Rule Number
    sudo ufw delete <rule_number>
    
  • Reset UFW to Default Settings
    sudo ufw reset
    
  • Show Advanced UFW Options
    sudo ufw --help
    
  • Show the List of Applications/Services That Can Be Enabled
    sudo ufw app list
    
  • Enable a Specific Application/Service (e.g., OpenSSH)
    sudo ufw allow OpenSSH
    
  • Disable a Specific Application/Service (e.g., OpenSSH)
    sudo ufw delete allow OpenSSH
    
  • Enable Logging of UFW Actions
    sudo ufw logging on
    
  • Disable Logging of UFW Actions
    sudo ufw logging off
    
  • Reset UFW to Factory Defaults (Disable and Delete All Rules)
    sudo ufw --force reset
    

Example Commands for Apache and Nginx

  • Allow Apache Traffic
    sudo ufw allow 'Apache Full'
    sudo ufw delete allow 'Apache'
    
  • Allow Nginx Traffic
    sudo ufw allow 'Nginx Full'
    sudo ufw delete allow 'Nginx HTTP'
    
  • Allow Specific Port (e.g., SSH)
    sudo ufw allow 22/tcp
    
  • Deny Specific Port (e.g., HTTP)
    sudo ufw deny 80/tcp
    

iptables Command Reference

Basic iptables Operations

  • List All Rules
    sudo iptables -L
    
  • Flush (Reset) All Rules
    sudo iptables -F
    

Managing Rules

  • Allow Incoming SSH Connections (Port 22)
    sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
    
  • Allow Incoming Connections from a Specific IP Address
    sudo iptables -A INPUT -p tcp -s 192.168.1.100 -j ACCEPT
    
  • Deny Incoming Connections from a Specific IP Address
    sudo iptables -A INPUT -p tcp -s 192.168.1.200 -j DROP
    
  • Delete a Specific Rule by Its Line Number
    sudo iptables -D INPUT [line number]
    

This docs provides a quick reference for managing firewall rules with UFW and iptables. Make sure to adjust the port numbers, IP addresses, and service names according to your specific requirements.