How to Open Ports in Linux

Allowing ports in Linux can vary based on the firewall you are using. The two most common firewalls are iptables and ufw (Uncomplicated Firewall). Below are the steps for both methods.

Using UFW (Uncomplicated Firewall)

1. Check UFW Status

Before making any changes, check if UFW is active:

sudo ufw status

2. Allow a Specific Port

To allow a specific port (for example, port 22 for SSH):

sudo ufw allow 22

To allow a different port (e.g., port 3389 for RDP):

sudo ufw allow 3389

3. Allow a Port with a Specific Protocol

You can specify the protocol (TCP or UDP) as follows:

sudo ufw allow 3389/tcp

4. Enable UFW (if not already enabled)

If UFW is not enabled, you can enable it with:

sudo ufw enable

You have successfully allowed the port using UFW!

Using iptables

1. Check Existing Rules

Before making changes, list the current iptables rules:

sudo iptables -L -n

2. Allow a Specific Port

To allow a specific port (e.g., port 22 for SSH):

sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT

To allow another port (e.g., port 3389 for RDP):

sudo iptables -A INPUT -p tcp --dport 3389 -j ACCEPT

3. Save Your Changes

To save your changes so that they persist after a reboot:

  • On Debian/Ubuntu systems:

sudo iptables-save | sudo tee /etc/iptables/rules.v4
  • On Red Hat/CentOS systems:

service iptables save

Tip: Always backup your current iptables rules before making changes.

Conclusion

By following these steps, you can successfully allow ports in Linux using either UFW or iptables. Always ensure to apply best security practices when configuring your firewall to protect your system.

Opening ports can expose your system to security risks. Ensure you are allowing only the necessary ports and use strong security practices.

Last updated

Was this helpful?