Back to Insights
VoIP Engineering

How to Set Up a coturn STUN/TURN Server for WebRTC and VoIP

Telephony Engineering Team
4 min read
How to Set Up a coturn STUN/TURN Server for WebRTC and VoIP

A STUN server tells a client its public address, and a TURN server relays media when a direct path is impossible. coturn implements both, and it is what most WebRTC deployments run. If users on corporate networks or mobile data cannot connect, a correctly configured TURN server is usually the fix.

STUN vs TURN in One Minute

  • STUN answers "what address do you see me as?" It is cheap and carries no media.
  • TURN allocates a relay address on the server and forwards media through it. It works through strict NAT and firewalls but uses your bandwidth.

ICE tries direct paths first, then STUN-derived ones, and falls back to TURN. The full process is in NAT traversal with STUN, TURN, and ICE.

Installation and Core Configuration

Install coturn from your distribution, enable the service, and edit /etc/turnserver.conf:

listening-port=3478
tls-listening-port=5349
listening-ip=0.0.0.0
external-ip=203.0.113.10        # public IP (use PUBLIC/PRIVATE if behind NAT)
realm=turn.example.com
fingerprint
lt-cred-mech                      # long-term credentials
use-auth-secret
static-auth-secret=CHANGE_THIS_LONG_RANDOM_SECRET
min-port=49152
max-port=65535
cert=/etc/letsencrypt/live/turn.example.com/fullchain.pem
pkey=/etc/letsencrypt/live/turn.example.com/privkey.pem
no-multicast-peers
no-cli

Use a real, long random secret. Renew the certificate automatically and reload coturn when it changes.

Time-Limited Credentials

With use-auth-secret, your application generates short-lived usernames and passwords for each session: the username is an expiry timestamp (optionally with a user ID), and the password is an HMAC of that username with the shared secret. The browser receives them in its ICE server configuration. This avoids shipping a permanent password in client code.

Firewall Ports

PortProtocolPurpose
3478UDP and TCPSTUN/TURN
5349TCP (TLS)TURN over TLS, which passes the strictest firewalls
443 (optional)TCPTURN over TLS on the HTTPS port for restrictive networks
49152 to 65535UDPRelay allocations

Security Hardening

  • Deny relaying to private and loopback ranges (denied-peer-ip entries) so TURN cannot be abused to reach your internal network
  • Set user-quota and total-quota to limit abuse
  • Do not run an open relay. Always require authentication.
  • Keep coturn updated and monitor bandwidth use (see VoIP security best practices)

Testing

  1. Use a trickle ICE test page to confirm that relay candidates appear.
  2. From a restrictive network, force relay-only ICE policy in a test client and place a call.
  3. Check coturn logs for allocation and traffic.
  4. Confirm that expired credentials are rejected.

Capacity Planning

Each relayed call consumes bandwidth in both directions. Estimate relay share conservatively. Many deployments relay only a minority of calls, but the percentage is highly variable by user population. Place servers near users to limit latency (see VoIP latency).


Need reliable WebRTC connectivity in the real world? Our WebRTC development team builds and operates TURN infrastructure, signaling, and SIP bridges.

Discussion

0

Join the conversation

Sign in with your preferred account to comment, reply, and keep the discussion useful for other engineers.

Takes a few seconds. No separate password required.

C

Controva LLC

Engineering Team

We build digital empires that scale. Specializing in high-performance web architecture, AI automation, and headless commerce.

Work with us

Share this article