Layer Four

Portal Home > Knowledgebase > Digital Voice > SIP Trunking & Device Configurations > Asterisk Example Configuration for SIP Trunking

Asterisk Example Configuration for SIP Trunking

This knowledge base article describes the SIP trunking feature of Layer Four Hosted PBX and how it can be used to connect a PBX to an extension. Also, you can find here the steps that have to be followed to set up Asterisk to act as a PBX, along with some practical examples.

Enable SIP Trunking for a Layer Four PBX Extension

When enabled at extension level, this feature allows you to connect a PBX to the extension (when a DID assigned to that extension is called, it is passed on to the connected PBX in the SIP:To header). This option was added because some customers wanted to connect a separate PBX to Hosted PBX account. 

In order to enable this feature, your account must be enabled for SIP Trunking. Check with your account representative if you're unsure of this.

How to Configure Asterisk to Act as a PBX

To set this up, you will need the latest public release of Asterisk ( There are plenty of installation tutorials on the web, so you shouldn't have any difficulty in installing Asterisk. In this example, ./configure --prefix=/opt is used to set /opt/ as a destination directory for the Asterisk files.

After completing the installation process, you will need to edit the files below:

Example sip.conf:

disallow = all 
allow = g729 
allow = ulaw 
allow = alaw 
bindaddr = asterisk_server_ip 
port = 5060
context = phones 
nat = no 
domain = your_server_ip 
register =XXXX*XXX:[email protected]/XXXX*XXX

fromuser = XXXX*XXX 
fromdomain = asterisk_server_ip 
defaultuser = XXXX*XXX 
authuser = XXXX*XXX 
dtmfmode = rfc2833 
dtmf = rfc2833 
disallow = all 
allow = g729 
allow = ulaw 
allow = alaw 
host =
qualify = yes 
nat = no 
context = from-layer4 
canreinvite = yes

defaultuser = 5000 
secret = secret 

The example above registers the local Asterisk PBX to Layer Four. XXXX*XXX is the extension used for registration to Layer Four Make sure the extension has SIP trunking enabled in the Layer Four Control Panel. 5000 is a local Asterisk extension that will be used for both incoming and outgoing calls.

Example of extensions.conf:


 exten => 5000,1,Dial(SIP/5000)
 exten => _X.,1,Dial([email protected])
 exten => _X.,2,Hangup
 exten => 18002304043,1,Dial(SIP/5000)
 exten => _X.,1,Congestion()

This is a basic example of an Asterisk dialplan that can place outgoing calls and receive incoming calls. 5000 is the only local extension. The rest of the prefixes matching _X. get forwarded through the layer4 channel. The [from-layer4] context is used for incoming calls.
To launch Asterisk, run:
/opt/sbin/asterisk -f -g -U asterisk -G asterisk

Testing the Configuration


To register the 5000 extension using a X-Lite softphone, you need to open its SIP accounts Properties menu page and set:
User name: 5000
Password: secret
Authorization user name: 5000
Domain: asterisk_server_ip

To call a different extension (e.g.: 103, from the Asterisk PBX, you need to simply dial 103). In order to take incoming calls via Layer Four on extension 5000, the [from-layer4] context needs to be added. When configuring your extensions.conf file, you must replace the 18002304043 example with the DID number assigned to your account.
Note that 18002304043 is a DID number assigned in the Layer Four interface for this example extension with enabled SIP trunking.

Disable SIP Trunking

The same behavior can be achieved by disabling the SIP trunking feature. However, if the Layer Four extension has multiple DIDs assigned and you want your Asterisk PBX to handle incoming calls correctly, it is necessary to extract the DID from the X-did header, otherwise every call will be forwarded to [email protected]_pbx and the system will report that there is no such extension. In order to fix this, you can add the following lines to extensions.conf:

exten => _X.,1,Noop(Getting DID from X-voipnow-did)
exten => _X.,n,Set(mydid=${SIP_HEADER(X-voipnow-did)})
exten => _X.,n,Goto(incoming,${mydid},1)
exten => 18002304043,1,Dial(SIP/5000) 
exten => _X.,1,Congestion()

Was this answer helpful?

Add to Favourites Add to Favourites    Print this Article Print this Article

Also Read

Powered by WHMCompleteSolution