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
| Port | Protocol | Purpose |
|---|---|---|
| 3478 | UDP and TCP | STUN/TURN |
| 5349 | TCP (TLS) | TURN over TLS, which passes the strictest firewalls |
| 443 (optional) | TCP | TURN over TLS on the HTTPS port for restrictive networks |
| 49152 to 65535 | UDP | Relay allocations |
Security Hardening
- Deny relaying to private and loopback ranges (
denied-peer-ipentries) so TURN cannot be abused to reach your internal network - Set
user-quotaandtotal-quotato limit abuse - Do not run an open relay. Always require authentication.
- Keep coturn updated and monitor bandwidth use (see VoIP security best practices)
Testing
- Use a trickle ICE test page to confirm that relay candidates appear.
- From a restrictive network, force relay-only ICE policy in a test client and place a call.
- Check coturn logs for allocation and traffic.
- 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.
Related Services
WebRTC Development
Browser and mobile real-time voice and video, bridged to SIP infrastructure.
Hire WebRTC Developers
WebRTC engineers for softphones, click-to-call and gateways that bridge browsers to SIP.
Custom VoIP Development
Purpose-built VoIP platforms: multi-tenant, white-label, and carrier-direct.
Keep Reading
Discussion
0Join 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.
Controva LLC
Engineering Team
We build digital empires that scale. Specializing in high-performance web architecture, AI automation, and headless commerce.
Work with us