Jun 052010
 

In my previous article we configured Asterisk with some SIP-devices, and created a basic dialplan so that they could dial eachother. We also created two additional extensions for test purposes. This time I will show you how to configure a SIP trunk, and add extensions in the dialplan so that the telephones can dial out through the trunk. And if you also have a telephone number (DID) associated with the trunk, for others to be able to dial your phones, through your Asterisk PBX.

[myphones]

; Call POTS numbers through Foo Provider (any number longer than 5 digits starting with 9)
exten => _9XXXX.,1,Log(NOTICE, Dialing out from ${CALLERID(all)} to ${EXTEN:1} through Foo Provider)
exten => _9XXXX.,n,Dial(SIP/fooprovider/${EXTEN:1},60)
exten => _9XXXX.,n,Playtones(congestion)
exten => _9XXXX.,n,Hangup()

There are a couple of things that might need explanation in the above. We use the Dial() application again, to dial the number we entered in our phone, but “${EXTEN:1}” uses the entered number, after the first digit, that is the meaning of “:1″. “60″ is the number of seconds to let it ring, until we give up and let Asterisk play congestion tones to us, increase the time value if you think it is too short. You also exchange “fooprovider” with the name of your real provider that you configured in sip.conf.

Test it

For our configuration to take effect we either have to reload it from Asterisk’s command-line interface, or restart Asterisk. To reload the SIP configuration and the dialplan, connect to the running Asterisk’s command-line:

asterisk -vcr

And run:

sip reload
dialplan reload

Verify that your Asterisk server registers with your provider correctly:

sip show registry

If necessary, troubleshoot the registration, use the following Asterisk CLI commands:

sip set debug on

Now at last, test the configuration. Dial your Asterisk server from your mobile phone, and hopefully your first SIP telephone will ring. Also watch the Asterisk console and see the Log() notice that we added appear and make you smile.

If that works, proceed with dialing out to your mobile phone from any of your configured and registered SIP phones, remember to dial 9 in front of the actual phone number.

While the call is going on, run the following command to see the two channels that are created, and switched together in your Asterisk: One channel to/from your SIP phone, and one through your trunk, to your mobile phone:

core show channels

Full example reference configuration files

Here are the full contents of sip.conf and extensions.conf, from the previous article, with the configuration from this article added, making up a fully working, basic, but yet complete Asterisk configuration.


sip.conf:

[general]
context=incoming

allow=ulaw
allow=alaw
allow=gsm

; Register and get calls from Foo Provider, to our number 1-555-455-1337
register => 15554551337:password123@sip.provider.foo

[fooprovider]
type=friend
secret=password123
username=15554551337
host=sip.provider.foo
dtmfmode=rfc2833
canreinvite=no
disallow=all
allow=ulaw
allow=alaw
allow=gsm
insecure=port,invite
fromdomain=sip.provider.foo
context=incoming

; ------------------------------------

[1000]
type=friend
secret=replacethis123
dtmfmode=rfc2833
callerid="First Phone" <1000>
host=dynamic        ; The device must always register
canreinvite=no
; Deny registration from anywhere first
deny=0.0.0.0/0.0.0.0
; Replace the IP address and mask below with the actual IP address and mask
; of the computer running the softphone, or the address of the hardware phone,
; either a host address and full mask, or a network address and correct mask,
; registering will be allowed from that host/network.
permit=192.168.1.0/255.255.255.0
context=myphones

[1001]
type=friend
secret=replacethis321
dtmfmode=rfc2833
callerid="Second Phone" <1001>
host=dynamic        ; The device must always register
canreinvite=no
; Deny registration from anywhere first
deny=0.0.0.0/0.0.0.0
; Replace the IP address and mask below with the actual IP address and mask
; of the computer running the softphone, or the address of the hardware phone,
; either a host address and full mask, or a network address and correct mask,
; registering will be allowed from that host/network.
permit=192.168.1.0/255.255.255.0
context=myphones

  22 Responses to “How to set up a SIP trunk in the Asterisk PBX”

Comments (17) Pingbacks (5)
  1. nice post. thanks.

  2. This is such a great resource that you are providing and you give it away for free. I enjoy seeing websites that understand the value of providing a prime resource for free. I truly loved reading your post. Thanks!

  3. Is it just me or is there a discrepancy between the article and the full config files at the bottom? They don’t seem to match up and the full config doesn’t seem to have any rules for incoming?

    -jp

  4. Thank you for your comment Jp.
    You are absolutely right. I must have made a copypaste error or just not checking properly when assembling the full config at the end. I have corrected it now, the full config now corresponds to the rest.
    Thanks for observing and pointing it out, I appreciate it. I must have been tired when finishing the article.

    Apologies to anyone following it not getting correct information. I hope you return and see the correction.

  5. Great article! I really hope you’ll write some more! Easy to understand and follow. I’d really like to see an article like this about all the config files and options. Bookmarked immediately! It’s hard to find information that’s to the point like this for us who don’t have much experience.

    Thank You!

  6. Great article!

    I did have a problem getting it to work with my VOSP and Asterisk 1.4 (Asterisk and SIP clients behind a NAT router), though: In sip.conf, I had to have two sections (Outgoing and Incoming), and the Outgoing section had to be located before Incoming or I would get a BUSY signal when calling the VOSP number from a cellphone:


    ;========== sip.conf
    ...
    register => myaccount:mypasswd@myvosp.com

    [vosp_outgoing]
    type=peer
    host=myvosp.com
    username=myaccount
    secret=mypasswd
    fromuser=myaccount
    fromdomain=myvosp.com
    nat=yes
    canreinvite=no

    [vosp_incoming]
    type=peer
    host=myvosp.com
    context=from_vosp
    nat=yes
    canreinvite=no

    [1234]
    type=friend
    context=my-phones
    secret=5678
    host=dynamic
    qualify=yes
    nat=no

    ;========== extensions.conf
    [from_vosp]
    exten => s,1,Dial(SIP/1234)
    exten => s,n,Hangup()

    [my-phones]
    exten => 1234,1,Dial(SIP/1234)
    exten => 1234,n,Hangup()

    exten => _0.,1,Dial(SIP/vosp_outgoing/${EXTEN})
    exten => _0.,n,Hangup()

    I don’t know the cause of this, but it took me a couple of hours to finally figure out why I could call out but not call in.

  7. Hi,

    Nice article!

    But i got a problem. When i put register => blablabla at the top of sip.conf. I get a error message that the sip.conf is invalid. If i remove the line, it works. I know iam writing my line correct. What to do?

  8. Hi,

    I followed the instructions above. I can only received incoming call.
    What is the configuration for outgoing calls?

  9. Magne,
    It sounds like you put your “register =>” first in the file? You can’t do that, it has to be within the “[general]” section, within a context preferebly, like in my example above.

    Leon,
    The configuration for outgoing calls is the section under the “; Register and get calls from Foo Provider” comment in the sip.conf example above. The definition of “fooprovider” (an example provider) is directly underneath, in the “[fooprovider]” section.
    These are then used in extensions.conf to make outgoing calls. In my example above, any number dialed starting with a “9″ and having more than 5 digits will be routed out to the provider (see the bottom of the example extensions.conf above).

  10. Hello,

    I have been having some serious problem trying to get my asterisk system to register with my sip provider. Could you please help me figure out why I am not able to connect to my sip provider?

    [general]
    register => username:password@sip.fooprovider.com

    [flowroute] ;keep this lowercase, do not change format
    type=friend
    secret=passworkd
    username=username
    host=sip.fooprovider.com
    dtmfmode=rfc2833
    context=inbound ;change to ‘ext-did’ or ‘from-trunk’ for asterisk@home
    canreinvite=no
    allow=ulaw
    allow=g729
    insecure=port,invite
    fromdomain=sip.fooprovider.com

    Your input on this will be much appreciated.

    Thanks
    Tamaso

  11. Its really helpful for the beginners like me i love read yor articles bro. :)

  12. I can make outgoing calls, but when I try to call in I get this error;

    [Apr 27 16:22:44] NOTICE[6002]: chan_sip.c:20161 handle_request_invite: Call from ’140363′ to extension ’3608137403′ rejected because extension not found in context ‘incoming’.

  13. I can make and recive calls but how do i enter an extension during a call?

    Thanks

  14. Hi,
    I wanted to know can we create sip trunk between two Asterisk server(To one with E1 From one without E1) Within a Lan Network.

  15. Hey hi this post of yours was really great thank u. . . can u tell me any disadvantage by using SIP TRUNK configuration in IP Phones. . . I need to implement on it can u please guide me. . . ???!!!

  16. Great article ! thank you

  17. Quality posts is the crucial to invite the viewers to go to see the web page, that’s what this site is providing.

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>