Asterisk or FreePBX VPS Setup: SIP, RTP and Security
Run Asterisk or FreePBX for your VPS. Learn SIP trunks, RTP UDP port ranges, firewall rules to block scanners, and how toll fraud starts.
Wetin self-hosted VoIP server be
Self-hosted VoIP server na SIP server wey dey run for VPS wey you control. Because of that, na your own machine dey route your calls instead of hosted phone service. VoIP (voice over IP) dey carry speech as UDP packets. SIP (session initiation protocol) na the signalling wey dey set up and end call. Audio no dey pass through SIP. Na this fact dey cause most of the wahala wey follow.
Four parts dey make the system work.
- The PBX (private branch exchange) software. Asterisk na the usual choice. E dey keep the extensions and dialplan.
- The endpoints. Desk phones or softphones wey register with the PBX using username and secret.
- The SIP trunk. Na paid account with provider wey connect you to public telephone network and rent real phone numbers give you.
- The media path. RTP (real-time transport protocol) flows dey carry audio through their own UDP ports.
Self-hosting the PBX no mean say you self-host phone numbers. Carrier dey provide the numbers, and you still dey pay for each number and each minute. Wetin you own na call routing, voicemail, recordings, and extension list. You also own the security responsibility for service wey people dey attack to collect money.
VoIP server wey you host by yourself need which ports?
SIP signalling dey use port 5060 for UDP and TCP, and port 5061 for SIP over TLS (transport layer security). Na call setup only dem ports dey carry. Audio for each call na separate UDP flow, and e dey go to one port wey e take from RTP range. Asterisk ships one sample rtp.conf wey dey set rtpstart=10000 and rtpend=20000, and the defaults wey dem compile inside am na 5000 and 31000. Each call dey take two ports from that range: one for RTP and one for RTCP (RTP control protocol).
Na this separation dey make most first attempts fail. Call go connect, both phones go show say dem answer am, but nobody go hear anything because firewall allow 5060 and drop every RTP packet. Signalling and media na separate flows, so dem need separate firewall rules. If this difference new to you, how ports and listening sockets dey work for Linux go help you read before you open any port.
Reduce the range before you open am. Twenty thousand ports plenty pass wetin small system need. Two ports for each call mean say range of two hundred fit handle one hundred calls at once.
[general]
rtpstart=10000
rtpend=10200Apply am with sudo asterisk -rx "core reload".
You suppose install Asterisk or FreePBX?
Asterisk na the engine. You configure am with text files for /etc/asterisk and you write the dialplan by yourself. FreePBX na web interface wey dem write with PHP and JavaScript. E dey sit on top Asterisk, generate those files for you, and add modules for voicemail and call queues.
The main difference for VPS na who get control of the machine. As of October 2026, the official FreePBX 17 installer expect plain Debian 12 system, and e install Asterisk, web server, database server and PHP. If you point am to machine wey already dey run other services, the result no go make you happy. Give FreePBX its own VPS.
wget https://github.com/FreePBX/sng_freepbx_debian_install/raw/master/sng_freepbx_debian_install.sh -O /tmp/sng_freepbx_debian_install.sh
sudo bash /tmp/sng_freepbx_debian_install.shThe install log dey land for /var/log/pbx/ under timestamped name with the form freepbx17-install-YYYY.MM.DD-HH.MM.SS.log, so every run get its own file. Na the first place to check when the script stop early, and ls -t /var/log/pbx/freepbx17-install-*.log | head -1 go find the newest one.
FreePBX dey control the configuration files wey e generate. If you edit pjsip.conf by hand for FreePBX machine, your change go disappear the next time the GUI write that file. FreePBX dey read separate files wey get _custom for the name for hand-written config, and e dey leave those ones alone.
The honest trade-off be this. FreePBX give you GUI, and e put login page for your phone system on the public internet. Raw Asterisk no get web surface at all, and every setting na documented directive wey you fit read for file and keep for git. If you install FreePBX, restrict its web port to your own address or reach am through VPN, because admin GUI for PBX na target wey get direct route to money.
Version note, correct as of October 2026: Asterisk 22 na the current long term support release, published for October 2024. Dem go fully support am until October 2028. After that, e go receive security fixes only until e reach end of life for October 2029. Asterisk 23 na the standard release. Asterisk 24, the next long term support release, dem schedule am for 15 October 2026. But on 27 October, the Asterisk versions page still list am as pre-release, and the download server carry only 24.0.0-rc3. Ubuntu 24.04 carry Asterisk 20.6.0 for the universe repository. Upstream move Asterisk 20 go security fixes only on 19 October 2026, and its end of life dey for October 2027. Ubuntu 26.04 carry Asterisk 22.5.2.
Install Asterisk for Ubuntu 24.04
Distribution package na the quickest way. Ubuntu dey patch am, and e dey start by itself under systemd.
sudo apt update
sudo apt install -y asterisk
sudo asterisk -rx "core show version"If you build am from source, you go get the current long term support release instead.
sudo apt update
sudo apt install -y build-essential wget
cd /usr/local/src
sudo wget https://downloads.asterisk.org/pub/telephony/asterisk/asterisk-22-current.tar.gz
sudo tar -xzf asterisk-22-current.tar.gz
cd asterisk-22.*
sudo contrib/scripts/install_prereq install
sudo ./configure
sudo make menuselect
sudo make -j"$(nproc)"
sudo make install
sudo make samples
sudo make config
sudo ldconfiginstall_prereq install go download the build dependencies for your distribution, while install_prereq test go print the commands wey e for run without changing anything. make menuselect go open the module picker, na there you go enable codec_opus under Codec Translators. Run make samples only for fresh install, because e go write the sample configuration inside /etc/asterisk. make config go install the init script for /etc/init.d/asterisk, and systemd go run am through its SysV compatibility layer, so sudo systemctl enable --now asterisk go work afterwards.
No matter which way you use, sudo asterisk -rvvv go connect to the daemon wey dey run and give you the CLI. If you run core show version there, e go show wetin you really install.
Configure SIP trunk and one extension
PJSIP na the SIP channel driver wey current Asterisk dey use. E configuration dey inside /etc/asterisk/pjsip.conf, and small typed sections dey build am; dem dey reference each other by name. Different section types fit share one name, na why every block below dey called mytrunk.
[transport-udp]
type=transport
protocol=udp
bind=0.0.0.0
[mytrunk]
type=registration
outbound_auth=mytrunk
server_uri=sip:sip.example.com
client_uri=sip:1234567890@sip.example.com
retry_interval=60
[mytrunk]
type=auth
auth_type=userpass
username=1234567890
password=REPLACE_WITH_A_LONG_RANDOM_SECRET
[mytrunk]
type=aor
contact=sip:sip.example.com:5060
[mytrunk]
type=endpoint
context=from-trunk
disallow=all
allow=ulaw
outbound_auth=mytrunk
aors=mytrunk
[mytrunk]
type=identify
endpoint=mytrunk
match=sip.example.comThe registration object dey send REGISTER wey tells your provider where to deliver your calls. The identify object na how Asterisk matches call wey dey enter from provider to this endpoint by source address, and provider usually publishes several addresses wey you go list there. Outbound registration and endpoint na separate objects on purpose: one tells provider where you dey, the other decides wetin go happen to the calls.
Desk phone need three more objects.
[6001]
type=endpoint
context=internal
disallow=all
allow=ulaw
auth=auth6001
aors=6001
direct_media=no
[auth6001]
type=auth
auth_type=userpass
username=6001
password=REPLACE_WITH_A_LONG_RANDOM_SECRET
[6001]
type=aor
max_contacts=1direct_media=no dey keep Asterisk for the media path. If you leave am out, Asterisk go try make the two endpoints send RTP directly to each other. This go fail when phone dey behind NAT (network address translation) for home router. Never make the secret match the extension number. The upstream sample uses password=6001 for extension 6001 so the example go remain easy to read, and scanners dey try that pattern first.
The dialplan for /etc/asterisk/extensions.conf dey decide wetin each context fit do.
[internal]
exten => 6001,1,Dial(PJSIP/6001,20)
exten => _9X.,1,Dial(PJSIP/${EXTEN:1}@mytrunk,60)
[from-trunk]
exten => 1234567890,1,Dial(PJSIP/6001,20)
same => n,Hangup()Those two contexts na security boundary. from-trunk dey handle calls wey dey enter from provider, and e fit only ring extension 6001. E no fit reach _9X. pattern, so call from outside no fit dial out again through your trunk. If you merge the contexts, you don build the usual toll fraud path: stranger calls your number, your dialplan dials out with your account, and na you go pay for am.
Apply the configuration and check am.
sudo asterisk -rx "pjsip reload"
sudo asterisk -rx "pjsip show registrations"
sudo asterisk -rx "pjsip show endpoints"pjsip show registrations suppose list mytrunk with status Registered. Rejected mean provider reject your credentials. Unregistered mean your REGISTER no dey get answer, so check the firewall next.
Firewall rules for SIP and RTP
Signalling and media need different handling because dem carry different risk. Restrict 5060 to the addresses wey your provider actually dey use and to the networks wey your phones dey on.
sudo ufw allow proto udp from 203.0.113.10 to any port 5060
sudo ufw allow proto tcp from 203.0.113.10 to any port 5060
sudo ufw allow 10000:10200/udp
sudo ufw status verboseYou no fit narrow the RTP range as much because media often dey come from a different address from the signalling. Ask your provider for their media subnets, and restrict to dem if dem publish am. Keep the range only as large as your busiest hour needs. Default policies, rule order and the rest of the ufw model dey covered for the ufw firewall basics guide for a VPS.
Check IPv6 too. ufw allows IPv6 by default, and rule wey no get source address, like the RTP rule above, applies to both families. The 5060 rules name an IPv4 source, so dem match IPv4 only, and provider wey dey signal over IPv6 need rules of their own. Setting IPV6=no inside /etc/default/ufw and reloading ufw no mean say IPv6 go remain unfiltered: e restricts IPv6 traffic to the loopback interface. Opening ports for IPv6 with ufw explains how the two families differ. Most providers also give you separate network firewall for the control panel, and that one dey enforce before your packets reach the VPS, so port must dey open for both places.
SIP brute force and toll fraud no be optional problem
Put 5060 for public address and scans go start. The pattern steady: REGISTER and INVITE requests from plenty source addresses, dey try common extension numbers with common secrets. Asterisk dey log each failure, and the line get this shape.
Request 'REGISTER' from '<sip:1000@198.51.100.20>' failed for '198.51.100.20:5060' (callid: 5f1a5c0d) - No matching endpoint foundThe reason this one need serious effort na money. Person fit use stolen extension make expensive international calls, often go premium rate numbers wey dey pay attacker share. You go pay the bill because the calls use your credentials. E dey run with machine speed, and most times e dey run overnight.
Six controls dey mandatory; dem no be optional hardening.
- Never create extension wey e secret na the extension number itself or short word. Generate one with
openssl rand -base64 24and paste am inside. - Leave anonymous inbound calls off. PJSIP dey reject unidentified calls by default, and e only accepts dem if you create endpoint named
anonymous. No create am. - Keep trunk context separate from any context wey fit dial out, as e show above.
- Restrict signalling by source address, both for ufw and for your provider network firewall.
- Set spending cap with your SIP trunk provider and disable international destinations wey you never call. Na this only control fit limit the loss when the other controls fail.
- Run fail2ban against the Asterisk log.
Asterisk still dey raise security event when request no match any endpoint, and fail2ban fit act on those events. The [global] section of pjsip.conf takes unidentified_request_count, wey default to 5, and unidentified_request_period, wey default to 5 seconds. But dem only apply when auth_username dey listed inside endpoint_identifier_order. That method need delay the event until request arrive with credentials. Then the two settings decide how many unmatched requests from one address inside the period go produce the event. With the default order of ip,username,anonymous, unmatched requests dey raise security events without that delay.
Ban scanners dem with fail2ban
fail2ban releases one asterisk jail wey don already write. E dey cover ports 5060 and 5061, read /var/log/asterisk/messages, and e default to maxretry = 10. Jail dey off until you enable am for /etc/fail2ban/jail.local.
[asterisk]
enabled = true
maxretry = 5
findtime = 600
bantime = 86400sudo systemctl restart fail2ban
sudo fail2ban-client status asteriskHealthy result go list the jail log file and count of addresses wey dem ban currently. For public 5060, that count go stop dey zero within one day. The shipped filter dey match the No matching endpoint found notices above, and e still match Asterisk structured SecurityEvent lines. Those events dey go separate log channel wey dem comment out for /etc/asterisk/logger.conf. So enable am there and add the file to the jail logpath if you want make e catch dem.
[logfiles]
console => notice,warning,error
messages.log => notice,warning,error
security.log => securityReload the logger with sudo asterisk -rx "logger reload". The filter still get journalmatch for asterisk.service, so journal backend go work if you prefer not to keep log files. Installation, jail.local structure and how to unban address wey lock you out dey covered for fail2ban guide for Ubuntu 24.04.
Latency and codec choice for far VPS
Geography dey determine delay, and you no fit configure am comot. ITU-T G.114 recommend say one-way delay make e stay below 150 ms for normal conversation, and e still regard up to about 400 ms as usable. Audio from phone dey go your VPS, then e dey go your trunk provider, so VPS wey dey wrong region go pay for that journey two times. Put am near the phones or near the provider. If both options no align, prefer near the phones, because that leg normally dey use consumer internet where jitter dey worst.
Codec choice dey determine bandwidth per call. Every codec for here dey send one packet every 20 ms. That mean 50 packets every second. Each packet carry 40 bytes of IP, UDP, and RTP headers on top of the audio payload.
Di data wey dey behind dis chart
[
{
"label": "G.711 ulaw",
"payload_kbps": 64,
"ip_kbps": 80
},
{
"label": "G.722",
"payload_kbps": 64,
"ip_kbps": 80
},
{
"label": "Opus at 24 kbps",
"payload_kbps": 24,
"ip_kbps": 40
},
{
"label": "G.729",
"payload_kbps": 8,
"ip_kbps": 24
}
]G.711 ulaw na the default for most trunks. Its payload na 64 kbps. With headers, call wey dey active dey use 80 kbps for each direction. Opus for 24 kbps dey use 40 kbps. G.729 reduce am to 24 kbps, but audio quality and CPU time go suffer. These figures na header arithmetic, no be measurements: payload rate plus 40 bytes per packet for 50 packets every second. Ethernet or VLAN framing go add small extra traffic for the wire.
Transcoding dey use CPU. If your phones and trunk both dey speak ulaw, allow only ulaw and Asterisk go pass the audio through without changing am. Opus dey perform well for links wey dey lose packets, but transcoding between Opus and G.711 need the external codec_opus module. You go select am for make menuselect, and dem no dey build am by default.
Wetin fit break, and the string wey you go see
The call connect but nobody dey hear anything. RTP no dey arrive. Confirm say the range for rtp.conf na the same range wey you open for firewall, then use sudo tcpdump -ni any udp portrange 10000-10200 watch packets while you make call. Empty capture only mean say filter no see anything wey match am. Packets fit stop before dem reach server, for example, provider network firewall fit block dem. Or the far end fit dey send dem go another address or port wey dey outside this filter. So compare am with the media address and port for the SDP wey pjsip set logger on show.
Audio dey only one direction. One side dey send RTP go address wey no fit receive am. Na address problem be this, no be port problem. If VPS get public address directly for its interface, you no need NAT handling. If provider give VPS private address with one-to-one public mapping, set the public address for transport and list your private range for local_net.
[transport-udp]
type=transport
protocol=udp
bind=0.0.0.0
local_net=10.0.0.0/8
external_media_address=198.51.100.5
external_signaling_address=198.51.100.5No matching endpoint found for log. Request no match any endpoint by IP address or username. If na scanner send am, this one normal and fail2ban go handle am. If na your own provider send am, e mean say identify section no list the address wey dem dey use call you.
Registration status na Rejected. Provider reject the credentials for your auth section. Run pjsip set logger on for CLI, watch one REGISTER and the response to am, then compare client_uri and username with wetin provider issue give you.
Nothing useful dey show for log at all. By default, Asterisk dey write notice level and anything higher to messages.log. Raise am with core set verbose 4 and pjsip set logger on while you reproduce the problem, then turn both off, because SIP logger dey write every packet.
Before you expose am
PBX no be like other things wey you self-host. If web app spoil, na one page you go lose. If PBX spoil, e fit create phone bill within hours while you dey sleep. Run am for VPS wey no dey do any other work. Restrict 5060 to addresses wey you know. Give every extension one random secret. Put spending cap for the trunk account. The rest of the server need the same basic security as any server wey dey exposed. this overview of how safe VPS hosting really be explain am.
FAQ
Which ports I need open for a self-hosted VoIP server?
Port 5060 for SIP signalling over UDP and TCP, port 5061 if you use SIP over TLS, and one range of UDP ports for RTP media. Asterisk sample rtp.conf uses 10000 to 20000, and the compiled-in defaults na 5000 to 31000. Each call dey use two ports from the range, so range of two hundred ports fit handle one hundred calls wey dey happen at the same time. Open the RTP range as UDP, and restrict 5060 to your provider addresses and your own networks instead of leaving am open to everybody.
Make I install Asterisk by itself or use FreePBX?
Install raw Asterisk when you want small attack surface and configuration files wey you fit keep for git, and you ready to write the dialplan. Install FreePBX when you want GUI for extensions, voicemail and call queues. As of October 2026, FreePBX 17 installer expect vanilla Debian 12 machine and e install Asterisk, web server, database server and PHP, so give am VPS of its own. FreePBX dey regenerate the config files wey e manage, so hand edits suppose dey inside its _custom files.
Why audio no dey after the call connect?
The signalling work, but the media no work. SIP set up the call on port 5060, and the audio na separate UDP flow to one port inside the RTP range wey something dey drop. Check say the range for rtp.conf match the range wey your firewall open, and check your provider network firewall as well as the one for the server. Run sudo tcpdump -ni any udp portrange 10000-10200 during a call. If the capture empty, e only mean say e no see matching packets: dem fit dey blocked before dem arrive, or dem fit dey sent to address or port wey dey outside the filter.
How I fit stop SIP brute force attacks and toll fraud?
Give every extension long random secret; make e never match the extension number. Keep the context wey your trunk use separate from any context wey fit dial out, so inbound call no fit dial back out through your account. Restrict port 5060 to your provider addresses. Enable the asterisk jail for fail2ban. E dey read /var/log/asterisk/messages and ban the addresses wey produce No matching endpoint found failures. Then set spending cap and block international destinations wey you no use with your provider, because na only this control fit limit the loss if the other protections fail.
VPS for far region fit spoil call quality?
Yes, because the audio dey pass through two legs: phone to VPS, then VPS to trunk provider. ITU-T G.114 recommend one-way delay below 150 ms, and VPS wey no dey well located fit use most of that budget on distance alone. Choose region wey near the phones, because that leg usually dey pass through consumer internet where jitter worst. Codec choice no fit remove network delay. E dey change bandwidth and add small coding and packetization delay of its own. RFC 6716 gives Opus algorithmic delays from 5 ms to 65.2 ms. So G.729 saves bytes, but e no go rescue path wey get 200 ms delay.