Ubuntu 22.04 安装 Asterisk 指南

Ubuntu 22.04 安装 Asterisk 双轨实录:源码编译 22.11.0 LTS 与 apt 直装 18.10 对照,systemd Type=notify 托管、RTP 端口段收窄至 10000-20000 全程实测验证。

Asterisk即时通信Asterisk2026/10/22

Ubuntu 22.04 安装 Asterisk 指南

目标:在 Ubuntu 22.04 服务器上从零安装 Asterisk 开源电话交换系统(PBX),覆盖源码编译与 apt 仓库两条安装路线的完整流程(依赖 → 编译/安装 → 示例配置 → systemd 托管 → 验证),并完成 RTP 媒体端口段收窄的实测闭环。两条路线均已在 Ubuntu 22.04 x86_64 环境实测通过:源码版 Asterisk 22.11.0(当前最新 LTS)与仓库版 Asterisk 18.10.0。组件定位与选型背景见 《Asterisk 22.11.0 介绍与选型指南》。

一、环境信息

本文所有命令与回显均在以下环境实测采集:

  • 操作系统:Ubuntu 22.04 LTS(x86_64);
  • 源码编译版:Asterisk 22.11.0(官方源码包 asterisk-22.11.0.tar.gz,约 26MB);
  • apt 仓库版:Asterisk 1:18.10.0~dfsg+~cs6.10.40431411-2(Ubuntu 22.04 jammy/universe 官方仓库);
  • 安装路径:主程序 /usr/sbin/asterisk,模块目录 /usr/lib/asterisk/modules,配置目录 /etc/asterisk,数据目录 /var/lib/asterisk,日志目录 /var/log/asterisk;
  • systemd 服务单元:源码版 /etc/systemd/system/asterisk.service(手工部署);apt 版 /lib/systemd/system/asterisk.service(软件包自带);
  • 媒体端口:RTP/UDP 10000-20000(本文完成收窄,默认 5000-31000);SIP 信令 UDP/TCP 5060。

二、前置条件

本章完成两条安装路线共用的系统准备:确认系统版本、更新软件源索引、安装基础网络工具。

先确认操作系统版本与架构:

lsb_release -a

实测回显:

No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 22.04.5 LTS
Release:	22.04
Codename:	jammy

更新软件源索引,保证后续依赖安装能命中最新可用版本:

apt-get update

实测回显(输出节选,实测首尾):

Get:7 http://archive.ubuntu.com/ubuntu jammy-updates/universe amd64 Packages [1628 kB]
Get:8 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages [4759 kB]
Fetched 12.4 MB in 7s (1659 kB/s)
Reading package lists...

💡 说明:apt-get update 只刷新软件源索引、不升级已装软件;回显里的 Fetched 12.4 MB 与耗时因网络环境而异,只要末尾出现 Reading package lists... 无 Err 行即代表索引刷新成功。

三、版本选型说明

Asterisk 官方对 Ubuntu 不提供自建 apt 仓库,两条安装路线的版本来源与差异如下表:

维度方案 A:源码编译方案 B:Ubuntu 仓库 apt 直装
可安装版本官方全部发布版(本文取最新 LTS 22.11.0)随发行版固定:18.10.0~dfsg+~cs6.10.40431411-2
版本来源downloads.asterisk.org 官方源码包jammy/universe 官方仓库
是否需编译是(约 10-20 分钟,视 CPU 而定)否(一条命令完成)
systemd 单元来源源码树 contrib/systemd/ 模板手工部署软件包自带,装好即托管
配置文件目录/etc/asterisk(make samples 生成全套样例)/etc/asterisk(软件包自带默认配置)
适合场景需要最新 LTS/新特性、自定义模块取舍快速验证、老版本足够、免维护编译环境

验证仓库当前可装的版本(Candidate 即 apt 路线的实际安装版本):

apt-cache policy asterisk

实测回显:

asterisk:
  Installed: 1:18.10.0~dfsg+~cs6.10.40431411-2
  Candidate: 1:18.10.0~dfsg+~cs6.10.40431411-2
  Version table:
 *** 1:18.10.0~dfsg+~cs6.10.40431411-2 500
        500 http://archive.ubuntu.com/ubuntu jammy/universe amd64 Packages

💡 说明:Ubuntu 22.04 仓库中的 Asterisk 停留在 18.10(上一代 LTS),官方最新 22.x 系列只能走源码编译。两条路线选择建议:要最新 LTS、生产长期使用选方案 A;快速体验、内网测试选方案 B。

四、方案 A:源码编译安装 Asterisk 22.11.0

4.1 方案说明

本章从官方源码包编译安装当前最新 LTS 版本 Asterisk 22.11.0,完整走通依赖安装 → 下载解压 → configure → make → make install → make samples → systemd 托管 → 验证的全流程。

4.2 安装编译依赖

编译 Asterisk 22 需要编译工具链与若干开发库:build-essential 提供 gcc/make,libxml2-dev 是必选依赖(XML 解析),libjansson-dev 供内置 JSON 支持,libsqlite3-dev 供内置数据库,libncurses-dev 供 CLI 控制台,libssl-dev 供 TLS/SRTP,uuid-dev、libedit-dev、libsrtp2-dev、libnss3-dev 供媒体与加密模块,pkg-config 供依赖探测:

apt-get install -y build-essential libxml2-dev libjansson-dev libsqlite3-dev \
  libncurses-dev libssl-dev uuid-dev libedit-dev libsrtp2-dev libnss3-dev \
  pkg-config wget curl

实测回显(依赖已装齐的环境重跑,输出节选;全新机器会逐包列出下载与解包进度后收尾于同一行汇总):

uuid-dev is already the newest version (2.37.2-4ubuntu3.6).
wget is already the newest version (1.21.2-2ubuntu1.5).
0 upgraded, 0 newly installed, 0 to remove and 2 not upgraded.

⚠️ 注意:全新 Ubuntu 22.04 执行该命令会实际下载约 100MB 依赖包(含 gcc-11、libc6-dev 等自动依赖),耗时 1-5 分钟视网络而定;只要末行出现 0 upgraded, N newly installed 或 0 newly installed 均代表依赖就绪。

4.3 创建运行用户

Asterisk 不建议以 root 运行,编译安装前先创建系统用户 asterisk(-r 建系统账户,-U 同时创建同名主组):

useradd -r -U asterisk

验证用户已创建:

id asterisk

实测回显:

uid=999(asterisk) gid=999(asterisk) groups=999(asterisk)

💡 说明:uid=999 是系统用户段的典型分配值,新机器上的具体 uid/gid 可能不同,只要 id asterisk 能输出用户与同名组即为成功。

4.4 下载并解压源码包

从官方下载站获取源码包:

cd /root
wget https://downloads.asterisk.org/pub/telephony/asterisk/asterisk-22.11.0.tar.gz

确认包体完整:

ls -lh asterisk-22.11.0.tar.gz

实测回显:

-rw-r--r-- 1 root root 26M Aug 27 15:30 asterisk-22.11.0.tar.gz

解压并进入源码目录:

tar xzf asterisk-22.11.0.tar.gz
cd asterisk-22.11.0

4.5 configure 配置编译选项

./configure 探测系统环境并生成编译配置(makeopts)。不加参数时使用默认安装前缀 /usr,模块装入 /usr/lib/asterisk/modules:

./configure

实测回显(输出节选,实测首尾):

checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking for gcc... gcc
checking whether the C compiler works... yes
...
config.status: creating makeopts
config.status: creating include/asterisk/autoconfig.h

               .$$$$$$$$$$$$$$$=..
            .$7$7..          .7$$7:.
          .$$:.                 ,$7.7
        .$7.     7$$$$           .$$77
     ..$$.       $$$$$            .$$$.
   $.$.   .$$$7. $$$$7 .7$$$.      .$$$.
 .777.   .$$$$$$77$$$77$$$$$7.      $$$,
 $$$~      .7$$$$$$$$$$$$$7.       .$$$.
.$$7          .7$$$$$$$7:          ?$$$.
$$$          ?7$$$$$$$$$$I        .$$$7
$$$       .7$$$$$$$$$$$$$$$$      :$$$.
$$$       $$$$$$7$$$$$$$$$$$$    .$$$.
$$$        $$$   7$$$7  .$$$    .$$$.
$$$$             $$$$7         .$$$.
7$$$7            7$$$$        7$$$
 $$$$$                        $$$
  $$$$7.                       $$  (TM)
   $$$$$$$.           .7$$$$$$  $$
     $$$$$$$$$$$$7$$$$$$$$$.$$$$$$
       $$$$$$$$$$$$$$$$.

configure: Package configured for:
configure: OS type  : linux-gnu
configure: Host CPU : x86_64
configure: build-cpu:vendor:os: x86_64 : pc : linux-gnu :
configure: host-cpu:vendor:os: x86_64 : pc : linux-gnu :

💡 说明:末尾的 ASCII 字符画与 Package configured for: 摘要是 configure 成功的标志;若中途报 error: *** XML configure failed,通常是缺 libxml2-dev,补装后重跑即可(见「八、常见问题」)。

4.6 编译与安装

先编译(-j 按 CPU 核数并行加速),后安装。编译耗时约 10-20 分钟:

make -j$(nproc)

实测回显(输出节选,实测尾段;此前为数百行逐模块 [CC]/[LD] 编译日志):

   [LD] res_timing_pthread.o -> res_timing_pthread.so
   [LD] res_timing_timerfd.o -> res_timing_timerfd.so
   [LD] res_pjsip_session.o res_pjsip_session/pjsip_session_caps.o res_pjsip_session/pjsip_session_reason_header.o -> res_pjsip_session.so
   [LD] res_websocket_client.o -> res_websocket_client.so
   [LD] res_rtp_asterisk.o -> res_rtp_asterisk.so
Building Documentation For: channels pbx apps codecs formats cdr cel bridges funcs tests main res addons
 +--------- Asterisk Build Complete ---------+
 + Asterisk has successfully been built, and +
 + can be installed by running:              +
 +                                           +
 +                make install               +
 +-------------------------------------------+

编译完成后安装到系统目录:

make install

实测回显(输出节选,实测尾段;此前为逐文件安装日志):

 +                                           +
 + For a sample basic PBX:                   +
 +    make basic-pbx                         +
 +                                           +
 +                                           +
 +-----------------  or ---------------------+
 +                                           +
 + You can go ahead and install the asterisk +
 + program documentation now or later run:   +
 +                                           +
 +               make progdocs               +
 +                                           +
 + **Note** This requires that you have      +
 + doxygen installed on your local system    +
 +-------------------------------------------+

💡 说明:安装完成框中给出的两个可选后续——make basic-pbx 安装一份精简可用的最小 PBX 示例配置,make progdocs 需 doxygen 生成开发文档;本文下一步用 make samples 安装全套配置样例,便于逐项学习各配置文件。

4.7 安装示例配置(make samples)

make samples 把全部模块的示例配置装入 /etc/asterisk(含 pjsip.conf、rtp.conf、extensions.conf 等):

make samples

实测回显(输出节选,实测尾段;此前为逐文件安装日志):

/usr/bin/install -c -d "/var/spool/asterisk/voicemail/default/1234/INBOX"
build_tools/make_sample_voicemail "//var/lib/asterisk" "//var/spool/asterisk"
Installing file phoneprov/000000000000-directory.xml
Installing file phoneprov/000000000000-phone.cfg
Installing file phoneprov/000000000000.cfg
Installing file phoneprov/polycom.xml
Installing file phoneprov/polycom_line.xml
Installing file phoneprov/snom-mac.xml

验证配置目录已生成:

ls /etc/asterisk/asterisk.conf /etc/asterisk/rtp.conf /etc/asterisk/pjsip.conf

实测回显:

/etc/asterisk/asterisk.conf
/etc/asterisk/pjsip.conf
/etc/asterisk/rtp.conf

💡 技术拓展:Asterisk 的 SIP 通道驱动有新旧两代实现,安装后默认模块集中两者并存——

驱动配置文件状态适用场景
chan_pjsippjsip.conf当前主线,新特性只在此演进新部署一律首选
chan_sipsip.conf旧驱动,22.x 起已移除仅维护存量老配置
chan_dahdichan_dahdi.conf接传统 T1/E1/PRI 硬件线对接运营商 ISDN 线路

本文及 Asterisk 22 实际使用 chan_pjsip(验证见 4.9 节模块列表)。

4.8 systemd 托管(Type=notify)

源码安装不自带 systemd 单元,需从源码树 contrib/systemd/ 复制官方模板并部署:

cp contrib/systemd/asterisk.service /etc/systemd/system/asterisk.service

在 [Service] 段末尾追加两行 RuntimeDirectory 指令,让 systemd 在启动时自动创建 /var/run/asterisk 套接字目录(权限 0755):

RuntimeDirectory=asterisk
RuntimeDirectoryMode=0755

与官方模板的差异可用 diff 确认(实测仅新增这两行):

diff contrib/systemd/asterisk.service /etc/systemd/system/asterisk.service

实测回显:

31a32,33
> RuntimeDirectory=asterisk
> RuntimeDirectoryMode=0755

部署后的完整单元文件内容如下(cat /etc/systemd/system/asterisk.service 实测):

[Unit]
Description=Asterisk PBX and telephony daemon.
After=network.target
#include these if asterisk need to bind to a specific IP (other than 0.0.0.0)
#Wants=network-online.target
#After=network-online.target network.target

[Service]
Type=notify
Environment=HOME=/var/lib/asterisk
#if systemd do not provide hostname and you need to use ${ENV(HOSTNAME)}
#Environment=HOSTNAME=%H
WorkingDirectory=/var/lib/asterisk
User=asterisk
Group=asterisk
ExecStart=/usr/sbin/asterisk -mqf -C /etc/asterisk/asterisk.conf
ExecReload=/usr/sbin/asterisk -rx 'core reload'
#if /var/run is a tmpfs, this will create /var/run/asterisk on start
#RuntimeDirectory=asterisk

#Nice=0
#UMask=0002
LimitCORE=infinity
#LimitNOFILE=
Restart=always
RestartSec=4

# Prevent duplication of logs with color codes to /var/log/messages
StandardOutput=null

PrivateTmp=true
RuntimeDirectory=asterisk
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target

逐项说明关键指令:

  • Type=notify:Asterisk 完成全部模块加载后通过 sd_notify 向 systemd 报告就绪,systemd 此刻才把服务标记为 Started——保证依赖它的服务(如反向代理、呼叫路由脚本)启动时 Asterisk 已可用;
  • User=asterisk / Group=asterisk:以 4.3 节创建的非 root 用户运行;
  • ExecStart=/usr/sbin/asterisk -mqf -C /etc/asterisk/asterisk.conf:-f 前台运行(由 systemd 接管生命周期)、-q 安静模式、-m 消息色彩重定向、-C 指定主配置;
  • ExecReload=/usr/sbin/asterisk -rx 'core reload':systemctl reload 触发 Asterisk 热重载配置;
  • Restart=always / RestartSec=4:进程异常退出后 4 秒自动拉起;
  • RuntimeDirectory=asterisk:本文新增的两行,启动时自动建 /var/run/asterisk 并按属主授权。

💡 技术拓展:systemd 服务类型(Type=)决定"何时算启动成功",常见取值对比——

Type就绪判定适用场景
simpleExecStart 进程一 fork 即就绪前台常驻、无需就绪信号
forking父进程退出后跟踪留下的子进程传统自守护进程的脚本
notify进程主动发 sd_notify READY 才就绪支持就绪通知的现代守护进程
dbus / oneshot总线注册完成 / 命令执行完D-Bus 服务 / 一次性任务

Asterisk 官方 contrib 模板采用 notify:模块加载完成前 systemd 不会判定其就绪,避免"半启动"状态被上游依赖调用。

加载单元并设置开机自启(逐条执行):

systemctl daemon-reload
systemctl enable asterisk

实测回显:

Synchronizing state of asterisk.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable asterisk
Created symlink /etc/systemd/system/multi-user.target.wants/asterisk.service → /etc/systemd/system/asterisk.service.

4.9 启动与验证

启动服务:

systemctl start asterisk

验证服务状态:

systemctl is-active asterisk

实测回显:

active

查看详细状态(输出节选,实测省略 CGroup 与日志明细行):

systemctl status asterisk

实测回显(输出节选):

● asterisk.service - Asterisk PBX and telephony daemon.
     Loaded: loaded (/etc/systemd/system/asterisk.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2026-10-21 11:48:01 UTC; 4h 23min ago
   Main PID: 107 (asterisk)
      Tasks: 70 (limit: 94371)
     Memory: 41.9M
        CPU: 1min 14.329s

确认主进程确以 asterisk 用户、按单元定义的参数运行:

ps -o user,pid,args -C asterisk

实测回显:

USER         PID COMMAND
asterisk     107 /usr/sbin/asterisk -mqf -C /etc/asterisk/asterisk.conf

通过远程 CLI 应答确认 Asterisk 内核真正可响应命令(Type=notify 托管成功的最终判据):

asterisk -rx "core show version"

实测回显:

Asterisk 22.11.0 built by root @ 7d9aaf7362ca on a x86_64 running Linux on 2026-10-21 11:21:56 UTC
asterisk -rx "core show uptime"

实测回显:

System uptime: 4 hours, 25 minutes, 57 seconds
Last reload: 4 hours, 25 minutes, 57 seconds

确认 SIP 通道主线模块 chan_pjsip 已随内核加载运行(输出节选):

asterisk -rx "module show like pjsip"

实测回显(输出节选,实测首尾):

Module                         Description                              Use Count  Status      Support Level
chan_pjsip.so                  PJSIP Channel Driver                     0          Running              core
func_pjsip_aor.so              Get information about a PJSIP AOR        0          Running              core
func_pjsip_contact.so          Get information about a PJSIP contact    0          Running              core
func_pjsip_endpoint.so         Get information about a PJSIP endpoint   0          Running              core
res_pjsip.so                   Basic SIP resource                       49         Running              core
res_pjsip_acl.so               PJSIP ACL Resource                       0          Running              core
...
37 modules loaded

⚠️ 注意:module show like pjsip 末行的模块总数因编译选项与版本而异,以 chan_pjsip.so ... Running 与 res_pjsip.so ... Running 出现为准。

五、方案 B:Ubuntu 仓库 apt 直装 Asterisk 18

5.1 方案说明

本章用 Ubuntu 22.04 官方仓库一条命令安装 Asterisk 18.10.0(上一代 LTS),软件包自带 systemd 单元与全套默认配置,安装即完成托管,适合快速验证与内网测试。

5.2 安装命令与包构成

apt-get install -y asterisk

实测回显(已装环境重跑,输出节选;全新机器会列出下载与解包进度后收尾于同一行汇总):

Reading state information...
asterisk is already the newest version (1:18.10.0~dfsg+~cs6.10.40431411-2).
0 upgraded, 0 newly installed, 0 to remove and 3 not upgraded.

该元包实际拉起的组件(全新安装后可见,实测 dpkg -l 节选):

dpkg -l | grep asterisk

实测回显:

ii  asterisk                    1:18.10.0~dfsg+~cs6.10.40431411-2           amd64        Open Source Private Branch Exchange (PBX)
ii  asterisk-config             1:18.10.0~dfsg+~cs6.10.40431411-2           all          Configuration files for Asterisk
ii  asterisk-core-sounds-en     1.6.1-1                                     all          asterisk PBX sound files - US English
ii  asterisk-core-sounds-en-gsm 1.6.1-1                                     all          asterisk PBX sound files - en-us/gsm
ii  asterisk-modules            1:18.10.0~dfsg+~cs6.10.40431411-2           amd64        loadable modules for the Asterisk PBX

💡 说明:asterisk 是元包,真正运行体在 asterisk-modules(模块)与 asterisk-config(/etc/asterisk 全套默认配置),语音包 asterisk-core-sounds-en-gsm 提供英文提示音(约 3MB,中文提示音需另装 asterisk-core-sounds-zh,Ubuntu 22.04 仓库未收录,可从官方 downloads 站补充)。

5.3 服务管理与验证

apt 版安装即自动启用服务。确认单元文件来自软件包(/lib/systemd/system/asterisk.service),同样是 Type=notify:

systemctl cat asterisk --no-pager

实测回显(输出节选,实测首段):

# /lib/systemd/system/asterisk.service
[Unit]
Description=Asterisk PBX
Documentation=man:asterisk(8)
Wants=network-online.target
After=network-online.target

[Service]
Type=notify
ExecStart=/usr/sbin/asterisk -g -f -p -U asterisk
ExecReload=/usr/sbin/asterisk -rx 'core reload'
Restart=on-failure
RestartSec=1
WorkingDirectory=/var/lib/asterisk

与源码版模板的差异:apt 版以 -U asterisk 参数在进程内切换用户(无需单元里写 User=),Restart=on-failure 仅异常退出时拉起,另带 astcanary 看护进程监测主循环阻塞。日常管理命令(逐条执行):

systemctl is-active asterisk

实测回显:

active
systemctl status asterisk

实测回显(输出节选,实测省略 CGroup 与日志明细行):

● asterisk.service - Asterisk PBX
     Loaded: loaded (/lib/systemd/system/asterisk.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2026-10-21 11:37:50 UTC; 4h 34min ago
       Docs: man:asterisk(8)
   Main PID: 41 (asterisk)
      Tasks: 69 (limit: 94371)
     Memory: 43.1M
        CPU: 1min 19.260s

CLI 应答验证(版本串注明了 Debian 构建机与打包时间,与源码版自编译字符串不同):

asterisk -rx "core show version"

实测回显:

Asterisk 18.10.0~dfsg+~cs6.10.40431411-2 built by nobody @ buildd.debian.org on a unknown running Linux on 2022-02-12 18:24:51 UTC

六、双方案对比与选择建议

两条路线在同一台 Ubuntu 22.04 上实测的关键差异汇总:

维度方案 A:源码编译 22.11.0方案 B:apt 直装 18.10.0
版本22.11.0(最新 LTS,2026-08 发布)18.10.0(仓库冻结版本,2022-02 构建)
systemd 单元/etc/systemd/system/asterisk.service(contrib 模板手工部署)/lib/systemd/system/asterisk.service(软件包自带)
启动参数asterisk -mqf -C /etc/asterisk/asterisk.confasterisk -g -f -p -U asterisk
重启策略Restart=always(4 秒间隔)Restart=on-failure(1 秒间隔)
配置来源make samples 全套样例(约 130 个 conf)软件包默认配置(约 90 个 conf)
升级方式下载新源码包重编译 make installapt-get upgrade(随仓库更新)
内存占用(空闲实测)41.9MB43.1MB

选择建议:生产环境、需要 22.x 新特性(如改进的 PJSIP 与媒体栈)选方案 A,代价是维护编译环境;验证、实验、对版本不敏感的内网部署选方案 B,安装与升级成本最低。两者都装在同一台机器会造成 /usr/sbin/asterisk 二进制与 /etc/asterisk 配置互相覆盖,应只选其一。

七、基础配置:RTP 端口段收窄(实测闭环)

Asterisk 的语音媒体流(RTP)默认在 UDP 5000-31000 范围内动态分配端口,防火墙要么全放行这个大段、要么通话单通。rtp.conf 样例文件注释中明确了默认值:

; RTP start and RTP end configure start and end addresses
;
; Defaults are rtpstart=5000 and rtpend=31000

将端口段收窄到 10000-20000(仍满足单路通话占用 4-8 个端口的并发余量),编辑 /etc/asterisk/rtp.conf 的 [general] 段:

[general]
;
; RTP start and RTP end configure start and end addresses
;
; Defaults are rtpstart=5000 and rtpend=31000
;
rtpstart=10000
rtpend=20000

逐项说明:

  • rtpstart=10000:RTP/RTCP 端口分配起点(默认 5000);
  • rtpend=20000:端口分配终点(默认 31000),收窄后防火墙只需放行 UDP 10000-20000;
  • 其余参数(strictrtp、icesupport 等)保持样例默认即可。

重启服务使配置生效:

systemctl restart asterisk

用 CLI 验证端口段已收窄(源码版 22.11.0 实测):

asterisk -rx "rtp show settings"

实测回显:


General Settings:
----------------
  Port start:      10000
  Port end:        20000
  Checksums:       Yes
  DTMF Timeout:    1200
  Strict RTP:      Yes
  Probation:       4 frames
  Replay Protect:  Yes
  ICE support:     Yes
  STUN:            disabled
   Address:        0.0.0.0:0

apt 版 18.10.0 同样操作后实测(Port start/Port end 同样生效,末行措辞略有差异):


General Settings:
----------------
  Port start:      10000
  Port end:        20000
  Checksums:       Yes
  DTMF Timeout:    1200
  Strict RTP:      Yes
  Probation:       4 frames
  Replay Protect:  Yes
  ICE support:     Yes
  STUN address:    0.0.0.0:0

💡 说明:两条路线闭环一致——改 rtp.conf → systemctl restart asterisk → rtp show settings 显示 Port start: 10000 / Port end: 20000 即收窄成功。回显其余字段为默认值:Strict RTP: Yes 开启媒体源校验(防 RTP 注入),DTMF Timeout: 1200 为 DTMF 超时采样数(22.x 默认值,18 样例注释中的 3000 为旧默认)。

八、常见问题(FAQ)

Q1:./configure 阶段报错 error: *** JSON support requires the jansson library. 或 XML 相关失败? 缺对应开发库。安装 libjansson-dev / libxml2-dev 后重新执行 ./configure(见 4.2、4.5 节依赖清单,本文实测环境已全部预装,configure 一次通过)。

Q2:make 编译到一半报错退出? 先确认 4.2 节依赖全部装齐(尤其 libssl-dev 与 libncurses-dev),再清缓存重编:在源码目录执行 make distclean 后从 4.5 节 ./configure 重来。编译失败多由依赖缺失引起,极少需要换机器。

Q3:systemctl start asterisk 后 is-active 显示 activating 或超时失败? Type=notify 要求 Asterisk 完成模块加载并回报就绪。常见原因是 /etc/asterisk 下某配置文件语法错误导致加载卡住:先用前台模式定位——/usr/sbin/asterisk -fqf -C /etc/asterisk/asterisk.conf,观察控制台报错行,修正对应 conf 后再交给 systemd 托管。

Q4:源码版单元启动报 status=203/EXEC? ExecStart 指向的 /usr/sbin/asterisk 不存在,说明 4.6 节 make install 未执行或被中断;重跑 make install 后 systemctl daemon-reload 再启动。

Q5:通话能建立但单向无声,防火墙已放行 5060? SIP 信令通了但 RTP 媒体被拦。放行 RTP 端口段(本文收窄后为 UDP 10000-20000),并核对 rtp show settings 输出的 Port start/Port end 与防火墙规则一致;若服务器在 NAT 后还需配置外部地址(属 pjsip.conf 的 external_media_address 范畴)。

Q6:apt 版能用了吗,为什么版本停在 18? 能用于生产(18 也是 LTS 线),Ubuntu 22.04 仓库随发行版冻结在 18.10(见三、apt-cache policy 实测,无更高 Candidate)。需要 22.x 只能走方案 A 源码编译。

九、卸载与清理

卸载方案 A(源码编译版)——先停用并移除服务单元(逐条执行):

systemctl stop asterisk
systemctl disable asterisk
rm /etc/systemd/system/asterisk.service
systemctl daemon-reload

再删除程序与数据目录(源码版全部文件集中于以下路径):

rm -rf /usr/sbin/asterisk /usr/lib/asterisk /usr/share/asterisk /var/lib/asterisk \
  /var/spool/asterisk /var/log/asterisk /var/run/asterisk /var/cache/asterisk /etc/asterisk

卸载方案 B(apt 版)——连配置一起清除并自动清理依赖:

apt-get remove --purge -y asterisk
apt-get autoremove -y

卸载验证(两方案通用)——以下命令应提示命令不存在:

asterisk -V

预期回显:

-bash: /usr/sbin/asterisk: No such file or directory

⚠️ 注意:rm -rf /etc/asterisk 会删除全部话务配置(分机、中继、拨号计划),重装不会自动恢复,生产环境卸载前先备份该目录。

十、参考链接