Mastering SIP Trunk Setup in Asterisk: From Outbound Auth to Inbound Routing | Brav

Learn how to set up a SIP trunk in Asterisk using pjsip: configure outbound registration, authentication, trunk identification, and routing to a specific extension. Follow the step-by-step guide with live commands and troubleshooting tips.

Mastering SIP Trunk Setup in Asterisk: From Outbound Auth to Inbound Routing

Published by Brav

Table of Contents

TL;DR

  • I’ll show you how to configure a SIP trunk in Asterisk using pjsip, covering outbound registration, authentication, trunk identification, and routing to a specific extension.
  • You’ll learn how to edit pjsip.conf with nano, reload pjsip without a full restart, and verify registrations and endpoints.
  • I’ll explain why inbound authentication can be omitted and how the contact_user field maps incoming calls.
  • The article includes a handy table comparing key pjsip configuration sections and troubleshooting tips.

Why this matters

Running a PBX is a lot like owning a train station: you need accurate schedules, reliable tracks, and a ticket-control system that knows who can get on and off. In a VoIP world, that translates to correct trunk configuration, authentication, and routing. If the wrong authentication settings are in place, calls can drop or never connect. If the trunk identify section doesn’t match the provider’s IP, the PBX will refuse incoming calls, and your customers will hang up.

For me, the biggest pain point was getting my outbound calls authenticated with the provider sip.flagonc.com while keeping the inbound path simple and secure. I needed a way to map incoming contacts straight to extension 9999 without overcomplicating the configuration.

Core concepts

Below is a quick snapshot of the three main pjsip configuration objects I work with every day:

SectionUse caseLimitation
endpointDefines the local interface for a SIP device or trunkCannot directly register to an external provider – needs registration object
registrationHandles outbound REGISTER requests to the providerInbound authentication is not supported here – you must use identify for inbound
identifyMatches inbound contacts based on IP or domain, routing them to the correct endpointRequires the provider’s IP or domain to be known; otherwise, all contacts are rejected

The endpoint is where the PBX talks to the outside world. The registration tells Asterisk how to prove its identity to the provider. The identify section is what catches incoming calls from the provider’s IP and tells Asterisk which endpoint to hand them to.

How to apply it

Below is my step-by-step workflow for setting up a SIP trunk with pjsip. I’ll reference the official Asterisk docs for each step so you can verify the syntax.

  1. Open the configuration file

    sudo nano /etc/asterisk/pjsip.conf
    

    I use nano because it’s simple and shows line numbers.

  2. Create the transport – the network interface through which SIP will travel.

    [transport-udp]
    type=transport
    protocol=udp
    bind=0.0.0.0
    

    Citation: Asterisk — PJSIP Configuration Examples (2024)

  3. Define the outbound registration – this is the “auth at sip.flagonc.com” part.

    [mytrunk]
    type=registration
    outbound_auth=mytrunk
    server_uri=sip:sip.flagonc.com
    client_uri=sip:[email protected]
    retry_interval=60
    contact_user=9999 ; maps incoming contacts to extension 9999
    

    Citation: Asterisk — Configuring Outbound Registrations (2024)

  4. Set up the authentication credentials

    [mytrunk]
    type=auth
    auth_type=userpass
    username=myuser
    password=mypassword
    

    Citation: Asterisk — PJSIP Configuration Examples (2024)

  5. Create the AOR (Address of Record) – this tells Asterisk where the provider will send REGISTERs.

    [mytrunk]
    type=aor
    contact=sip:sip.flagonc.com:5060
    
  6. Configure the endpoint – this is the local representation of the trunk.

    [mytrunk]
    type=endpoint
    context=from-external
    disallow=all
    allow=ulaw
    outbound_auth=mytrunk
    aors=mytrunk
    

    Citation: Asterisk — PJSIP Configuration Examples (2024)

  7. Add the identify section – this tells Asterisk to accept incoming calls only from the provider’s IP.

    [mytrunk]
    type=identify
    endpoint=mytrunk
    match=sip.flagonc.com
    

    Citation: Asterisk — PJSIP Configuration Examples (2024)

  8. Reload the configuration – no full restart needed.

    asterisk -rx 'module reload res_pjsip.so'
    

    Citation: Asterisk — Reload (2024)

  9. Verify the registration – the provider should now see a REGISTER.

    asterisk -rx 'pjsip show registrations'
    

    You should see the registration zip for mytrunk. Citation: Asterisk — PJSIPShowRegistrationsInbound (2024)

  10. Check the endpoint status – confirm the endpoint zip and that the trunk is online.

    asterisk -rx 'pjsip show endpoint mytrunk'
    

    Citation: Asterisk — PJSIPShowEndpoint (2024)

  11. Update the dial plan – route outbound calls through the trunk and inbound calls to the right extension.

    [from-external]
    exten => _X.,1,Dial(SIP/${EXTEN}@mytrunk)
    

    When you receive a call, Asterisk will match the identify section and forward it to extension 9999.

  12. Test a call – place an outbound call and see the inbound callback.

Pitfalls & edge cases

IssueWhy it happensFix
Inbound authentication missingThe provider doesn’t require it and we only care about outboundLeave out the inbound auth section; use identify instead
Call drops after a few secondsMis-typed match IP in identify sectionDouble-check provider’s IP and update the match line
pjsip show registrations shows unregisteredThe provider’s URI changed or authentication failedVerify server_uri, client_uri, and credentials; re-reload
Outbound call fails with 401 UnauthorizedThe outbound_auth section is missing or wrongRe-add the auth block and reload

If you run into any of these, start by reviewing the config sections highlighted above and cross-check the output of the pjsip show commands.

Quick FAQ

Q: What does contact_user=9999 do? A: It tells Asterisk to map any incoming SIP contact to the local extension 9999. This is the same as setting the contact_user in the registration.

Q: Why is inbound authentication omitted? A: Many providers only need the PBX to prove its identity outbound. By not configuring inbound auth, you reduce complexity and the risk of mis-configuring credentials that could expose your system.

Q: How does the identify section match IP addresses? A: The match option takes a domain or IP. When Asterisk receives a SIP request, it checks the source IP against this list. If it matches, the request is routed to the specified endpoint.

Q: What is the address of record? A: It’s the SIP URI the provider uses to reach your PBX – essentially the “public” SIP address.

Q: Should I use pjsip reload or restart Asterisk after changes? A: Use pjsip reload (or module reload res_pjsip.so) for most changes. Restart only when you modify core files or the Asterisk binary.

Q: How can I confirm the trunk is working? A: Run pjsip show registrations and pjsip show endpoint – look for the registration zip and endpoint zip values.

Q: Can I use this configuration with any SIP provider? A: The structure is generic, but you’ll need to adjust server_uri, client_uri, and the match IP to match your provider’s requirements.

Conclusion

By following these steps, you’ll have a reliable SIP trunk that authenticates outbound, accepts inbound calls from a trusted provider, and routes those calls to the correct internal extension. Remember to keep the configuration file tidy, reload only the parts you change, and verify with pjsip show commands. If the trunk is working, the next step is to build the dial plan around it and monitor call quality.

This workflow works for Asterisk 18+, and the same concepts apply to newer releases with minor syntax changes. Keep your Asterisk up to date and test each change in a lab environment before deploying to production.


References

Last updated: December 20, 2025

Recommended Articles

Hardware Hacking on a Budget: Master Low-Cost Tools & Techniques | Brav

Hardware Hacking on a Budget: Master Low-Cost Tools & Techniques

Build a low-budget hardware hacking lab with free tools and a variable-temperature soldering iron. Learn firmware extraction, UART hacking, and RF probing.
How I Mastered the Yantra: Step-by-Step Drawing Guide for Artists & Geometry Hobbyists | Brav

How I Mastered the Yantra: Step-by-Step Drawing Guide for Artists & Geometry Hobbyists

Discover how to draw a Yantra step-by-step, with symmetry hacks, petal construction, and framing tips for artists and geometry hobbyists.
Master AI Image Generation in Minutes with a 4-Layer Framework | Brav

Master AI Image Generation in Minutes with a 4-Layer Framework

Learn how to create cinematic AI images and videos in minutes using the 4-layer framework with Nano Banana Pro and Kling 01. A step-by-step guide for creators.
Asterisk Architecture Demystified: Build, Configure, and Scale Your PBX | Brav

Asterisk Architecture Demystified: Build, Configure, and Scale Your PBX

Discover how to master Asterisk’s modular architecture, configure channels, dial plans, and APIs. Build a scalable PBX from scratch with step-by-step guidance.
I Configured a SIP Trunk in Asterisk—No More Call Drops | Brav

I Configured a SIP Trunk in Asterisk—No More Call Drops

Set up a reliable SIP trunk in Asterisk with Firstcom Europe. Configure pjsip, dial plan, and avoid call drops. Step-by-step guide for small-business admins.