Coturn Guide

Coturn Server: Basic Setup & Usage

A quick guide for your team to get started with Coturn on your VPS.

1. What is Coturn?

Coturn is a free open-source implementation of a STUN (Session Traversal Utilities for NAT) and TURN (Traversal Using Relays around NAT) server. It's primarily used in WebRTC (Web Real-Time Communication) applications.

Essentially, Coturn helps WebRTC applications establish connections between users even when they are behind complex network configurations.

2. Why Do We Need It?

When building applications that use WebRTC (like video/audio chat, file sharing, etc.), users are often behind NATs (Network Address Translators), which can prevent direct connections.

3. Basic Configuration (turnserver.conf)

Coturn is configured via the /etc/turnserver.conf file (or /usr/local/etc/turnserver.conf depending on installation). Here’s a basic setup:

# --- Listening IPs and Ports ---
# IPs the TURN server listens on. Use your VPS's public IP.
# If you have multiple IPs, you can list them.
listening-ip=YOUR_VPS_PUBLIC_IP

# Port for STUN/TURN. Default is 3478 for UDP and TCP.
listening-port=3478

# TLS listener port (for TURNS). Default is 5349.
# You'll need SSL certificates for this.
# tls-listening-port=5349

# --- External IP ---
# The public IP address that clients will use to connect to the server.
# If you have one public IP, Coturn can often detect it.
# If behind NAT or for clarity, specify it:
external-ip=YOUR_VPS_PUBLIC_IP
# or if Coturn is on a private IP behind a NAT router:
# external-ip=YOUR_VPS_PUBLIC_IP/YOUR_VPS_PRIVATE_IP

# --- Realm and Server Name ---
# Realm is used for authentication. Choose something unique to your service.
realm=yourdomain.com
server-name=turn.yourdomain.com

# --- Authentication ---
# For simplicity, we'll use static user accounts.
# Format: user=password
user=webrtc_user:SomeStrongPassword123
user=another_user:AnotherSecurePass456

# For more security, use long-term credentials mechanism (more complex setup)
# or disable auth for STUN only (not recommended for TURN).
# no-auth (allows STUN requests without auth - generally okay)
# stun-only (disables TURN, only STUN - useful if you have separate TURN)

# --- Logging ---
log-file=/var/log/turnserver.log
verbose # More detailed logging, good for troubleshooting
# no-stdout-log # Uncomment to disable logging to stdout if running as a service

# --- Relay Ports ---
# Range of ports for relaying media. Ensure your firewall allows these.
min-port=49152
max-port=65535

# --- Security (Optional but Recommended) ---
# If using TLS (highly recommended for production):
# cert=/etc/letsencrypt/live/yourdomain.com/fullchain.pem
# pkey=/etc/letsencrypt/live/yourdomain.com/privkey.pem
# no-tlsv1
# no-tlsv1_1

# Deny connections from bogon (unroutable) IP addresses
no-bogon

# Consider mobility for ICE-TCP (RFC6062) if needed by clients
# mobility
            

Important: Replace YOUR_VPS_PUBLIC_IP, yourdomain.com, and credentials with your actual values. Ensure the relay port range (min-port to max-port) is open in your VPS firewall for UDP traffic.

4. Running Coturn

How you run Coturn depends on how it was installed.

As a Service (Systemd - Common on modern Linux):

  1. Enable the configuration: Some distributions require you to uncomment a line in /etc/default/coturn to tell it to use the turnserver.conf file. Open it and look for a line like TURNSERVER_ENABLED=1 or ensure it's not set to 0.
  2. Start Coturn:
    sudo systemctl start coturn
  3. Enable on boot:
    sudo systemctl enable coturn
  4. Check status:
    sudo systemctl status coturn
    You should see it active and running. Check the logs (/var/log/turnserver.log) for any errors.

Manually (for testing or if not using systemd):

sudo turnserver -c /etc/turnserver.conf -o -v

5. Testing Your Coturn Server

The best way to test is using a WebRTC application or a dedicated testing tool.

Firewall: Double-check your VPS firewall (e.g., ufw, firewalld, or cloud provider's firewall). You need to allow UDP and TCP traffic on port 3478 (or your configured listening-port) and the UDP relay port range (49152-65535 by default).

Example UFW commands:

sudo ufw allow 3478/udp
sudo ufw allow 3478/tcp
sudo ufw allow 49152:65535/udp
sudo ufw reload

6. Next Steps & Security Considerations

This guide provides a starting point. Coturn has many more configuration options for advanced scenarios. Always refer to the official Coturn documentation for detailed information.