Asterisk tutorial

From LXF Wiki

Revision as of 17:11, 27 Sep 2008; view current revision
←Older revision | Newer revision→
Table of contents



With a rapidly emerging VoIP world, David Coulson takes a closer look at telephony's answer to the Swiss Army Knife for Linux...

It's always an exciting time when a technology moves from the early adopters into the commercial world. It happened with the Internet in the mid-90s, with Linux shortly before the turn of the millennium, and now it's happening with IP telephony. With each shift, the way we think about a technology or service has to take a flying leap, and nothing is ever quite the same again.

IP telephony, more commonly referred to as Voice over IP, or VoIP (pronounced `voypuh'), certainly isn't anything new, but it's only in the last 12 months that consumers have seen stable and cost-effective services and platforms becoming available. Much of the excitement is hype rather than genuine newness, because IP telephony has been around in various forms over the past decade, with varying levels of success. However, VoIP really is a technology where significant advantages can be found by moving to an IP method for delivering voice services, as well as opening up the arena, previously held by large telecommunications corporations, to smaller companies with existing IP infrastructures.

Naturally, as VoIP is a service that operates over the public Internet, or a private LAN/WAN segment, the majority of the protocols and standards used are open, allowing for a range of software and hardware to be developed. There are several different implementations of VoIP, each of which has its advantages and disadvantages, and some software supports one, while a different package supports another.

VoIP for the masses

Implementing VoIP generally consists of two components: a process to get audio information from a user into a VoIP network (commonly known as a telephone), and a process to enable a VoIP environment to communicate with the public telephone network. The latter is something that many proprietary systems have failed to provide, despite the fact that being unable to use a VoIP system as one would a standard telephone makes it virtually useless for both residential and business purposes.

Although the public telephone network in many countries is detested by technical individuals, it is a standard and almost everyone is connected to it. Unfortunately, as great as VoIP is, not everyone uses it, so we still need to be able to call out to standard telephone systems, and to receive calls from the rest of the world.

Interfacing two telephone networks is nothing new ­ any business with more than five people is likely to have a private telephone network with extensions and voice services. To connect this network to the public switched telephone network (PSTN), a private branch exchange (PBX) is used. A PBX can be thought of as a telephone router, with a private range of addresses (or extensions) on the inside, and access to public numbers on the outside.

A good-quality PBX isn't cheap, usually costing at least £15,000 for the bare minimum of features, although they do offer the reliability users expect from their voice network, with near 100% uptime over years, if not decades.

If you're interested in starting to build a VoIP environment, the first point of interest should always be the PBX, because this is your border between the PSTN and your users. Commercial VoIP PBX systems are available from almost anyone who offers standard PBX devices, as well as being part of Cisco's suite of network devices. However, in true Open Source style, there's also a great alternative available that runs on standard Intel hardware.

Starting out with Asterisk

Asterisk is an Open Source PBX that has extensive voice and VoIP features. While Asterisk was developed for use with VoIP and has support for a wide range of VoIP protocols, it's perfectl possible to use it as a drop-in PBX replacement, without any VoIP capabilities on the system. Unfortunately, this is boring, and anyone who likes expensive Telco loop charges, and obscene long distance and international pricing, is more than welcome to build this themselves.

Asterisk was initially developed by Mark Spencer, the founder of Digium (, which continues to be the primary supporter of Asterisk development, as well as a supplier of hardware. Being a system that's `everything voice', Asterisk was named after the `*' character in Unix, literally meaning `anything and everything'. The slogan `any call, any time, from anywhere to anywhere' sums up the aims of the Asterisk project, and indeed the current capabilities of the software as of the 1.0 release.

Asterisk is designed to be able to transparently handle calls in, out and through the PBX without the user even knowing if it's going out through VoIP or a standard telephone line. Likewise, inbound callers don't need to know if the call is picked up by an employee at the office, or shipped halfway around the world to be handled by someone on a business trip to Japan.

Of course, being a little cheaper than a £15,000 PBX helps Asterisk to be more cost-effective for a small business or home user to install, as well as offering extreme flexibility with regard to managing voice communications.

Installing Asterisk

We're going to look at setting up Asterisk on a Linux system using pure IP for transport. This is a great first step because no hardware or telephony infrastructure is required, yet it gives a real-world example of VoIP.

As you would expect, a public Internet connection is required if any outbound calls are to be originated from the network, but the provisioning and configuration of such a service is outside the scope of this article. Ideally, the Asterisk system needs to have a dedicated, routable IP address, but we can get by with a non-routable IP address behind a firewall performing NAT.

Most distributions, including Fedora Core and Debian, have binary packages available for Asterisk, as well as the kernel modules and support libraries required. Unless voice hardware is being used to connect to a telephone network, the kernel-level support isn't required. Debian users can simply apt-get install asterisk to download and install the Asterisk system, or you can download the source code from

Compiling Asterisk is very simple, although it does require the compilation of libpri and zaptel for headers. Each of these can be installed with a standard make && make install command as root, and the standard builds of each will be sufficient for most users. Asterisk does take a short while to compile, but once installed, it will use some of the default configuration files shipped with the packages.

We can test out Asterisk install by running the following command as root:

# asterisk ­cvvvvv

Lots of startup information will fly by on the screen as each module loads, but eventually you'll be presented with a simple-looking prompt through which you're able to manage your entire Asterisk system.

 Asterisk Ready.

Asterisk command line

The Asterisk command line is very much like that of Cisco IOS, so anyone who is familiar with the popular router software will be able to find their way around very quickly. show version gives us the current version of Asterisk running, and all of the commands can be reviewed by typing ? at the CLI prompt. One of the most useful initial commands is show modules, which displays all modules that are part of the Asterisk suite. Each capability of Asterisk, such as telephony protocol provisions and codecs for audio compression, as well as the various applications that are installed as default, are shown as modules in Asterisk.

During the installation, Asterisk will set up a number of default SIP and IAX connections, which can be used for testing or for base configuration. We can review the presently connected SIP users, if any, with sip show users at the CLI (see the boxout `Reviewing SIP users at the CLI').

As we add SIP clients, or configure outbound SIP connections for delivering voice calls, this list will change, and we can review what's connected to our Asterisk system. Whenever we change the configuration, we can run reload from the command line, which will reload our Asterisk configuration and modules, without dropping any existing calls. Of course, if we break the configuration and impact the routing of connections, then new calls won't work very well.

Take a SIP

The first step for outbound calls is to configure a SIP user and connect a phone to our Asterisk system. Without spending money on a real phone, we can use one of the many soft phones available on the Internet ( that support SIP. Windows users can try X-Lite (, which is a great free SIP phone.

We configure SIP users in /etc/asterisk/sip.conf, and a standard SIP configuration looks something like this:

 port = 5060
 bindaddr =
 callerid="David Coulson" <100>

The above SIP configuration is fairly simple, but has all of the basic configuration entries that we would require for most clients. Once our SIP client has authenticated against our Asterisk system, we can call out on Digium `demo' extension ­Simply dial 500 and we should get a voice telling us we're connecting to Digium, followed by their IVR system. If extension 500 fails, checking the Asterisk console will give some cryptic information to aid in debugging the problem. Because the outbound connection to Digium is using IAX, it should work on any network.

Configuring an extension to call our SIP phone is done through /etc/asterisk/extensions.conf, and we can simply add a 100 extension to the default context. The extensions.conf file is split into numerous contexts, so depending upon how the call comes in, or how we choose to route the call, various contexts are used. Within contexts, extensions are defined as lists of priorities, with each being called in turn until we either get to the end, or one of the calls jumps us to a different priority.

For example, a busy device will jump us to the current extension + 101, so we can drop people in voicemail, rather than simply hang up on them. Our extension 100 configuration would look something along the lines of the following:

 exten => 100,1,Dial(SIP/david-l1,20,Ttr)
 exten => 100,2,VoiceMail(u100)
 exten => 100,3,Hangup()
 exten => 100,102,VoiceMail(b100)
 exten => 100,103,Hangup()

It really doesn't get much simpler than this. We try to call our david-l1 SIP user for 20 seconds and if no one answers, we jump to voicemail box 100 using the `unavailable' announcement. Should the david-l1 user be busy, we use the `busy' announcement. In both cases, we hang up on the caller when we're done with them to avoid keeping a phone circuit busy longer than it needs to be. With two SIP handsets, we should now be able to call in and out to each of them.

IAX to the Internet

The IAX2 protocol is used by a number of VoIP carriers because we can run multiple calls over a single UDP session, avoiding lots of separate connections, plus it will simply jump through a single firewall port. We're going to set up Asterisk using NuFone, a Michigan-based telephone company that goes to great lengths to support the Asterisk project and can provide low-cost US call termination to anyone in the world.

Depending upon your location in the world, a variety of VoIP carriers can be used to drop calls onto, and we can have as many connections to VoIP carriers as we choose. We start out by adding entries to /etc/asterisk/iax.conf for NuFone, and then register ourselves with their server. Registration avoids the need for a carrier to statically configure our IP address because our Asterisk system will simply tell the remote end every 30 seconds or so what our current IP address is as part of the IAX2 session.

register =>

We can verify that the above configuration is functional by doing iax2 show registry and iax2 show peers from the command line. To originate US-formatted calls onto NuFone, we can add a few entries to /etc/asterisk/extensions.conf, within the default context, and tell it where we want our calls to go. NuFone also offers origination of international calls to users, on a pre-paid pricing scheme, so we can add entries for these calls if we want to permit international calling.

exten => _91NXXNXXXXXX,1,SetCallerID(2169203100)
exten => _91NXXNXXXXXX,2,Dial(IAX2/dcoulson@NuFone/
exten => _91NXXNXXXXXX,3,Congestion
exten => _900_,1,SetCallerID(2169203100)
exten => _900_,2,Dial(IAX2/dcoulson@NuFone/${EXTEN:2})
exten => _900,3,Congestion

Going further with Asterisk

Building an Asterisk system is a complex business, and building it out with voicemail and interactive voice response (IVR) capabilities, as well as being able to reliably handle calls, is quite a challenge. However, we can start out with a very simple call router and add in all of the additional capabilities as we need them, or as they interest us.

For small to medium-sized businesses, Asterisk is a great choice when considering phone facilities, and it's used by thousands of existing installations as a production telephone system. As with any voice system, the expectation is significantly higher than that of IP services, and providing adequate backup and redundancy for any connectivity is a requirement. Rebooting a PBX during the day or having a switch port go bad for 20 minutes can cause real problems, and having a phone system up or down can make or break business relationships.

As with other Open Source projects, Asterisk has very active mailing lists, as well as IRC channels, which are great resources for new users or even experienced administrators who have a weird or complex question that needs an answer. For production environments, particularly for non-standard configurations, Digium offers consulting services, which for larger businesses is a great way to make sure everything is working as it should. LXF