Port forwarding on a dedicated server means opening a specific network port so internet traffic can reach a service on your machine. Unlike a home network where a router performs NAT, a dedicated server has a public IP directly, so port forwarding happens at the server's own firewall. This guide covers configuration across UFW, firewalld, and iptables, security best practices, and troubleshooting.

Key takeaways

  • Port forwarding on a dedicated server means writing a firewall rule, not configuring a home-router NAT.
  • UFW, firewalld, and iptables all do the same job with different syntax, choose based on your distribution.
  • With a firewall enabled on a default-deny policy, every port stays closed until you open it; always verify a new rule from outside the network.
  • A VPN tunnel or reverse proxy is often safer for services that don't need public exposure.
  • Never forward database ports (MySQL 3306, PostgreSQL 5432) to the internet, use SSH tunnels instead.

What is port forwarding on a dedicated server?

A firewall rule that lets outside traffic reach a service listening locally. Since your server has a public IP directly (no home-router NAT to traverse), "port forwarding" simply means opening a port in the server's firewall so inbound traffic on a given TCP or UDP port reaches the application listening on it. True forwarding, where the firewall rewrites the destination address or port (DNAT), only comes in when traffic must reach a different port, or a virtual machine or container on a private network behind the server. If the server also runs IPv6 alongside IPv4, check that each rule covers both protocols.

Your Kimsufi server has a public IP address, and anti-DDoS protection from the OVHcloud network is included. That protection does not decide which ports your services expose: your server's firewall is the gatekeeper.

Critical security warnings

Read this before opening any port. Every forwarded port is a potential entry point for attackers.

  1. Ask: does this service really need internet exposure?
  2. Use long, unique passwords for each service, or key-based authentication where the service supports it.
  3. Enable two-factor authentication where available.
  4. Keep software updated and apply security patches promptly.
  5. Close ports immediately when no longer needed.

Services you should NEVER forward without expert guidance:

ServicePortRiskSafe Alternative
MySQL3306Brute force exposureSSH tunnel
PostgreSQL5432Brute force exposureSSH tunnel
MongoDB27017Notorious for data breachesVPN + firewall
RDP3389High-value ransomware targetVPN only
VNC5900Weak authentication protocolsVPN only

Firewall configuration: UFW, firewalld, and iptables

All three do the same job with different syntax. Choose the one matching your distribution.

UFW (Ubuntu default, available on Debian)

bash
# Allow SSH before anything else, or enabling the firewall locks you out
sudo ufw allow 22/tcp comment 'SSH'

# Open a port (example: Minecraft on TCP 25565)
sudo ufw allow 25565/tcp comment 'Minecraft Server'

# Open standard web ports
sudo ufw allow 80,443/tcp comment 'Web Server'

# Enable the firewall
sudo ufw enable

# Verify
sudo ufw status numbered

Once UFW is enabled, rules added with ufw allow apply straight away; sudo ufw reload is only needed after editing the files in /etc/ufw by hand. See the official UFW documentation for advanced configurations.

firewalld (CentOS, RHEL, Rocky, Fedora)

bash
# Open a port permanently
sudo firewall-cmd --permanent --add-port=25565/tcp

# Open standard web ports
sudo firewall-cmd --permanent --add-port={80,443}/tcp

# Reload to apply (--permanent rules are not active until you do)
sudo firewall-cmd --reload

# List open ports
sudo firewall-cmd --list-ports

See the firewalld documentation for zone-based configurations.

iptables (universal, advanced)

bash
# Allow inbound TCP on port 25565 (-I puts the rule at the top of the chain)
sudo iptables -I INPUT -p tcp --dport 25565 -j ACCEPT

# Same rule for IPv6 (iptables only covers IPv4)
sudo ip6tables -I INPUT -p tcp --dport 25565 -j ACCEPT

# Save rules persistently (Debian/Ubuntu)
sudo apt install iptables-persistent
sudo netfilter-persistent save

# Save rules (RHEL family, with the iptables-services package)
sudo sh -c 'iptables-save > /etc/sysconfig/iptables'
sudo sh -c 'ip6tables-save > /etc/sysconfig/ip6tables'

Prefer -I to -A: a rule appended after an existing DROP or REJECT rule is never reached. And sudo iptables-save > file fails, because the redirection runs without root rights, hence sh -c. On RHEL-family systems firewalld is the default; manage iptables directly only if you have disabled it. See the netfilter documentation for advanced chain management.

Common port forwarding scenarios

Game servers

GameDefault PortProtocolNotes
Minecraft25565TCPJava Edition server
Minecraft (Bedrock)19132UDPBedrock Edition server
Rust28015UDPGame port; also open the UDP query port to appear in the server browser, RCON uses TCP (Facepunch wiki)
ARK: Survival7777UDPGame port
Counter-Strike 227015TCP/UDPGame + RCON
Valheim2456-2457UDPGame + Steam query ports

Web servers on custom ports

bash
# UFW: Open port 8080
sudo ufw allow 8080/tcp comment 'Dev Web Server'

# firewalld
sudo firewall-cmd --permanent --add-port=8080/tcp
sudo firewall-cmd --reload

For production web hosting, see our hosting a website on a dedicated server guide.

Test a port from outside the server

A test run from the server itself proves little: a connection to your own public IP goes through the loopback interface, which most firewall configurations accept, so the port can look open while outside traffic is blocked. Check locally that the service is listening, then test from another machine:

bash
# On the server: is the service listening, and on which address?
sudo ss -tulpn | grep 25565

# From another machine (Linux or macOS): TCP connection test
nc -vz your-server-ip 25565

# From another machine: one TCP port, then one UDP port
nmap -p 25565 your-server-ip
sudo nmap -sU -p 19132 your-server-ip

A service listening only on 127.0.0.1 refuses outside connections whatever the firewall says; it must listen on 0.0.0.0, :: or the public IP. For UDP, nmap often reports open|filtered because many UDP services do not answer an empty probe: the reliable test is a connection from the real client, such as the game itself.

Troubleshooting: the 3 most common causes

  1. Rule not active: a firewalld rule added with --permanent only applies after firewall-cmd --reload, a UFW rule does nothing while ufw status says inactive, and an iptables rule that was not saved disappears at reboot.
  2. Service not listening: verify with sudo ss -tulpn | grep <port> that the service is running and listening on a public address, not only on 127.0.0.1.
  3. Protocol mismatch: you opened TCP but the service uses UDP, or vice versa.

Always allow SSH before enabling the firewall, or you'll lock yourself out:

bash
sudo ufw allow 22/tcp # Allow SSH first!
sudo ufw enable # Then enable

Security best practices

Use non-standard ports

Moving SSH off port 22 mainly reduces noise: many automated scans only try port 22, so fewer of their attempts reach sshd and your logs stay readable. It is not a security control by itself, since a full port scan finds the new port anyway. What protects SSH is key-based authentication with password login turned off (PasswordAuthentication no, see the sshd_config manual) and fail2ban against repeated attempts. If you do change the port, open the new one first and test it before closing port 22:

bash
# 1. Open the new port first
sudo ufw allow 22345/tcp comment 'SSH'

# 2. Set "Port 22345" in sshd_config, then check the syntax
sudo nano /etc/ssh/sshd_config
sudo sshd -t

# 3a. Apply on Ubuntu 24.04 and later (socket activation)
sudo systemctl daemon-reload
sudo systemctl restart ssh.socket

# 3b. Apply on Debian or Ubuntu 22.04
sudo systemctl restart ssh

# 4. From a second terminal, before closing your session
ssh -p 22345 user@your-server-ip

# 5. Only then remove the old rule
sudo ufw delete allow 22/tcp

On Ubuntu 24.04 and later, sshd is started through ssh.socket, so restarting the service alone leaves it on port 22; the daemon-reload is what applies the new port (Ubuntu bug 2069041). On RHEL, Rocky or Fedora, the service is called sshd, you open the port with firewall-cmd --permanent --add-port=22345/tcp then firewall-cmd --reload, and SELinux must allow the new port before the restart: sudo semanage port -a -t ssh_port_t -p tcp 22345.

Install fail2ban

fail2ban watches authentication failures and temporarily bans the addresses behind them. Put your settings in /etc/fail2ban/jail.local, not in jail.conf, which package updates overwrite. If you moved SSH, give the jail the new port, otherwise bans only block port 22:

bash
sudo apt install fail2ban
ini
# /etc/fail2ban/jail.local
[sshd]
enabled = true
port    = 22345
bash
# After creating or editing jail.local
sudo systemctl enable fail2ban
sudo systemctl restart fail2ban
sudo fail2ban-client status sshd

On a system that logs only to the systemd journal (no /var/log/auth.log), also add backend = systemd to the [sshd] section.

Document and audit open ports

bash
# Check what's listening (TCP and UDP, with process names)
sudo ss -tulpn

# Scan all TCP ports from an external machine (only scan servers you own)
nmap -sV -p- your-server-ip

Standard port assignments are recorded in the IANA registry.

When NOT to use port forwarding

ScenarioBetter AlternativeWhy
Temporary admin accessSSH tunnel (ssh -L 8080:localhost:80 user@server)No permanent port exposure
Private file accessWireGuard VPNEncrypted, one UDP port instead of one per service
Web service exposureReverse proxy (Nginx)Hides backend, adds TLS
Database accessSSH tunnelNever expose databases directly

If you go the VPN route, choosing between WireGuard and OpenVPN depends on your clients and network. Our firewall setup guide covers hardening before opening ports. For Anti-DDoS protection, see our Anti-DDoS solutions.

Conclusion

Port forwarding comes down to one firewall rule matched to the right port, protocol, and tool. Verify from outside the network, and use a VPN or reverse proxy when an application doesn't need public exposure.

Kimsufi is part of the OVHcloud group: every server comes with anti-DDoS protection included and full root access, so the firewall is entirely yours to configure, from $11.10/month on the KS range. Ready to get started? Browse our dedicated server range or read our Linux dedicated server config guide.

FAQ

How do I set up port forwarding on a dedicated server?

Write a firewall rule opening the specific port and protocol your service uses (UFW on Ubuntu, firewalld on RHEL-based systems, iptables anywhere). Then test from an external network to confirm the connection works.

Why is my port forwarding not working?

The three most common causes: (1) the rule is not active (firewalld rule added with --permanent but not reloaded, or UFW disabled), (2) the service is not listening on the port, or only on 127.0.0.1, check with sudo ss -tulpn, (3) protocol mismatch (TCP vs UDP). Check these in order, and always test from another machine.

Should I use UFW, firewalld, or iptables?

Use your distribution's default: UFW on Ubuntu, firewalld on CentOS/RHEL/Rocky/Fedora. iptables is universal and the most granular but has more complex syntax. All three achieve the same result.

Does changing the SSH port make my server more secure?

Not by itself. Moving SSH off port 22 mainly cuts log noise from automated scans that only try the default port; a full port scan still finds the new one. Key-based authentication with password login disabled, plus fail2ban, matters much more.

Is port forwarding safe?

Safe when done correctly: open only the specific port and protocol needed, use SSH keys rather than passwords, install fail2ban, keep software updated, and close ports when no longer needed. Never expose database ports directly.