Using Lync with an Asterisk Server & SIP Trunking

3/8/2013 UPDATE: I’ve since used these instructions to integrate with Lync 2013 and have had the same success with using Asterisk as my PTSN gateway. I’ve added some notes in the Trunks section based on my experience!

Back in January I had a client look to use a hosted PBX solution for their office, with a mix of desk and soft phones, but didn’t want to make the full on investment with Lync (boo). So as a compromise they went with an Asterisk box plus some inexpensive Cisco SPA phones. Without diverging too much off course, I went through a few iterations with SIP providers and eventually came to the realization that using a Lync certified partner AND have a diverse product offering were not two things that could exist in the SMB IT universe.

So I decided to setup a front end Asterisk server as the “PTSN” gateway, housing the SIP trunks and the extensions for the client, and thought to myself “Why can’t I just as easily tie Lync into this system?” After DAYS of research plus trial & error, I wanted to share my pain points to help out the IT community.

A little on my deployment:

I’m the latest version of FreePBX for my Asterisk box, and Lync Server 2010 with CU6 for my Enterprise voice deployment. I’ve also an Exchange 2010 SP1 CU6 Unified Messaging box for voicemail on the Lync side.

With everything setup and rolling on the FreePBX side, and the ability to call between Lync clients, my next step was to integrate the two.

I enabled Enterprise Voice capabilities on my Front End server, and made sure to change the listening port to 5060 (because this is what FreePBX listens on).

I also added a Mediation server, using the IP address of the Asterisk Box.

Once that was done, I published my topology and that was it from the Lync infrastructure side.

Over on the FreePBX side, I create a new SIP TRUNK called Lync and all I needed to complete was the following OUTGOING SETTINGS > PEER details, where the host is the IP of my Lync box and the fromdomain is the IP of the IP of my FreePBX box (this is important!) UPDATE: I’ve since discovered that the fromdomain doesn’t effect the traffic one way or another!!

One additional pointer: the context of from-internal is very important for routing calls from Lync through the Asterisk box and outbound through the SIP trunks. Anything else will still allow internal extension dialing, but fail elsewhere.


Be sure to leave the INCOMING SETTINGS > Peer Details blank

Now I already have all my Incoming Routes setup (my DIDs) I’m ready to setup my Outgoing Routes. The first is going to be called LYNC, and all this has is a dial pattern for FreePBX to have the ability to call 4 digit extensions between the two systems.

Next, in the existing Outgoing Route, which I have called Outgoing, we have all of the local exchanges here in the Twin Cities, another to account for everything else, and finally, MOST IMPORTANTLY, the dial pattern to account for E.164 dialing within Lync.

If you don’t add this, the calls WILL go between the two systems and their internal extensions, but will never make it to the public PTSN. You’ll see something like this in the Asterisk debug logs:

[2012-07-11 11:34:36] NOTICE[-1] chan_sip.c: Call from ‘Lync’ ( to extension ‘+19521234567’ rejected because extension not found in context ‘from-internal’.

At this point the trunk configuration is changed, however we need to add 2 “Other SIP Settings” on the Asterisk server, because by default it doesn’t listen properly on port 5060, and will prevent communication issues between the Lync & FreePBX servers:

After you make this change, it’s best to perform a restart of the PBX server for them to take effect.

Now at this point, with the FreePBX server configuration completed, we would go back into our Inbound Routes (DIDs) in FreePBX and change the Trunk Destination to the Lync trunk.

But now the FreePBX side is only half the battle. On the Lync side, we need to make sure we create our users having the same phone number we are forwarding from the SIP trunk, but of course in E.164 format (+1xxxxxxxxxx). Also, make sure to enable Unified Messaging on the Exchange server for the users when completed.

The most challenging part of the integration by far was the Voice Routing and Normalization. Apart from the +1NXXNXXNXXX pattern on the FreePBX side, getting the correct normalization rules to allow calls FROM Lync and TO FreePBX, plus vice versa, all boils down to normalization. But luckily when it’s all said it done, there are really only 5 rules:

I added in a normalization rule for communication internally from the FreePBX server using 4 digit extensions over the the Lync server. The other key on this was to specify it as an internal extension:

Once these are setup and ready, we modify the Global Voice Policy to add a PTSN Usage that is essentially a Wildcard for any numbers, to pass them to the Mediation server. This then lets the FreePBX server do the translation.

You can name your Voice Route anything you’d like; I just named my SIP Trunk so it would stand out for the sake of recognizing what I had added.

Finally, perform a Commit All to the configuration in Lync, and you’re ready to go!

I use the 3CX Softphone to connect to the FreePBX server, and with the Lync client fired up I call the internal extension – volia!

And then vice versa from Lync back over toFreePBX

The only outstanding issue I’ve run across with this deployment is using the call forwarding and corresponding Lync Mobility features. I’m digging into them and hope to be back soon with a solution.

Chris Blackburn

Learn More →

14 thoughts on “Using Lync with an Asterisk Server & SIP Trunking

  1. Nethawk July 12, 2012 at 9:19 am

    Very amazing you can actually use asterisk as PSTN gateway. very good application of Asterisk as gateway.

  2. VoIP Pakistan July 12, 2012 at 9:24 am

    We have hard time selling microsoft lync certified gateways Audiocodec etc are expensive brand. We can tell our client that low-cost FXO Asterisk cards using a 200 USD PC would make it cheap gateway fully working with Lync. need to do proof of concept 🙂

  3. Sergey October 22, 2012 at 11:09 am

    Hi Chris,
    I’m currently setting up Asterisk/Lync trunk using Freepbx distro.
    I’ve tons of questions regarding FreePBX/Lync 2010 setup.

    I’ve been using your guide above and was able to configure the trunk. Inbound calls from outside through asterisk worked just fine and right away. however I couldn’t get Lync clients calling outside. I’m using freepbx distro.

    the default context keeps on changing to from-sip-external
    inside of /etc/asterisk/sip_general_additional.conf

    then i found work around and added context=from-internal
    to /etc/asterisk/sip_general_custom.conf which overrides sip_general_additional.conf

    This has fixed outbound calling from Lync clients through asterisk trunks however the trunk to Lync is no longer relevant for outbound calls.
    Even if i disable the Lync trunk I still able to make an outbound call from Lync client.

    1. Chris Blackburn October 26, 2012 at 9:37 pm

      Sergey, what build of FreePBX are you using? From all of my research anything above 2.6 DOES NOT recommend doing manual configuration file edits and instead using the GUI. I personally started around 2.7 and now have updated all the way to 2.10 (which is what the walk through is based on).
      Also make sure in your FreePBX Outgoing Route your dial plans are setup EXACTLY on how E.164 dial patterns need to be for your region or they will fail. The debug logging on both FreePBX and your Lync trace logs should reveal more information.

  4. paul November 2, 2012 at 2:32 pm

    Does anyone know what if there is a minimum version requirement in Asterisk to allow configuring TCP transport of SIP?

    1. Chris Blackburn November 2, 2012 at 11:05 pm

      Paul, I know a lot of the different PBX platforms (TrixBox, FreePBX, AsteriskNow) use different versions of Asterisk so as long as you’ve run updates you should be OK. As for a “minimum”, I’ve read that as long as you use Asterisk 1.6 or higher, it supports TCP ‘out the box’, and from there it’s just a matter of what tweaks are necessary to get them talking.

  5. Jerome Haltom December 7, 2012 at 9:22 pm

    I’m trying to do something similar, but with sip: URIs and not telephony extensions. I’d like Lync to send all SIP requests for contacts at non-local URIs (,, to a local SIP proxy.

    Seems that PSTN gateways are only used if the number being dialed is a number. And it seems that SIP federation only allows one to use SRV lookups by Lync itself.

    I just want Lync to send sip:*.* to a local proxy.

    Any clues on this one?

    1. Chris Blackburn March 22, 2013 at 1:04 pm

      Looks like you may have figured this out based on the comment on the Technet forums, but it appears you are looking at doing Lync Static Routing.

      Here is a good walkthrough on how to do this:

  6. Sergio's Shack : One for the books April 23, 2014 at 4:11 am

    […] Since we are building our knowledge about enterprise voice, you will need an IP-PBX that you can configure in order to fully understand enterprise voice. A free IP-PBX that you can use for your lab is Asterisk of FreePBX. The integration between Lync and Asterisk, is well documented here […]

  7. Mohammed JH February 9, 2015 at 7:36 am

    Dear Chris,

    I have integrated Lync with a customized version of asterisk and didn’t configure the asterisk my self but a certain company did all the integration from Asterisk side. I would like to know how secure is this distro as I’ve heard many horrible stories about it.

    the other question I’d like to know if it’s possible to allow simultaneous inbound calls to a certain user. let’s say I have a user with a SIP phone, Mobile and Lync extension. is it possible to allow inbound calls to be forked to all those destinations in the same time?

    This feature is available on SBC like Sonus, and other gateways like Patton. I have configured them but would be interested to know if it’s also available on this freepbx version.


    1. Chris Blackburn February 17, 2015 at 3:39 pm

      Unfortunately its just about different with every Asterisk distro so I’d recommend reading over this great blog post that gives some scenarios & try to find the one that works best for yours.

  8. Steve May 11, 2015 at 3:39 pm

    Great to see you’re keeping current with this blog! This is really invaluable information as I’ve found it’s hunt ‘n’ peck for good Lync/Asterisk config info.

    I’m currently configuring Lync Server 2013 with FreePBX Distro for our EV deployment. (We already have IM, Presence, and Conferencing [-dial-in] set up.

    So here are some questions:
    How many SIP trunks do I need to purchase? Is it one per call path?

    Do I need to purchase SIP trunks from Digium for Asterisk and more from my ITSP?

    Any info is greatly appreciated!!!

    1. Chris Blackburn May 13, 2015 at 2:24 pm

      You will need one SIP trunk per inbound or outbound call yes. All internal calls (Lync to Lync or Lync to Asterisk) do not require this.

    2. Kevin Hayashi May 30, 2015 at 10:34 am

      It all depends on the SIP trunk that you purchase. If you purchase a SIP trunk from SIPStation or Digium with an unlimited call plan, then it is typically one call path per trunk.

      I found it to be much more flexible to purchase the Digium per minute trunk which allows unlimited concurrent call paths on a single trunk. I use this with my Asterisk / Lync 2013 server installation and have 5 DID’s. The Lync Conference bridge can handle multiple callers and I still have Direct Dial in for my main phone number, Auto Attendant, and Voicemail. The rate is $0.015/min and the most expensive phone bill I’ve had so far is about $20.

      My other number is reserved for faxing which is directed to a Fax mailbox as the Digium trunk supports. t.38


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.