Asterisk SIP 401 Unauthorized on an Outbound Trunk: Fix
An Asterisk SIP 401 unauthorized outbound trunk error is only a real failure if the second INVITE, sent with credentials, is also rejected or never sent. A 403 is a final refusal, so fix IP, caller ID or permissions instead.
- A 401 is a challenge, so a 401 followed by 200 OK is normal and needs no fix.
- If no second INVITE carries an Authorization header, the endpoint's outbound_auth is missing or not linked to a type=auth section.
- A second 401 means wrong username, password or realm; a 403 means the provider refused, so credentials will not fix it.
- A sudden 403 on an IP-authenticated trunk usually means your public IP changed and is no longer whitelisted.
- Confirm the fix with pjsip set logger on and a channel originate test showing INVITE, 401, INVITE with Authorization, 100, 180, 200, keeping the billed test call short.
What does an Asterisk SIP 401 unauthorized outbound trunk error mean, and how does it differ from 403 and 407?
On an Asterisk SIP 401 unauthorized outbound trunk, the 401 is a challenge. The provider replies with a WWW-Authenticate header holding a realm and nonce, and the client is expected to send the same INVITE again with an Authorization header. Asterisk does this automatically when the endpoint has outbound_auth set, so a 401 in a trace is often just the first half of a healthy call.
A 401 only counts as a failure when the retry is also rejected or is never sent. A 407 Proxy Authentication Required is the same mechanism from a proxy, using Proxy-Authenticate and Proxy-Authorization.
A 403 Forbidden is a decision. The provider understood the request, may have even validated the credentials, and refuses anyway. Resending the same password cannot change that, so the cause is policy: source IP, destination permission, caller ID, balance or limits.
| Code | Meaning | Retry with credentials helps? | Typical owner of the fix |
|---|---|---|---|
| 401 | Challenge from the server or registrar | Yes, that is the design | Your auth config |
| 407 | Challenge from a proxy | Yes | Your auth config |
| 403 | Refusal after a decision | No | Provider policy, IP, From or account |
How do I capture the SIP trace for a rejected outbound call?
Capture first and guess later. In PJSIP the logger shows every message in the Asterisk console; chan_sip has its own equivalent. For a file you can open in Wireshark or sngrep, use tcpdump on the signaling port.
If the trunk uses TLS the payload is encrypted, so use the Asterisk logger instead of tcpdump in that case.
# PJSIP
asterisk -rvvv
pjsip set logger on
# chan_sip
sip set debug on
# Packet capture (UDP/TCP 5060)
tcpdump -i any -s0 -w /tmp/sip.pcap port 5060
# Live ladder view
sngrep -d any port 5060What does a healthy 401 versus a failed one look like in the trace?
Read the sequence, not the single code. A healthy call shows the first INVITE without credentials, the 401 challenge, then a second INVITE with a higher CSeq and an Authorization header.
The challenge looks like this:
INVITE sip:0015551234567@sip.provider.example SIP/2.0
CSeq: 102 INVITE
From: <sip:1001@sip.provider.example>;tag=abc
SIP/2.0 401 Unauthorized
WWW-Authenticate: Digest realm="provider.example", nonce="8f1c...", algorithm=MD5
CSeq: 102 INVITE
INVITE sip:0015551234567@sip.provider.example SIP/2.0
CSeq: 103 INVITE
Authorization: Digest username="acct1001", realm="provider.example", nonce="8f1c...", uri="sip:0015551234567@sip.provider.example", response="d41d..."
SIP/2.0 100 Trying
SIP/2.0 180 Ringing
SIP/2.0 200 OK Why does the second INVITE never carry credentials, or get a second 401?
Two different failures hide behind the same code. If there is no second INVITE with an Authorization header, Asterisk had nothing to answer with. If there is one and the provider returns 401 again, the credentials were sent and rejected.
In the first case, check that the endpoint's outbound_auth points to a section of type=auth. Auth sections linked by auth= are used for inbound challenges, not for answering the provider. Confusing the two is the most common mistake.
In the second case, compare the auth username with what the provider expects. The auth ID is often not the DID or the account name. Look for trailing spaces, special characters such as # or ; in the password, and copy-paste errors. A realm that does not match can also fail, so leaving realm blank is usually safest because PJSIP then answers any realm. One shared realm across several accounts can make Asterisk pick the wrong auth section.
; pjsip.conf (plain Asterisk)
[provider]
type=endpoint
transport=transport-udp
context=from-trunk
disallow=all
allow=ulaw,alaw
outbound_auth=provider-auth
aors=provider
from_user=acct1001
from_domain=sip.provider.example
direct_media=no
rewrite_contact=yes
send_pai=yes
[provider-auth]
type=auth
auth_type=userpass
username=acct1001
password=S3cretPass
; realm= left blank
[provider]
type=aor
contact=sip:sip.provider.example:5060Registration or IP authentication: which does my trunk use?
A registering trunk sends REGISTER with credentials and the provider then accepts calls from that account. It needs a registration section, and pjsip show registrations must show Registered. An IP-authenticated trunk has no registration. The provider whitelists your public IP and accepts INVITEs from it, and you add an identify section so inbound calls from the provider match your endpoint.
This explains a sudden 403 with no config change. If your ISP changes your public IP, or NAT egress moves to a different address, the whitelist no longer matches and the provider refuses with 403. Check your current egress address and compare it with the provider portal.
A registered trunk can also give 403 on INVITE while REGISTER succeeds, which points to destination, caller ID or limits rather than auth.
[provider-reg]
type=registration
outbound_auth=provider-auth
server_uri=sip:sip.provider.example
client_uri=sip:acct1001@sip.provider.example
retry_interval=60
[provider-identify]
type=identify
endpoint=provider
match=203.0.113.10
; transport for NAT
[transport-udp]
type=transport
protocol=udp
bind=0.0.0.0
external_signaling_address=198.51.100.20
external_media_address=198.51.100.20
local_net=192.168.1.0/24 What causes a 403 Forbidden on an outbound call, and how do I read it?
Read the reason phrase and any Reason or Warning header first, since providers often state the cause. Then match the trace to the table. Triage by symptom keeps you from changing passwords when the problem is policy.
Check From and Contact too. Providers often require the From user to equal the account ID or an owned DID, and may require P-Asserted-Identity or Remote-Party-ID. For example, From: <sip:+15550001111@host> when only 15550002222 is owned draws a 403. Set from_user, from_domain, callerid and send_pai or trust_id_outbound to match. For NAT, set contact_user, rewrite_contact=yes, external_signaling_address, external_media_address and local_net.
Transport and port matter as well. If the provider expects TCP, TLS or a port other than 5060 and you send UDP to 5060, you get a 403 or silence. Set the transport on the endpoint and use outbound_proxy=sip:host:port where the proxy differs from the domain.
| Symptom in trace | Likely cause | Fix |
|---|---|---|
| 401 then 200 OK | Normal challenge | None |
| 401, INVITE without Authorization | outbound_auth missing or not linked to type=auth | Set outbound_auth on the endpoint |
| 401 twice | Wrong password, auth username or realm | Correct credentials, blank realm, check hidden spaces |
| Direct 403 on first INVITE | IP not whitelisted or From not allowed | Whitelist public IP, fix from_user |
| 403 after REGISTER succeeded | Destination prefix, balance, channel or CPS limit, caller ID | Enable destination, add credit, fix callerid |
| 403 on only some numbers | International, premium or mobile blocked, or dial format | Enable in portal, use the E.164 or 00 format required |
| No reply, then timeout | Wrong port, transport, or firewall or fail2ban block | Match transport, check fail2ban and portal blocks |
SIP/2.0 403 Forbidden
Reason: SIP ;cause=403 ;text="Source IP not allowed"
From: <sip:1001@sip.provider.example>;tag=abc
CSeq: 103 INVITE How do I fix this in FreePBX and confirm the call works?
In FreePBX open the trunk, then the pjsip tab. Fill Username, Auth username (when it differs), Secret, Authentication (Outbound for registering), Registration (Send), From User, From Domain, Client URI, Server URI and Outbound Proxy. Click Submit and then Apply Config, because changes do nothing until applied.
Inspect the generated files, but never edit them by hand because FreePBX overwrites them: /etc/asterisk/pjsip.auth.conf, pjsip.endpoint.conf and pjsip.registration.conf. Also check that fail2ban has not banned your own server.
Then verify with the steps below. The last test places a live call to a real PSTN number, so read the cost warning first.
- Run pjsip show endpoint provider and confirm outbound_auth and from_user.
- Run pjsip show auth provider-auth to confirm username and realm.
- Run pjsip show registrations and confirm Registered (registering trunks only).
- Run fail2ban-client status asterisk and unban your IP if listed: fail2ban-client set asterisk unbanip 198.51.100.20.
- Turn on pjsip set logger on and place: channel originate PJSIP/0015551234567@provider application Playback demo-congrats. This is a real, billed call, so use a number you own and let it end as soon as the prompt plays.
- Confirm the trace shows INVITE, 401, INVITE with Authorization, 100, 180, 200 for the channel originate test.
Common questions
Is a SIP 401 always an error?
No. A 401 is the normal first response from a provider that requires digest authentication. It is only a problem if the retry with an Authorization header fails or is never sent.
Why does the provider return 403 even with the right password?
A 403 is a policy refusal, so the password is not the issue. Common reasons are a non-whitelisted source IP, a blocked destination, a disallowed caller ID, no credit or a channel limit.
Does the username have to match the auth username in PJSIP?
Not always. The username in the auth section is what goes into the Authorization header, while from_user and client_uri control identity in From. Many providers use a separate auth ID, so set each as they specify.
How do I tell whether fail2ban blocked my own server?
Run fail2ban-client status asterisk and look for your server address in the banned list. Repeated 401s from bad credentials can trigger it, and unbanning with fail2ban-client set asterisk unbanip clears it.
Do chan_sip trunks behave differently?
The SIP exchange is identical. In chan_sip you set secret, defaultuser and fromuser and use register => lines, and you debug with sip set debug on instead of pjsip set logger on.