Managing Ports on Ubuntu and Debian-based Systems
This guide provides concise steps to manage ports on Ubuntu and other Debian-based systems. Ports control network traffic and security. Below are methods to allow or block ports using tools compatible with most Debian derivatives.
Prerequisites
- A Debian-based operating system (e.g., Ubuntu, Linux Mint, Debian).
- Sudo or root privileges to execute commands.
Methods to Allow or Block Ports
1. Using UFW (Uncomplicated Firewall)
UFW is the default, user-friendly firewall management tool on Ubuntu and works seamlessly on most Debian-based systems.
Steps:
Check if UFW is Installed and Active:
sudo apt update
sudo apt install ufw
Enable UFW:
sudo ufw enable
Allow a Port (e.g., 80):
sudo ufw allow 80
Block a Port (e.g., 22):
sudo ufw deny 22
Allow a Port with a Specific Protocol (e.g., 443 with TCP):
sudo ufw allow 443/tcp
Check Active Rules:
sudo ufw status
Remove a Rule (e.g., remove port 80):
sudo ufw delete allow 80
2. Using iptables
For advanced users, iptables
provides fine-grained control over network traffic. It is compatible with Ubuntu and other Debian-based systems.
Steps:
Allow a Port (e.g., 8080):
sudo iptables -A INPUT -p tcp --dport 8080 -j ACCEPT
Block a Port (e.g., 3306):
sudo iptables -A INPUT -p tcp --dport 3306 -j DROP
Delete a Rule (e.g., unblock port 8080):
sudo iptables -D INPUT -p tcp --dport 8080 -j ACCEPT
Save Changes to Persist Rules Across Reboots:
sudo apt install iptables-persistent
sudo netfilter-persistent save
3. Using FirewallD
FirewallD is a dynamic firewall management tool compatible with most modern Linux distributions, including Ubuntu and Debian derivatives.
Steps:
Install and Enable FirewallD:
sudo apt update
sudo apt install firewalld
sudo systemctl start firewalld
sudo systemctl enable firewalld
Allow a Port (e.g., 22):
sudo firewall-cmd --permanent --add-port=22/tcp
sudo firewall-cmd --reload
Block a Port (e.g., 8080):
sudo firewall-cmd --permanent --remove-port=8080/tcp
sudo firewall-cmd --reload
List Current Rules:
sudo firewall-cmd --list-all
Summary
- UFW: Simplified configuration; ideal for most users.
- Iptables: Advanced configurations; for experienced users.
- FirewallD: Dynamic and robust; suited for evolving setups.
This guide applies to Ubuntu, Debian, and other Debian-based operating systems
CrownCloud - Get a SSD powered KVM VPS at $4.5/month!
Use the code WELCOME
for 10% off!
1 GB RAM / 25 GB SSD / 1 CPU Core / 1 TB Bandwidth per month
Available Locations: LAX | MIA | ATL | FRA | AMS