How to create an Asterisk SIP Trunk on Debian 12

To Create An Asterisk SIP Trunk On Debian 12

Introduction:

Session Initiation Protocol (SIP) trunking is the digital method of making and receiving phone calls and other digital communication over an internet connection. The term trunk in SIP trunking refers to virtual phone lines that you can use to make phone calls to anyone with a phone number over the internet. SIP trunks not only carry voice calls but can also be used for multimedia communication that combines voice, video, and messaging services. SIP trunks are replacing analog and ISDN phone lines, with many countries now scheduling the termination of ISDN services.

Procedure:

Step 1: Check the OS version by using the below command.

root@linuxhelp1:~# cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
NAME="Debian GNU/Linux"
VERSION_ID="12"
VERSION="12 (bookworm)"
VERSION_CODENAME=bookworm
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

Step 2: Make Registration of Local Account and Trunk Server Account by using the below command.

root@linuxhelp1:~# vim /etc/asterisk/sip.conf
[linuxhelp2]
type=friend
host=192.168.6.130
context=internal
insecure=invite
allow=all


[1000]
type=friend
host=dynamic
context=internal
allow=all
secure=1000

Step 3: Make the Dialplan for both local and trunk by using the below command.

root@linuxhelp1:~# vim /etc/asterisk/extensions.conf
[internal]

exten => 1000,1,Dial(SIP/1000)
same=>n,Hangup()

exten => _2XXX,1,Dial(SIP/${EXTEN}@linuxhelp2)
same=>n,Hangup()

Step 4: Login to the asterisk console by using the below command.

root@linuxhelp1:~# asterisk -rvvvvvvvvvvvvvvvvvvvvvvvvv
Asterisk 20.4.0, Copyright (C) 1999 - 2022, Sangoma Technologies Corporation and others.
Created by Mark Spencer <markster@digium.com>
Asterisk comes with ABSOLUTELY NO WARRANTY; type 'core show warranty' for details.
This is free software, with components licensed under the GNU General Public
License version 2 and other licenses; you are welcome to redistribute it under
certain conditions. Type 'core show license' for details.
=========================================================================
Connected to Asterisk 20.4.0 currently running on linuxhelp1 (pid = 946)
linuxhelp1*CLI> 

Step 5: Load the SIP module by using the below command.

linuxhelp1*CLI> module load chan_sip
Loaded chan_sip
SIP channel loading...
  == SIP Listening on 0.0.0.0:5060
  == Using SIP CoS mark 4
    -- Message technology 'sip' registered.
  == Registered channel type 'SIP' (Session Initiation Protocol (SIP))
  == Registered RTP glue 'SIP'
  == Registered application 'SIPDtmfMode'
  == Registered application 'SIPAddHeader'
  == Registered application 'SIPRemoveHeader'
  == Registered custom function 'SIP_HEADER'
  == Registered custom function 'SIP_HEADERS'
  == Registered custom function 'SIPPEER'
  == Registered custom function 'CHECKSIPDOMAIN'
  == Manager registered action SIPpeers
  == Manager registered action SIPshowpeer
  == Manager registered action SIPqualifypeer
  == Manager registered action SIPshowregistry
  == Manager registered action SIPnotify
  == Manager registered action SIPpeerstatus
[Jun 22 08:42:51] WARNING[26399]: chan_sip.c:35472 deprecation_notice: chan_sip has no official maintainer and is deprecated.  Migration to
[Jun 22 08:42:51] WARNING[26399]: chan_sip.c:35473 deprecation_notice: chan_pjsip is recommended.  See guides at the Asterisk Wiki:
[Jun 22 08:42:51] WARNING[26399]: chan_sip.c:35474 deprecation_notice: https://wiki.asterisk.org/wiki/display/AST/Migrating+from+chan_sip+to+res_pjsip
[Jun 22 08:42:51] WARNING[26399]: chan_sip.c:35475 deprecation_notice: https://wiki.asterisk.org/wiki/display/AST/Configuring+res_pjsip
 Loaded chan_sip.so => (Session Initiation Protocol (SIP))

Step 6: Reload the dialplan by using the below command.

linuxhelp1*CLI> dialplan reload
Dialplan reloaded.
    -- Time to scan old dialplan and merge leftovers back into the new: 0.000008 sec
    -- Time to restore hints and swap in new dialplan: 0.000009 sec
    -- Time to delete the old dialplan: 0.000007 sec
    -- Total time merge_contexts_delete: 0.000024 sec
    -- pbx_config successfully loaded 1 contexts (enable debug for details).

Step 7: Take control the Second server by using the below command.

root@linuxhelp1:~# ssh root@192.168.6.130
root@192.168.6.130's password: 
Linux linuxhelp2 6.1.0-17-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.69-1 (2023-12-30) x86_64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Sat Jun 22 08:23:15 2024 from 192.168.6.131
root@linuxhelp2:~# 

Step 8: Make Registration of Local Account and Trunk Server Account by using the below command.

root@linuxhelp2:~# vim /etc/asterisk/sip.conf
[linuxhelp1]
type=friend
host=192.168.6.131
context=internal
insecure=invite
allow=all


[2000]
type=friend
host=dynamic
context=internal
secure=2000
allow=all

Step 9: Make the Dialplan for both local and trunk by using the below command.

root@linuxhelp2:~# vim /etc/asterisk/extensions.conf
[internal]

exten => 2000,1,Dial(SIP/2000)
same=>n,Hangup()

exten => _1XXX,1,Dial(SIP/${EXTEN}@linuxhelp1)
same=>n,Hangup()

Step 10: Login to the asterisk console by using the below command.

root@linuxhelp2:~# asterisk -rvvvvvvvvvvvvvvvv
Asterisk 20.4.0, Copyright (C) 1999 - 2022, Sangoma Technologies Corporation and others.
Created by Mark Spencer <markster@digium.com>
Asterisk comes with ABSOLUTELY NO WARRANTY; type 'core show warranty' for details.
This is free software, with components licensed under the GNU General Public
License version 2 and other licenses; you are welcome to redistribute it under
certain conditions. Type 'core show license' for details.
=========================================================================
Connected to Asterisk 20.4.0 currently running on linuxhelp2 (pid = 1108)
linuxhelp2*CLI> 

Step 11: Load the sip module by using the below command.

linuxhelp2*CLI> module load chan_sip
Loaded chan_sip
SIP channel loading...
[Jun 22 08:47:11] ERROR[14541]: netsock2.c:303 ast_sockaddr_resolve: getaddrinfo("linuxhelp2", "(null)", ...): Name or service not known
[Jun 22 08:47:11] WARNING[14541]: acl.c:890 resolve_first: Unable to lookup 'linuxhelp2'
  == SIP Listening on 0.0.0.0:5060
  == Using SIP CoS mark 4
    -- Message technology 'sip' registered.
  == Registered channel type 'SIP' (Session Initiation Protocol (SIP))
  == Registered RTP glue 'SIP'
  == Registered application 'SIPDtmfMode'
  == Registered application 'SIPAddHeader'
  == Registered application 'SIPRemoveHeader'
  == Registered custom function 'SIP_HEADER'
  == Registered custom function 'SIP_HEADERS'
  == Registered custom function 'SIPPEER'
  == Registered custom function 'CHECKSIPDOMAIN'
  == Manager registered action SIPpeers
  == Manager registered action SIPshowpeer
  == Manager registered action SIPqualifypeer
  == Manager registered action SIPshowregistry
  == Manager registered action SIPnotify
  == Manager registered action SIPpeerstatus
[Jun 22 08:47:11] WARNING[14541]: chan_sip.c:35472 deprecation_notice: chan_sip has no official maintainer and is deprecated.  Migration to
[Jun 22 08:47:11] WARNING[14541]: chan_sip.c:35473 deprecation_notice: chan_pjsip is recommended.  See guides at the Asterisk Wiki:
[Jun 22 08:47:11] WARNING[14541]: chan_sip.c:35474 deprecation_notice: https://wiki.asterisk.org/wiki/display/AST/Migrating+from+chan_sip+to+res_pjsip
[Jun 22 08:47:11] WARNING[14541]: chan_sip.c:35475 deprecation_notice: https://wiki.asterisk.org/wiki/display/AST/Configuring+res_pjsip
 Loaded chan_sip.so => (Session Initiation Protocol (SIP))

Step 12: Reload the dialplan by using the below command.

linuxhelp2*CLI> dialplan reload
Dialplan reloaded.
    -- Time to scan old dialplan and merge leftovers back into the new: 0.000007 sec
    -- Time to restore hints and swap in new dialplan: 0.000008 sec
    -- Time to delete the old dialplan: 0.000008 sec
    -- Total time merge_contexts_delete: 0.000023 sec
    -- pbx_config successfully loaded 1 contexts (enable debug for details).

Step 13: Make the Test call from the second server sip to first server sip by using microsip softphone as shown in below image.

Server 1:
  == Using SIP RTP CoS mark 5
       > 0x7eff8c00c8d0 -- Strict RTP learning after remote address set to: 192.168.6.130:18850
    -- Executing [1000@internal:1] Dial("SIP/linuxhelp2-00000000", "SIP/1000") in new stack
  == Using SIP RTP CoS mark 5
    -- Called SIP/1000
    -- SIP/1000-00000001 is ringing
       > 0x7effb0002d80 -- Strict RTP learning after remote address set to: 192.168.6.104:4002
    -- SIP/1000-00000001 answered SIP/linuxhelp2-00000000
    -- Channel SIP/1000-00000001 joined 'simple_bridge' basic-bridge <f888bfe7-6122-47f3-9e0d-c8d35abd7211>
    -- Channel SIP/linuxhelp2-00000000 joined 'simple_bridge' basic-bridge <f888bfe7-6122-47f3-9e0d-c8d35abd7211>
       > Bridge f888bfe7-6122-47f3-9e0d-c8d35abd7211: switching from simple_bridge technology to native_rtp
       > Remotely bridged 'SIP/linuxhelp2-00000000' and 'SIP/1000-00000001' - media will flow directly between them
       > 0x7effb0002d80 -- Strict RTP learning after remote address set to: 192.168.6.104:4002
       > 0x7eff8c00c8d0 -- Strict RTP learning after remote address set to: 192.168.6.130:18850
       > 0x7eff8c00c8d0 -- Strict RTP learning after remote address set to: 192.168.6.104:4000
       > 0x7effb0002d80 -- Strict RTP learning after remote address set to: 192.168.6.104:4002
    -- Channel SIP/1000-00000001 left 'native_rtp' basic-bridge <f888bfe7-6122-47f3-9e0d-c8d35abd7211>
    -- Channel SIP/linuxhelp2-00000000 left 'native_rtp' basic-bridge <f888bfe7-6122-47f3-9e0d-c8d35abd7211>
  == Spawn extension (internal, 1000, 1) exited non-zero on 'SIP/linuxhelp2-00000000'
       > 0x7eff8c00c8d0 -- Strict RTP learning after remote address set to: 192.168.6.104:4000
Server 2:
[Jun 22 08:48:03] ERROR[14545][C-00000002]: netsock2.c:303 ast_sockaddr_resolve: getaddrinfo("linuxhelp2", "(null)", ...): Name or service not known
[Jun 22 08:48:03] WARNING[14545][C-00000002]: acl.c:890 resolve_first: Unable to lookup 'linuxhelp2'
  == Using SIP RTP CoS mark 5
       > 0x7f9ee00033b0 -- Strict RTP learning after remote address set to: 192.168.6.104:4000
    -- Executing [1000@internal:1] Dial("SIP/2000-00000000", "SIP/1000@linuxhelp1") in new stack
[Jun 22 08:48:03] ERROR[14547][C-00000002]: netsock2.c:303 ast_sockaddr_resolve: getaddrinfo("linuxhelp2", "(null)", ...): Name or service not known
[Jun 22 08:48:03] WARNING[14547][C-00000002]: acl.c:890 resolve_first: Unable to lookup 'linuxhelp2'
  == Using SIP RTP CoS mark 5
    -- Called SIP/1000@linuxhelp1
    -- SIP/linuxhelp1-00000001 is ringing
       > 0x7f9ef001db60 -- Strict RTP learning after remote address set to: 192.168.6.131:19406
    -- SIP/linuxhelp1-00000001 answered SIP/2000-00000000
    -- Channel SIP/linuxhelp1-00000001 joined 'simple_bridge' basic-bridge <665bdb35-f35d-453d-a966-0d5ae6eeff15>
    -- Channel SIP/2000-00000000 joined 'simple_bridge' basic-bridge <665bdb35-f35d-453d-a966-0d5ae6eeff15>
       > Bridge 665bdb35-f35d-453d-a966-0d5ae6eeff15: switching from simple_bridge technology to native_rtp
       > 0x7f9ef001db60 -- Strict RTP learning after remote address set to: 192.168.6.104:4002
       > Remotely bridged 'SIP/2000-00000000' and 'SIP/linuxhelp1-00000001' - media will flow directly between them
       > 0x7f9ee00033b0 -- Strict RTP switching to RTP target address 192.168.6.104:4000 as source
       > 0x7f9ef001db60 -- Strict RTP learning after remote address set to: 192.168.6.104:4002
       > 0x7f9ef001db60 -- Strict RTP switching to RTP target address 192.168.6.104:4002 as source
       > 0x7f9ee00033b0 -- Strict RTP learning complete - Locking on source address 192.168.6.104:4000
       > 0x7f9ef001db60 -- Strict RTP learning complete - Locking on source address 192.168.6.104:4002
       > 0x7f9ef001db60 -- Strict RTP learning after remote address set to: 192.168.6.131:19406
    -- Channel SIP/linuxhelp1-00000001 left 'native_rtp' basic-bridge <665bdb35-f35d-453d-a966-0d5ae6eeff15>
    -- Channel SIP/2000-00000000 left 'native_rtp' basic-bridge <665bdb35-f35d-453d-a966-0d5ae6eeff15>
  == Spawn extension (internal, 1000, 1) exited non-zero on 'SIP/2000-00000000'

Step 14: Make the Test call from the first server sip to second server sip by using microsip softphone as shown in below image.

Server 1:
  == Using SIP RTP CoS mark 5
       > 0x7eff8c00c0b0 -- Strict RTP learning after remote address set to: 192.168.6.104:4004
    -- Executing [2000@internal:1] Dial("SIP/1000-00000002", "SIP/2000@linuxhelp2") in new stack
  == Using SIP RTP CoS mark 5
    -- Called SIP/2000@linuxhelp2
    -- SIP/linuxhelp2-00000003 is ringing
       > 0x7effb001e5f0 -- Strict RTP learning after remote address set to: 192.168.6.130:14164
    -- SIP/linuxhelp2-00000003 answered SIP/1000-00000002
       > 0x7effb001e5f0 -- Strict RTP learning after remote address set to: 192.168.6.104:4002
    -- Channel SIP/linuxhelp2-00000003 joined 'simple_bridge' basic-bridge <eab1da37-df92-493a-a5c4-35f2d77aff0c>
    -- Channel SIP/1000-00000002 joined 'simple_bridge' basic-bridge <eab1da37-df92-493a-a5c4-35f2d77aff0c>
       > Bridge eab1da37-df92-493a-a5c4-35f2d77aff0c: switching from simple_bridge technology to native_rtp
       > Remotely bridged 'SIP/1000-00000002' and 'SIP/linuxhelp2-00000003' - media will flow directly between them
       > 0x7effb001e5f0 -- Strict RTP learning after remote address set to: 192.168.6.104:4002
       > 0x7eff8c00c0b0 -- Strict RTP switching to RTP target address 192.168.6.104:4004 as source
       > 0x7effb001e5f0 -- Strict RTP switching to RTP target address 192.168.6.104:4002 as source
       > 0x7eff8c00c0b0 -- Strict RTP learning complete - Locking on source address 192.168.6.104:4004
       > 0x7effb001e5f0 -- Strict RTP learning complete - Locking on source address 192.168.6.104:4002
    -- Channel SIP/1000-00000002 left 'native_rtp' basic-bridge <eab1da37-df92-493a-a5c4-35f2d77aff0c>
  == Spawn extension (internal, 2000, 1) exited non-zero on 'SIP/1000-00000002'
    -- Channel SIP/linuxhelp2-00000003 left 'native_rtp' basic-bridge <eab1da37-df92-493a-a5c4-35f2d77aff0c>
Server 2:
 [Jun 22 08:48:29] ERROR[14545][C-00000003]: netsock2.c:303 ast_sockaddr_resolve: getaddrinfo("linuxhelp2", "(null)", ...): Name or service not known
[Jun 22 08:48:29] WARNING[14545][C-00000003]: acl.c:890 resolve_first: Unable to lookup 'linuxhelp2'
  == Using SIP RTP CoS mark 5
       > 0x7f9ee00033b0 -- Strict RTP learning after remote address set to: 192.168.6.131:15388
    -- Executing [2000@internal:1] Dial("SIP/linuxhelp1-00000002", "SIP/2000") in new stack
[Jun 22 08:48:29] ERROR[14552][C-00000003]: netsock2.c:303 ast_sockaddr_resolve: getaddrinfo("linuxhelp2", "(null)", ...): Name or service not known
[Jun 22 08:48:29] WARNING[14552][C-00000003]: acl.c:890 resolve_first: Unable to lookup 'linuxhelp2'
  == Using SIP RTP CoS mark 5
    -- Called SIP/2000
    -- SIP/2000-00000003 is ringing
       > 0x7f9ef0002120 -- Strict RTP learning after remote address set to: 192.168.6.104:4002
    -- SIP/2000-00000003 answered SIP/linuxhelp1-00000002
    -- Channel SIP/2000-00000003 joined 'simple_bridge' basic-bridge <b8aae704-3713-4007-b408-6d7db4685641>
       > 0x7f9ef0002120 -- Strict RTP switching to RTP target address 192.168.6.104:4002 as source
    -- Channel SIP/linuxhelp1-00000002 joined 'simple_bridge' basic-bridge <b8aae704-3713-4007-b408-6d7db4685641>
       > Bridge b8aae704-3713-4007-b408-6d7db4685641: switching from simple_bridge technology to native_rtp
       > Remotely bridged 'SIP/linuxhelp1-00000002' and 'SIP/2000-00000003' - media will flow directly between them
       > 0x7f9ee00033b0 -- Strict RTP learning after remote address set to: 192.168.6.131:15388
       > 0x7f9ee00033b0 -- Strict RTP learning after remote address set to: 192.168.6.104:4004
       > 0x7f9ee00033b0 -- Strict RTP learning after remote address set to: 192.168.6.131:15388
    -- Channel SIP/linuxhelp1-00000002 left 'native_rtp' basic-bridge <b8aae704-3713-4007-b408-6d7db4685641>
    -- Channel SIP/2000-00000003 left 'native_rtp' basic-bridge <b8aae704-3713-4007-b408-6d7db4685641>
  == Spawn extension (internal, 2000, 1) exited non-zero on 'SIP/linuxhelp1-00000002'

Conclusion:

We have reached the end of this article. In this guide, we have walked you through the steps required to create an Asterisk SIP Trunk on Debian 12. Your feedback is much welcome.

FAQ
Q
What security considerations should I keep in mind when using SIP trunks with Asterisk?
A
When using SIP trunks with Asterisk, it's essential to consider security measures such as:
Using strong passwords and encryption for SIP trunk authentication.
Restricting access to SIP ports and services through firewall rules.
Regularly updating Asterisk and associated software to patch security vulnerabilities.
Implementing fail2ban or similar tools to mitigate SIP scanning and brute-force attacks.
Monitoring Asterisk logs and network traffic for suspicious activity or unauthorized access attempts.
Q
How do I troubleshoot SIP trunk issues in Asterisk?
A
Common troubleshooting steps for SIP trunk issues in Asterisk include:
Checking the configuration files (sip.conf or pjsip.conf) for errors or misconfigurations.
Verifying network connectivity and firewall settings to ensure that Asterisk can communicate with the SIP provider.
Reviewing Asterisk logs (/var/log/asterisk) for any error messages related to SIP trunk registration or call setup.
Using tools like Wireshark to capture SIP traffic and analyze communication between Asterisk and the SIP provider.
Q
What are the benefits of using SIP trunks with Asterisk?
A
Using SIP trunks with Asterisk offers several benefits:
Cost savings: SIP trunks are usually more cost-effective than traditional phone lines for making calls.
Scalability: SIP trunks can easily scale to accommodate more channels or users as your business grows.
Flexibility: SIP trunks allow for integration with various communication applications and services.
Geographic flexibility: SIP trunks enable virtual phone numbers from different geographic locations, enhancing your business's global presence.
Q
How do I configure SIP trunks in Asterisk?
A
To configure SIP trunks in Asterisk, you typically edit the sip.conf or pjsip.conf configuration files. You define parameters such as the SIP provider's server address, authentication details (username and password), codecs, and other settings required for establishing and maintaining the SIP trunk connection.
Q
What is Asterisk SIP trunking?
A
Asterisk SIP trunking involves configuring Asterisk, an open-source PBX software, to establish SIP (Session Initiation Protocol) trunks with SIP service providers. SIP trunks enable the transmission of voice and other media over the internet, replacing traditional phone lines.