0Pricing
Linux Server Deployment & SSH Mastery · Lesson

Port Management & Security

Open and close specific ports for services, implement port forwarding, and understand common attack vectors related to open ports.

Port Management & Security is a free Linux Server Deployment & SSH Mastery lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Linux Server Deployment & SSH Mastery learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Are Network Ports?

Imagine a server as an apartment building. Each apartment has a number, and visitors need to know which apartment number to go to. In networking, these 'apartment numbers' are called ports.

Network ports are communication endpoints. They help your server direct incoming network traffic to the correct service or application. Each port is assigned a unique number, ranging from 0 to 65535.

Common Port Numbers

Many services use standard, well-known port numbers. Knowing these helps you identify services and configure firewalls.

  • Port 22: Used for SSH (Secure Shell) for remote access.
  • Port 80: Used for HTTP (Hypertext Transfer Protocol) for standard web traffic.
  • Port 443: Used for HTTPS (HTTP Secure) for encrypted web traffic.
  • Port 53: Used for DNS (Domain Name System) name resolution.

These are just a few examples; thousands of ports exist for various applications.

Checking Listening Ports

Before you open new ports, it's good to know which ones are already active on your server. The ss command (socket statistics) is a modern tool to inspect network sockets.

The -ltn options show listening TCP sockets numerically without resolving hostnames.

ss -ltn

Managing Ports with UFW

UFW (Uncomplicated Firewall) is a popular firewall management tool on Ubuntu and other Debian-based systems. It simplifies configuring iptables rules.

To allow or deny traffic, you specify the port number and protocol (TCP or UDP). Remember to reload or enable UFW after changes.

sudo ufw allow 80/tcp
sudo ufw deny 22
sudo ufw enable

Managing Ports with firewalld

On CentOS, RHEL, and Fedora systems, firewalld is the default firewall management utility. It uses 'zones' to manage different network interfaces.

You add rules to a zone, typically the public zone for internet-facing services, and then reload firewalld to apply changes.

sudo firewall-cmd --zone=public --add-port=443/tcp --permanent
sudo firewall-cmd --zone=public --remove-port=22/tcp --permanent
sudo firewall-cmd --reload

Introducing Port Forwarding

Port forwarding is a technique that redirects network traffic from one port to another. This can be on the same machine, or from one machine to another.

It's often used to allow external devices to access services on a private network, or to tunnel traffic securely through an intermediary server.

Local Port Forwarding with SSH

One common type is Local Port Forwarding using SSH. This creates a secure tunnel from your local machine to a remote server, and then from that remote server to another target host and port (which could even be the remote server itself).

It's useful for accessing services on a remote private network that aren't directly exposed to the internet, like a database or an internal web application.

SSH Local Forwarding Demo

Here's how you can use SSH local port forwarding. This command forwards traffic from your local machine's port 8080 to port 80 on the localhost (the remote server itself) through the SSH connection to your_server_ip.

Once connected, you can access the remote web server (on port 80) by browsing to http://localhost:8080 on your local machine.

ssh -L 8080:localhost:80 user@your_server_ip

Port Security Best Practices

Managing ports is crucial for server security. Here are key practices:

  • Principle of Least Privilege: Only open ports that are absolutely necessary for your services to function.
  • Regular Audits: Periodically check your firewall rules and active listening ports to ensure no unauthorized ports are open.
  • Strong Passwords/Keys: For services like SSH, always use strong authentication.
  • Rate Limiting: Consider limiting connection attempts to critical ports (like SSH) to prevent brute-force attacks.

Quick Check: Port Management

Which of the following are considered good security practices when managing server ports?

Recap: Ports & Security

In this lesson, we explored network ports, which are essential for directing traffic to server services. We learned about common port numbers and how to check listening ports with ss.

You now know how to manage firewall rules using UFW and firewalld, and understand the concept of SSH local port forwarding. Crucially, we covered best practices for port security to keep your server safe.

Frequently asked questions

Is the “Port Management & Security” lesson free?

Yes — the full text of “Port Management & Security” is free to read here on the web, and the Linux Server Deployment & SSH Mastery course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Linux Server Deployment & SSH Mastery course, upgrade to CoddyKit PRO.

What will I learn in “Port Management & Security”?

Open and close specific ports for services, implement port forwarding, and understand common attack vectors related to open ports. You practise Linux Server Deployment & SSH Mastery with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Linux Server Deployment & SSH Mastery?

No prior experience is required. Linux Server Deployment & SSH Mastery on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Port Management & Security” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Linux Server Deployment & SSH Mastery lesson?

Yes. Every Linux Server Deployment & SSH Mastery lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Basic Network Configuration
  2. Understanding Firewalls (UFW/firewalld)
  3. Port Management & Security
  4. DNS Configuration and Troubleshooting
← Back to Linux Server Deployment & SSH Mastery